16Bit SPI

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

Hi All

I would like to receive a 16bit SPI in a AVRmega1284p say. Ultimately i would like to receive the 16bits in a lower & upper byte (would have to anyway) & then send them out in usart specification to a wireless transmitter. That leads me to my second question of can you have one port being SPI & the other being USART?

Or there a C coded Software SPI example if the hardware cant do it? (I have looked ) :)

Thanks
Warren

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

Well to receive 16 bits is easy, it's just two SPI transfers. To receive on SPI (assuming the AVR is master) requires you to transmit two bytes.

As for using both SPI and UART. Surely that's not an issue?>? They are on separate sets of pins

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
    spi_init();
    uart_init();    
    ...
    hi = spi(0);       // send a dummy value
    lo = spi(0);
    uart_putc(hi);
    uart_putc(lo);
    ...

Look at the pin-out of your AVR. The SPI is on different pins to the UART.
You can use one UART for SPI and another UART for RS232. The regular SPI is simpler than using the UART for SPI.

David.

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

Oh yes the SPI is on a different pinout. I just imagined they used the Usart pins. Silly me haha. Thanks David.

To Receive 16bits in a hi lo configuration isn't a problem but the ADC that is sending them will send them all at once from what i can see on the data sheet. Is this not the case in real life due to some SPI protocol, that the AVR can tell the ADC to pause at 8bits & then send the next 8bits when requested?

Or is the AVR that quick it can deal with the incoming pulses count to eight bits & store them in a separate register before the other 8bits has had a chance to turn up? Im guessing that would only be true if the AVR is the master. I realize it would be my code to tell it to store the value in a separate register.

Thanks
Warren

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

warren1 wrote:
To Receive 16bits in a hi lo configuration isn't a problem but the ADC that is sending them will send them all at once from what i can see on the data sheet. Is this not the case in real life due to some SPI protocol, that the AVR can tell the ADC to pause at 8bits & then send the next 8bits when requested? Or is the AVR that quick it can deal with the incoming pulses count to eight bits & store them in a separate register before the other 8bits has had a chance to turn up? Im guessing that would only be true if the AVR is the master. I realize it would be my code to tell it to store the value in a separate register. Thanks Warren
If the AVR is the SPI master then there is no problem at all since the AVR is supplying the clock to shift out the data! In other words the slave (ADC) must run at the AVR's speed.
If the ADC were the SPI master it would be different, but I doubt that is the case.

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

[If the AVR is the SPI master then there is no problem at all since the AVR is supplying the clock to shift out the data! In other words the slave (ADC) must run at the AVR's speed.
If the ADC were the SPI master it would be different, but I doubt that is the case.]

So you are saying the AVR is quick enough (Master clock is faster) to move the 8bits out of the buffer into a register before the next 8 in the 16bit sequence arrive?

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

warren1 wrote:
So you are saying the AVR is quick enough (Master clock is faster) to move the 8bits out of the buffer into a register before the next 8 in the 16bit sequence arrive?

No, you don't understand how SPI works. To receive a byte from the slave the master must send a byte to the slave. Doing this causes the master to activate the clock line which then clocks out the (dummy)data while clocking IN the slave's data. So you send one dummy byte, read the received byte, then send a second dummy byte and read the second received byte. The master is in complete control of WHEN the slave data is clocked in.

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

Ah so its a send one bit & receive one bit transfer & could be stopped at any bit if desired. I thought there would be a request & you would get the lot lol.

Ok ill will have to look at some C code to work out how to send data on the SPI bus but i guess you could send a decimal value of 255 & that would get you the first 8bits then another 255 to get the last 8 right?

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

Quote:
i guess you could send a decimal value of 255 & that would get you the first 8bits then another 255 to get the last 8 right?

Correct but the actual value you send is irrelevent, UNLESS you are also controlling the ADC as well in which case it's possible you'll have to send data that doesn't cause any problems.

______
Rob

Scattered showers my arse -- Noah, 2348BC.
Rob Gray, old fart, nature photographer, embedded hardware/software designer, and serial motorhome builder, www.robgray.com

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

warren1 wrote:
Ah so its a send one bit & receive one bit transfer & could be stopped at any bit if desired. I thought there would be a request & you would get the lot lol.

No. You can only send / receive 8 bits at a time.
If you only want 3 bits, you discard the other 5 bits. Likewise you send 8+8 bits to receive 16 bits back.

I would guess that you actually want to control a MicroWire or ADC device. These 'appear' to use an odd number of bits. In practice, they respond to either the first N bits after chip-enable or the bits that follow a 'start bit'.

Life is a lot easier if you ask 'How do I control a LTC1298 ?' or whatever.

David.

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

Did you read the first response I made? You keep asking subsequent questions that were answered by what I wrote above.