Spi interupt (spie) and spdr makes me problem.

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

Hi dears.
I work with atmega16, in both side master and slave.
I want to send a numbr between 100-999 b that entered by keypad to the slave.
Numbers are 16 bit ,and spdr register is 8 bit, so i send first lsb then msb bits. This works well.
But my problem is in slave side:
I enabled interupt spie in slave side so each spdr recived from master tooke as a interupt and goes to ISR function. But becouse of my number i want to take each 16 bit or two byte as a interupt. What should i do???
Please help me. Thanks.

Aasshh

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

Aashkanpasha wrote:
What should i do???

volatile uint16_t SPI_received;
volatile bool  SPI_valid;

ISR(SPI_vect) {
    static bool low = false;
    if (!low) {
        SPI_received = (SPDR << 8);
        SPI_valid = false;
        low = true;
    }
    else {
        SPI_received |= SPDR;
        SPI_valid = true;
        low = false;
    }
}

This toggles between two states (low / !low). I'm assuming you send high byte then low byte each time. First time !low is true so it puts the reading into the upper 8 bits of SPI_received, marks the reading as not yet valid and then toggles to wait for the low byte. The second time "low "is true so this time it ORs the other 8 bits into the low half of SPI_Received and marks it as a valid, full 16 bit reading then toggles state again to wait for the next high byte.

 

(or something along these lines anyway)

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

Or split your value into two 7-bit values. Set the top bit high for the MSB and low for the LSB. You then have enough information to accurately reconstruct your data.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Or use the CS line to synchronise.  e.g.

 

CS_LO;   //active

spi(hi);

spi(lo);

CS_HI;   //release