Hardware Reference
In-Depth Information
Table 10-9. Null Byte at the End of the String
Transfer #
1
2
3
4
5
6
7
Sent
S
A
M
P
L
E
-
Received
-
S
A
M
P
L
E
The transfer between master and slave is procedural and requires the connection to be maintained consistently
between the devices. The communication in the sample code follows the steps in Table 10-10 . The example sends one
byte at a time as Table 10-10 shows; however, large strings can be sent within a loop. The use of loops for sending data
is demonstrated in Listing 10-3, along with an alternative method to create master device by using registers.
Table 10-10. Master and Slave Communication Steps
Master
Slave
1. Drop slave select low
1. Begin listening to master
2. Write data to be sent
2. Write data to be sent
3. Full-duplex transfer
3. Full-duplex transfer
4. Read received data
4. Read received data
5. Slave select high
5. Return to idle
Multiple Slaves
Developing a master through register manipulation is a logical next step to developing tightly controlled protocols.
The next feature I'll address, though, is connecting to multiple slaves. Under a normal four-wire SPI connection, the
addition of slaves beyond the first requires additional SS lines controlled by the master. The MISO, MOSI, and SCK
are shared lines between all devices on an SPI network; however, the SS line will be separated under all but the most
unusual SPI networks. This allows the master to select one slave at a time, and a slave that is not signaled will ignore
the communication.
Master in Register
While in most cases, the SPI library will suffice in the creation of a master SPI device, it will fall short when creating
more complex protocols. For that reason, and to gain a better understanding of the SPI, writing the master code in
register is the next step and is shown in Listing 10-3.
Listing 10-3. Master Code Register Sketch
const int bufferSize = 64; // Sets the size of the txBuffer and the rxBuffer
byte txBuffer[bufferSize]; // Created to hold data waiting to be sent to the slave
byte rxBuffer[bufferSize]; // Created to hold incoming data received from the slave
void setup() {
Serial.begin(115200);
DDRB |= 0b00101101; // LED(8) MOSI SCK SS Output
PORTB |= 0b00000100; // Set slave select HIGH
 
Search WWH ::




Custom Search