Embedded System Design
Experiment 7
SPI PROTOCOL
Date: 14-9-2018
Name: Aishwarya Yagnyaprabha Sahoo
Registration number: 16BEC1254
Aim: To perform SPI protocol using mbed board
General information:
The mbed NXP LPC11U24 Microcontroller in particular is designed for prototyping low
cost USB devices, battery powered applications and 32-bit ARM® Cortex™-M0 based
designs. It is packaged as a small DIP form-factor for prototyping with through-hole
PCBs, stripboard and breadboard, and includes a built-in USB FLASH programmer.
The switch and LED are grounded along with the GND terminal of the microcontroller. The
supply given was the regulated 3.3 V supply. The I/O ports are from p5 to p30.
External components:
API Used:
Basic input and output fuctions: putc(), getc(), printf() etc.
For PWM Output pin:
PwmOut variable(p5,…..,p30);
To set the PWM
period:
variable.period(value’f’);
For DAC:
AnalogIn ain(p20);
For enabling the serial port:
Serial pc(USBTX,USBRX);
Other serial port API:
Serial bt(p9,p10);
To set baud rate:
variable.baud(value);
For delay:
wait(t) ; where ‘t’ is in seconds
API’s for Master:
API’s for
Slave:
The SPI interface:
The SPI Interface can be used on pins p5/p6/p7 and p11/p12/p13
Default settings of the SPI interface on the mbed:
–Default clock frequency of 1 MHz
–Default data length of 8 bits
–Default mode of 0
TASK 1:
Question:
Set the mbed up as Master, and exchanges data with a slave, sending its own switch
positions, and displaying those of the slave using LEDs.
Code:
MASTER
#include "mbed.h"
SPI ser_port(p11,p12,p13); DigitalOut
led1(LED1); DigitalOut led2(LED2);
DigitalOut cs(p14); DigitalIn switch_ip1(p7);
DigitalIn switch_ip2(p8); char switch_word;
char recd_val; int main() {
while(1) { switch_word=0xa0;
if(switch_ip1==1)
switch_word=switch_word|0x01;
if(switch_ip2==1)
switch_word=switch_word|0x02; cs=0;
recd_val=ser_port.write(switch_word);
cs=1;
wait(0.01);
led1=0;
led2=0; recd_val=recd_val&0x03;
if(recd_val==1)
led1=1; if(recd_val==2) led2=1;
if(recd_val==3){ led1=1; led2=1;}
}
SLAVE
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14);
DigitalOut led1(p21);
DigitalOut led2(p22); DigitalIn
switch_ip1(p5); DigitalIn
switch_ip2(p6); char switch_word;
char recd_val; int main() {
while(1) { switch_word=0xa0;
if(switch_ip1==1)
switch_word=switch_word|0x01;
if(switch_ip2==1)
switch_word=switch_word|0x02;
if(ser_port.receive())
{
recd_val=ser_port.read();
ser_port.reply(switch_word);
led1=0;
led2=0; recd_val=recd_val&0x03;
if(recd_val==1)
led1=1; if(recd_val==2) led2=1;
if(recd_val==3){
led1=1;
led2=1;
Output:
TASK 2:
Question:
Display text typed into the MASTER terminal application on to the slave serial terminal.
Code:
MASTER
#include "mbed.h"
SPI ser_port(p11,p12,p13);
Serial pc(USBTX,USBRX);
DigitalOut cs(p14); char c;
char recd_val; int main() {
while(1) { cs=0; c=pc.getc();
recd_val=ser_port.write(c); cs=1;
wait(0.01);
SLAVE
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14);
Serial pc(USBTX,USBRX);
char switch_word; char recd_val;
int main()
{
while(1)
if(ser_port.receive())
recd_val = ser_port.read();
pc.printf("%c",recd_val);
}
Output:
-
MASTE
R
- SLAVE
Verification:
Result:
Thus, the SPI protocol is followed using the mbed board. The tasks given are
completed and its corresponding outputs are shown.