SPI and RTC

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

first, Im starting to learn SPI using atmega8. Tried to simulate it in AVRstudio4. The SPDR doesnt seem to load the data written to it. Is it supported in this AVR version? If not, how will i know that the data is actually passed to the slave, say the DS1306 RTC chip?

In my SPI interrupt, i'll write data to the SPDR, should i also read data from the SPDR at the same time? say,

void SPI_int (void)
{
  SPDR = datatransmit;        //transmit this data
  arrvieddata = SPDR;          //read data from slave
}

thanks...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wrong function. Try this:
unsigned char SPI_int (unsigned char data)
{
SPDR = data;
while (!(SPDR & 0x80));
return SPDR
}
of course you have to init SPI..
Kostas

It's better to keep your mouth shut and think you a fool, than open it and move out the doubts!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!

Quote:
kostas wrote:
unsigned char SPI_int (unsigned char data)
{
SPDR = data;
while (!(SPDR & 0x80));
return SPDR
}

It should be SPSR instead.
/ C

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:oops: :oops: :oops: I apologize...ATMEGAMAN is correct.It's SPSR indeed.
Kostas

It's better to keep your mouth shut and think you a fool, than open it and move out the doubts!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so i should wait for the interrupt flag to be cleared before i read the register?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes! The flag signals end of transmission. Is the SPI set up properly? Is it enabled?
I'd suggest reading through the relevant section in the data sheet.
/ C

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Yes! The flag signals end of transmission

that is why the interrupt is generated. i don't think i still have to read this flag. isn't it automatically cleared when my program services the ISR? i put that code 'cause somewhere in my program i started the transmission by writting a single byte into SPDR. to continously transmit the remaining strings, i enabled the SPI interrupt (code above).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm fumbling in the dark right now. Please post your code (init as well) it will make it easier to see what the problem is.
/ C

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Tried to simulate it in AVRstudio4. The SPDR doesnt seem to load the data written to it. Is it supported in this AVR version?

Data is transferred (out'ed) in just one cycle and the AVRStudio simulator represents the transmision (i.e. the writing of SPDR) only during this single cycle. You can 'see' the transmission if you watch the PINxx of the AVR after writing SPDR by single-stepping though the code the right number of cycles (this is dependant of the clock frequency you selected for SPI transmission clock). On the MOSI pin, you'll see the right bit state.
Quote:

void SPI_int (void) 
{ 
  SPDR = datatransmit;        //transmit this data 
  arrvieddata = SPDR;          //read data from slave 
}


Quote:

should i also read data from the SPDR at the same time? say,

This code isn't an ISR, but I suppose you call this function from inside the ISR code. If you want to read what the SPI slave sent to the master, you must read SPDR BEFORE writting to it again.

Quote:

i don't think i still have to read this flag

You're right, if you are using interrupts, you don't need to read the flag again.