does anyone have a easy tutorial for SPI?

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

does anyone have a easy tutorial about SPI?

I am a newbie, please email me, or leave a link here,

many thankz
vincent

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

There is not much to it. There is sample code in the datasheet that shows you the main operation. Aside from that, there are just a few options to consider. If you have multiple slaves, just have the master pull the SS line of the current target to low. The thing you need to remember with SPI is that every transmission is both a send and a receive on both sides, though frequently only one direction or the other on any given transmission may not really mean anything. That really depends on the data transmission protocol that you are using.

Regards,
Steve A.

The Board helps those that help themselves.

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

thankz steve,

I hope some experts can make a tutorial here,

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

I do not think that there is much to tutorial on.....

you init the spi peripheral by setting it in the mode wanted. then the only thing you do is make the chip select line low of the peripheral that you want to talk to and send a byte to it.

I think you perhaps need to do a google search on how SPI is working and not how to implement it on an AVR. It sounds all difficult, but it all is very straight forward. send a byte and at the same tiem receive a byte.

regards

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

What possible "tutorial" could there be? Apart from getting your brain around the CPOL/CPHA selection a five year old child could program SPI.

I guess the only possibly non-obvious fact is that every transmit is also a receive and the slave cannot instigate a transfer. So if the slave has a byte to be delivered the master MUST send something to trigger the transfer (the generation of 8 SCK pulses).

As such it doesn't make sense for a master to have separate transmit and receive routines. But instead a composite:

uint8_t SPI_send_receive(uint8_t data) {
  // transmit the byte to be sent
  SPDR = data;
  // wait for the transfer to complete
  while (!(SPSR & (1<<SPIF)));
  // then return the byte the slave just returned
  return SPDR;
}

which is used in one of three ways:

SPI_send_receive(0xNN); // send only
retval = SPI_send_receive(0xNN); // send and receive
retval = SPI_send_receive(0xFF); // receive only where the 0xFF that is sent is a "don't care" value that the slave will ignore

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

One common mistake using AVR SPI is to not pay attention to that /SS pin must NOT be input/low in Master mode. If that happens SPI switch to Slave mode.
It's all in the datasheet but who has time to read a datasheet.

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

This tut my be helpful. Follow along withe the data sheet.
http://www.ermicro.com/blog/?p=1050
Atmel also has some nice app notes on the topic at their website. IIRC app note 317 and several others would be a good read.

Self proclaimed Captain Link