Embedded Lab 05

You might also like

You are on page 1of 6

Department of Electrical Engineering

Faculty Member: Dr. Usman Zabitt Dated: 08-10-2019

Semester: 7th Section: BEE-8D (Group 1)

EE-423: Embedded System Design

Lab 5: I2C and SPI Communication using Hardware


Abstraction Layer

PLO4 PLO5 PLO8 PLO9


Name Reg. No Viva / Analysis Modern Ethics Individual
Quiz / Lab of data Tool and and Team
Performa in Lab Usage Safety Work
nce Report

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks

Saad Mohsin 174007

Hasan Ali Ansari 178555

Abdullah Makhdoom 173676


Objectives
The main objective of this lab was to familiarize ourselves with the Mbed OS as
Hardware Abstraction Layer. This lab also focused on the communication protocols as
SPI and I2C.

Observations
There was one main task in this lab. We had to communicate between the Arduino Uno
and STM32F429I Disc. using SPI and I2C protocols. The Arduino Uno was SPI Master
that was sending the its state to STM32F429 and it was acting as I 2C slave.
The state diagram was given and with the help of it we were able to code the logic.

CODE:
#include "mbed.h"
SPISlave spi_slave(PA_7, PA_6, PA_5, PA_4); // mosi, miso, sclk, ssel
char mood[10];

I2C i2c(PB_11, PB_10);


char c1[2]= {'C','U'};
char c2[2] = {'C','D'};
char c3[2] = {'R',1};
char c4[2] = {'R',2};
char c5[2] = {'R',3};
char c6[2] = {'R',4};
const int addr8bit = 0x08 << 1;
DigitalIn button_c(PC_0); // Configure Pc_1 pin as input Count Up cOUNT dOWN
DigitalIn button_r12(PC_1); // Configure Pc_2 pin as input STATE
DigitalIn button_r34(PC_2); // Configure Pc_3 pin as input

int main() {
spi_slave.reply(0x00); //first reply
while(1){
if(button_c.read()==1)
{
i2c.write(addr8bit, c1, 2);
}
if(button_c.read()==0)
{
i2c.write(addr8bit, c2, 2);
}
if(button_r12.read()==0)
{
i2c.write(addr8bit, c3, 2);
}
if(button_r12.read()==1)
{
i2c.write(addr8bit, c4, 2);
}
if(button_r34.read()==1)
{
i2c.write(addr8bit, c5, 2);
}
if(button_r34.read()==0)
{
i2c.write(addr8bit, c6, 2);
}

wait(1);
if(spi_slave.receive()) {
char v = spi_slave.read(); // Read byte from master
printf("First character read %c",v);
if (v=='E'){
int len = spi_slave.read();
int i = 0;
while(i<len){
mood[i] = spi_slave.read();
i++;
}
printf("Arduino is feeling with len %d ",len);
for (int i=0;i<len;i++)
printf("%c",mood[i]);
printf(" right now.\r\n");
}
wait(0.5);
}

}
Outputs
Shocked State

Bored state
Happy state

Sad state
Count up and down

Conclusion
The results were in accordance with the theory. The lab helped us understand
the communications protocols as well as their implementation.

You might also like