SPI master - lost every second byte

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

Hi,

when I send some data in SPI master mode (ATmega168) I noticed strange behavior. Every time data block is sent, second byte is lost (not transmitted).
The operation is as follows.:

1. Load byte #1 to SPDR
2. wait for buffer empty (SPIF bit)
3. send byte #2
4. wait for buffer empty (SPIF bit)
5. send byte #3
6. wait for buffer empty (SPIF bit)
and so on.

I suspect it's connected with the way SPI module buffers data, but I'd rather expect that 1st byte is lost.
Temporary solution is just to send twice byte #2.
I played with timing an other things but without satisfying results.
Anybody know clean solution of this problem?

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

is this done in interrupt, or just wait for send to complete and then send next.

perhaps post the plaqued code parts like the SPI tx routine SPI_init routine and the routine that puts the bytes in the buffer.

then we can check if nothing strange happened there.

regards

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

The code is very typical, I didn't discover anything new. Init & send is the same as in Atmel app note.
The entire data send procedure is in interrupt. The chip is in sleep mode and timer1 generates interrupt which sends the data. I thought maybe sleep mode is a problem..

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

well you can test this. just for a starter stop going to sleep and see if the problem is disappeared.

It also can be that it goes wrong somewhere in the interrupt that you are to quick trying to send the data or the data is overwritten.