Author Topic: AVR SPI slave  (Read 9046 times)

0 Members and 1 Guest are viewing this topic.

Offline izuaTopic starter

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
AVR SPI slave
« on: June 13, 2008, 01:02:15 PM »
Hi!
I'm having a problem making an atmega16 act as a slave device. No one seems to know how it's done around avrfreaks.
I have one atmega16, which runs at 10khz. This is the sender. It sends after every reset the C string "testing"
The receiver runs at 16MHz. Whenever it detects the SPI flag set, it takes SPDR, puts it in a buffer, and increases the buffer pointer.
if SPDR == 0, then it calls a functions which prints every character from buffer up to the pointer, and then resets the pointer.

The flag was set only once, after the first byte.
Not good. So i tweaked the software, reducing and minimising it till the very least. NOTHING WORKED!
This is my current version.

Code: [Select]
int main(void)
{
refresh = 0;
iptr = 0;
initSerial();
SPCR = 1 << SPE;
DDRB = (1 << PB6);
uart_out("SPI test\r");
DDRA = 0xFF;

// sei();
while(1)
{

while ((SPSR & SPIF) == 0)
;
uint8_t k;
k = SPDR;
SPDR = k;

SPDR = 0xAA;
PORTA ^= 0xFF;

}

}

This is supposed to toggle PORTA everytime a byte is received by SPI. Signals are ok (checked with ISIS VSM scope).
What am I doing wrong?
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: AVR SPI slave
« Reply #1 on: June 13, 2008, 03:28:10 PM »
Since you are trying to make a SPI slave SS signal is more than needed...

Did you include that????

SPI gotta act as a interrupt if you expect not to have data losses...

Did you checked SPI speed... the transmitter gotta have same or less speed than receiver...

Is the crystal 10KHz or 10MHz???


If you transmit data back you gotta have same transmitting receiving speed...

You use different crystals... use same and test...

There is a prescaler for SPI speed for example if you use a prescaler of 16 and a 16MHz crystal then you gonna get a 1MHz SPI
And generally 10MHz is no good speed for SPI... cause the prescaler is 4, 16, 64, 128......

Look... I haven't work that much with SPI so I may tell you nothing good right know...
At least this is want I remember... from the compiler I use (BASCOM)


Hope I helped and not confused...
Lefteris
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline izuaTopic starter

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
Re: AVR SPI slave
« Reply #2 on: June 13, 2008, 03:58:49 PM »
SS doesn't have to act as an interrupt, since it's put to 0 only at the frame start. Which means, you can interrupt on it only once.
I think I'm pretty clear about clocks :P master sender 10khz, receiver 16Mhz. Sincer the master is driving the clock, the speed of instruction execution is irrelevant (as long as the slave can keep up with the clock). SPI is clocked, more exactly. Only one end generates the clock.

I don't think the speed actually counts, since the electronics check the transitions, and are not locked to powers of two, like 16, 8, etc..
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline TrickyNekro

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: AVR SPI slave
« Reply #3 on: June 13, 2008, 06:59:04 PM »
I didn't say that the SS acts as interrupt...

I said SPI acts as interrupt meaning that there is a clock pulse before the real transmition etc etc etc....

The thing is that this must be a code problem as I see it....

Nothing I can help since I use basic and you use C...


Check one or two parameters... And wish you best of luck...

Lefteris
« Last Edit: June 16, 2008, 04:48:38 AM by TrickyNekro »
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline izuaTopic starter

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
Re: AVR SPI slave
« Reply #4 on: June 13, 2008, 08:02:11 PM »
Write it in BASIC. Since SPI on ATMEGAs is a hardware layer, I'll translate it later to C.
I just need to understand how to initialise the registers properly.

I've tried several versions, polling the flag and via interrupt, with opposing clock polarities and in phase clock, changed sampling edges, toggling a led on byte receipt or storing messages in a buffer, sending data in both directions or just receiving it. Nothing works. I can't get the interrupt/flag to trigger after the first byte.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

 


Get Your Ad Here

data_list