NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Master/Slave SPI
May 03, 2011 by Noter |
Here is an example of master/slave communication between the nerdkit and another ATmega using the SPI interface. It's quite simple, the master sends a number to the slave, the slave increments the number and sends the result back to the master. Although SPI is full duplex, the catch is that the slave can't give the result back to the master until the next time the master sends a number. Other than that one byte delay, SPI is fairly easy to grasp.
================================================================================================
|
---|---|
May 04, 2011 by Ralphxyz |
Man Paul, that is great, I've just gone through it quickly. One question what about multiple Slaves, I am sure it is in there but ... Ralph |
May 04, 2011 by Noter |
Hi Ralph, This example has only one slave and uses the master SS pin to select it. For more slaves you need to use other master pins connected to the slave(s) SS to select them. Don't be confused by my use of the master SS in this example, it's not absolutely required. Hexorg has shown on another thread the master SS needs to be configured as output else it is used to enable/disable the SPI interface. So it's probably a good rule of thumb to use the master SS for your first slave just to be sure it is always configured as output. Paul |
September 08, 2011 by Rachid |
Hi, Can anyone explain the following line of code: define OUTPUT2(port,pin) DDR ## port |= _BV(pin)I understand this what #define OUTPUT2(port,pin) port |= _BV(pin) means but I dont understand DDR##. Thank you |
September 09, 2011 by 6ofhalfdozen |
Rachid, Noter would be the expert since its his code, but I believe the DDR ## stands for the Data Direction Register for Port ##, which you pick/assign. As I recall this is part of one method of assigning pins as inputs or outputs. Hopefully that helps enough until Noter or someone more knowledgable can answer. |
September 09, 2011 by bretm |
The ## is the concatenation operator. "port" is a string such as "B" but you can't just say DDRport because that's just a different identifier. DDR ## port concatenates the string "DDR" and the value of port, so you get "DDRB" |
September 09, 2011 by Ralphxyz |
Is that concatenation operator (##) only a C pre-processor operator? Ralph |
September 10, 2011 by bretm |
Yes, that's right. |
September 14, 2011 by Rachid |
Thanks alot guys. |
May 28, 2012 by sask55 |
This is a very good thread that can be used by anyone as an example of SPI. Some time ago I noticed one small inaccuracy that would be inconsequential for most people that may be basing a SPI communication project from this thread. I thought that I might point out changes that could be made in order to make the SPI operate as expected at all possible speeds. The set of defined prescalers will not work as expected for values above 3
That is to say the statement used to set the bits in the SPCR register
Will fail for the last four SPI clock ratios listed. The statement above will set the 3 LSB (lease significant bits) in the SPRC register in accordance with the values defined in the list. From the data sheet the 3 LSB of SPRC register are CPHA, SPR1 and SPR0. CPHA is the Clock phase bit not a clock rate select bit. From table 18-5 in the data sheet Bit 0 (SPR0) and bit 1 (SPR1) are the correct bits to set the SPI clock rate ratio. However the MSB from this table is SPI2X which is bit 0 on the SPSR register. So in order to select a SPI clock ratio from the lower half of the table, bits should be set as required in both registers. example of fosc/32 using Noter method
or
Excellent thread: I just thought this issue if not noticed may cause a problem in some applications |
Please log in to post a reply.
Did you know that you can make a spooky Halloween Jack-O-Lantern with a microcontroller? Learn more...
|