Question
Use Program Example 7.3 on pages 123-124 to answer the following questions: a) explain how the command if(ser_port.receive()) works. b) review the block of code
Use Program Example 7.3 on pages 123-124 to answer the following questions: a) explain how the command if(ser_port.receive()) works. b) review the block of code following the command if(ser_port.receive()): will it be true even if no button is closed on the master? Is it necessary to close a button on the master for the slave to send the switch_word variable back to the master?
here is the program
/*Program Example 7.3: Sets the mbed up as Slave, and exchanges data with a Master, sending its own switch positions, and displaying those of the Master. as SPI slave.
*/
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14); // mosi, miso, sclk, ssel
DigitalOut red_led(p25); //red led
DigitalOut green_led(p26); //green led
DigitalIn switch_ip1(p5);
DigitalIn switch_ip2(p6);
char switch_word ; //word we will send
char recd_val; //value received from master
int main() {
//default formatting applied
while(1) {
//set up switch_word from switches that are pressed
switch_word=0xa0; //set up a recognisable output pattern
if (switch_ip1==1)
switch_word=switch_word|0x01;
if (switch_ip2==1)
switch_word=switch_word|0x02;
if(ser_port.receive()) { //test if data transfer has occurred
recd_val = ser_port.read(); // Read byte from master
ser_port.reply(switch_word); // Make this the next reply
}
//now set leds according to received word
...
(continues as in Program Example 7.2)
...
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started