SPI slave on ATmega162

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hello.

I'm studying mega162 chip and trying to implement SPI slave capability. I
wanna send 8 bytes to master side and after reaching last byte get back and
send again, ie continuosly.

Later I'd like to implement receiving.

So, I made the following simple code:

#include <io.h>
#include <interrupt.h>
#include <sig-avr.h>

char *data = "12345678"+0x00;
char *t;

void SPI_SlaveInit(void)
{
   DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7);
   SPCR = (1 << SPE);
  PORTB |= (1 << PB6);
}

int main(void)
{
   sei();
   SPI_SlaveInit();

   t = data;
   while (1)
   {
   if (*data != 0) {
     SPDR = data;
     data++;
   }
   else data = t;
   }

   return 1;
}

According to oscilloscope SCK, SS and MOSI signals (I get from master side)
are fine, but MISO is some garbage, so it seems I'm not sending any real
data... While debugging in studio, I can see that SPDR is filled with my
data, byte by byte, but how can I check this data are moved on to the shift
register? I suppose only interrupt call...

I'm using AVR studio and WinAVR compiler.

Thanks in advance for any help.


With best regards, Roman Mashak.  E-mail: snipped-for-privacy@tusur.ru



Re: SPI slave on ATmega162

Quoted text here. Click to load it
and
side)
Quoted text here. Click to load it
shift

I haven't studied all your code, but I found two major errors:
- You have to wait for the previous byte to be sent before you write a new
value to SPDR. Something like this:
- You should write whatever data points at to SPDR, not the pointer value
itself.

while (1) {
   if (SPSR & (1 <<SPIF)) {
      if (*data == 0) {
         data = t;
      }
      SPDR = *data;
      data++
   }
}

-Jan Leithe



Re: SPI slave on ATmega162
Hello, Jan!
You wrote  on Thu, 25 Nov 2004 09:01:04 +0100:

 Jl> I haven't studied all your code, but I found two major errors:
 Jl> - You have to wait for the previous byte to be sent before you write a
 Jl> new value to SPDR. Something like this:
 Jl> - You should write whatever data points at to SPDR, not the pointer
 Jl> value itself.

 Jl> while (1) {
 Jl>    if (SPSR & (1 <<SPIF)) {
 Jl>       if (*data == 0) {
 Jl>          data = t;
 Jl>       }
 Jl>       SPDR = *data;
 Jl>       data++
 Jl>    }
 Jl> }
    Thank you for help with C, but unfortunately problems still remains. I
suppose, it's hardware related...

With best regards, Roman Mashak.  E-mail: snipped-for-privacy@tusur.ru



Re: SPI slave on ATmega162
Hello, Jan!
You wrote  on Thu, 25 Nov 2004 09:01:04 +0100:

 Jl> I haven't studied all your code, but I found two major errors:
 Jl> - You have to wait for the previous byte to be sent before you write a
 Jl> new value to SPDR. Something like this:
 Jl> - You should write whatever data points at to SPDR, not the pointer
 Jl> value itself.
By the way, while debugging this code in AVR studio (after modification
according to your notes) I noticed that data put into SPDR register are
strange. Originally I'm sending string "12345678", but character codes
stored into SPDR are 0x31, 0x32, 0x33 etc.

Why ?
 Jl> while (1) {
 Jl>    if (SPSR & (1 <<SPIF)) {
 Jl>       if (*data == 0) {
 Jl>          data = t;
 Jl>       }
 Jl>       SPDR = *data;
 Jl>       data++
 Jl>    }
 Jl> }


With best regards, Roman Mashak.  E-mail: snipped-for-privacy@tusur.ru



Re: SPI slave on ATmega162
Quoted text here. Click to load it

That is correct. A 0x31 is the hexadecimal ASCII code for character '1',
0x32 for a '2' and so on....

Meindert



Re: SPI slave on ATmega162
Hello, Meindert!
You wrote  on Thu, 25 Nov 2004 12:27:56 +0100:

 ??>> By the way, while debugging this code in AVR studio (after
 ??>> modification according to your notes) I noticed that data put into
 ??>> SPDR register are strange. Originally I'm sending string "12345678",
 ??>> but character codes stored into SPDR are 0x31, 0x32, 0x33 etc.

 MS> That is correct. A 0x31 is the hexadecimal ASCII code for character
 MS> '1', 0x32 for a '2' and so on....
 oh,, sorry for my stupidity :)

Anyway, I still don't get output signal looking at oscilloscope.  I believe,
I should make on slave side some checking of SPI is ready, SS checking or
similar?

With best regards, Roman Mashak.  E-mail: snipped-for-privacy@tusur.ru



Site Timeline