Sie sind auf Seite 1von 80

EMBEDDED SYSTEMS DESIGN

LAB REPORT

JUNE 11, 2019


MARIYAM AFSHAAN GHANI
17BEC1090
LED BLINKING USING DIGITALOUT FUNCTION

Date: 12-07-19
Exp. No. : 1 Page No. : 1

Aim:

 Give the introduction about Mbed Online Compiler and installation steps
 Write an mbed c code using DigitalOut function

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 as shown in Fig. 1.

Fig. 1 : LPC11U24 Pin details


Specifications of LPC11U24:
 NXP LPC11U24 MCU
o Low power ARM® Cortex™-M0 Core
o 48MHz, 8KB RAM, 32KB FLASH
o USB Device, 2xSPI, I2C , UART, 6xADC, GPIO
 Prototyping form-factor
o 40-pin 0.1" pitch DIP package, 54x26mm
o 5V USB, 4.5-9V supply or 2.4-3.3V battery
o Built-in USB drag 'n' drop FLASH programmer
 mbed.org Developer Website
o Lightweight Online Compiler
o High level C/C++ SDK
o Cookbook of published libraries and projects

Task-1: On-board LED Blinking


Syntax used for digital output:
DigitalOut (PinName pin)

For LED blinking we’ve used:


DigitalOut variable(LEDn); where n= 1,2,3,4

For delay:
wait(t) ; where ‘t’ is in seconds

Code for LED Blinking:


#include "mbed.h"
DigitalOut myled(LED1);
int main() {
while(1) {
myled = 1;
wait(0.2);
myled = 0;
wait(0.2);
}
}
Output:

a. LED off b. LED ON


Result: LED Blinking using mbed software was successfully executed and verified.

LED BLINKING USING BUSOUT FUNCTION

Date: 19-07-19
Exp. No. : 2 Page No. : 1

Aim:

 Give the introduction about Mbed Online Compiler and installation steps
 Write an mbed c code for the following task using DigitalOut function
 Task-1: Lit two-two onboard LEDs at a time
 Task-2: Display hexadecimal counting pattern from 0-15 by blinking on the
board LEDs
 Task-3: Read the multiple switch values and display status of the switches in
multiple LEDs. If the first two are on then display 0x55 and if the last two are on
display 0x AA
 Task-4: Perform half adder

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 as shown in Fig. 1.

Fig. 1 : LPC11U24 Pin details


Specifications of LPC11U24:
 NXP LPC11U24 MCU
o Low power ARM® Cortex™-M0 Core
o 48MHz, 8KB RAM, 32KB FLASH
o USB Device, 2xSPI, I2C , UART, 6xADC, GPIO
 Prototyping form-factor
o 40-pin 0.1" pitch DIP package, 54x26mm
o 5V USB, 4.5-9V supply or 2.4-3.3V battery
o Built-in USB drag 'n' drop FLASH programmer
 mbed.org Developer Website
o Lightweight Online Compiler
o High level C/C++ SDK
o Cookbook of published libraries and projects

Task-1: On-board LED Blinking

API Used:

Syntax used for digital output:


DigitalOut (PinName pin)
BusOut(pin name pin)

Syntax used for digital input:


DigitalIn (PinName pin)
BusIn (pin name pin)

For delay:
wait(t) ; where ‘t’ is in seconds

1. Lit two-two onboard LEDs at a time


#include "mbed.h"
BusOut myleds(LED1, LED2, LED3, LED4);

int main() {
while(1) {
myleds=3;
wait(0.4);
myleds=0;
wait(0.4);
}}
Output:
a. LED off b. LED on

2. Display hexadecimal counting pattern from 0-15 by blinking on the board LEDs

#include "mbed.h"
BusOut myleds(LED1, LED2, LED3, LED4);

int main() {
while(1) {
for(int i=0; i<16; i++) {
myleds = i;
wait(0.5);
}
}
}
Output:

0 1 2
3 4 5

6 7 8

9 10 11
12 13 14

15

3. Read the multiple switch values and display status of the switches in multiple LEDs.
If the first two are on then display 0x55 and if the last two are on display 0x AA
#include "mbed.h"
BusOut myled(LED1,LED2,LED3,LED4,p33,p34,p35,p36);
DigitalIn S1(p15); DigitalIn S2(p16); DigitalIn S3(p17); DigitalIn S4(p18); int main() {
while(1)
{

if(S1 ==1 && S2==1)


myled=0x55;
else if(S3==1 && S4==1) myled=0xAA;
else
myled=0x00;
}
}
OUTPUT:
first 2 switches last 2 switches on

4. Perform half adder


#include "mbed.h"
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalIn S1(p15);
DigitalIn S3(p17);

int main() { while(1)


{

myled1=S1^S3; myled2=S1&S3;
}
}
Output:
00 01 10 11

Result: LED Blinking using mbed compiler was successfully executed and verified.
EXPERIMENT -3

Date:26/07/19 Exp. No. : 3

TASK:
1)A Serial program to display a message to the pc.
2)To display the corresponding character you type in Serial Window.
3)To monitor the status of switch connected to pin p5 & to display “Hello” and blink LED1
when the pin p5 is high & to display “Sorry” and switch off LED1 when the pin p5 is low.
4)To use ‘Y’ & ‘N’ keys from PC to make LEDs on the Mbed board to display A & 5
respectively.
Algorithm

1) Import “mbed.h” header file for pre-defined functions.


a) Create an instance of Serial with USBTX & USBRX pins.
b) Initiate an infinite while loop.
c) Print “Hello” in the Serial window with a delay of 1sec.
2)
a) Import “mbed.h” header file for pre-defined functions.
b) Create an instance of Serial with USBTX & USBRX pins.
c) Initiate an infinite while loop.
d) Give the pre-defined putc and getc commands.
e) Open the Tera term Serial window and connect it to the mbed port.
f) Serial window displays the input given by the user.
3)

a) Import “mbed.h” header file for pre-defined functions.


b) Create an instance of Serial with USBTX & USBRX pins.
c) Create an instance of DigitalIn with p(p5).
d) Create an instance of BusOut with myled (LED1).
e) Initiate an infinite while loop.
f) If p is high, print “Hello” and blink LED1 & print “Sorry” and switch off the LED1 when p
is low.
4)If input given is ‘Y’, display a hexadecimal value A with the onboard LEDs; else if the input
given is ‘N’, display a hexadecimal value 5 with onboard LEDs; else switch off the LEDs.
CODES:
1)
#include "mbed.h"
Serial pc(USBTX,USBRX);
int main() {
pc.printf("hello world");
}

2)

#include "mbed.h"

DigitalOut myleds(LED1);

Serial pc(USBTX,USBRX);

int main()

while(1)

char s=pc.getc();

wait(.5);

pc.putc(s);

OUTPUT:
3)
#include "mbed.h"
DigitalOut myled(LED1);
DigitalIn sw(p5);
Serial pc(USBTX,USBRX);
int main() {
while(1) {
if (sw == 1)
{
pc.printf("Hello");
myled = 1;
}
else
{
pc.printf("Sorry");
myled = 0;
}
}
OUTPUT:
4)

#include "mbed.h"
BusOut myleds(LED1,LED2,LED3,LED4);
Serial pc(USBTX,USBRX);

int main()
{
while(1)
{
char s= pc.getc();
wait(.5);
if (s=='Y' || s=='y')
myleds=0xA;
else if(s=='N' || s=='n')
myleds=0x5;
else
myleds=0;

}
}
OUTPUT:
RESULT: Use of Serial API to display different output messages for the inputs
given by the user following the appropriate conditions mentioned.
EXPERIMENT-4
Aim:
 Write an mbed c code for the following task using AnalogIn function
 Task-1: Attach a potentiometer o/p to pin 20 and display varying input values on
tera-term
 Task-2: Using 4 onboard LEDs, display potentiometer levels for 0.2 intervals
 Task-3: Using LM35,display temperature on computer

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 as shown in Fig. 1.

Fig. 1 : LPC11U24 Pin details

Specifications of LPC11U24:
 NXP LPC11U24 MCU
o Low power ARM® Cortex™-M0 Core
o 48MHz, 8KB RAM, 32KB FLASH
o USB Device, 2xSPI, I2C , UART, 6xADC, GPIO
 Prototyping form-factor
o 40-pin 0.1" pitch DIP package, 54x26mm
o 5V USB, 4.5-9V supply or 2.4-3.3V battery
o Built-in USB drag 'n' drop FLASH programmer
 mbed.org Developer Website
o Lightweight Online Compiler
o High level C/C++ SDK
o Cookbook of published libraries and projects

Task-1: Attach a potentiometer o/p to pin 20 and display varying input values on tera-term

API Used:

Syntax used for AnalogIn:


AnalogIn ain(PinName pin)

For delay:
wait(t) ; where ‘t’ is in seconds

TASK1:
CODE:

#include "mbed.h"

Serial pc(USBTX,USBRX);

AnalogIn A(p20);

float adcdata;

int main()

pc.printf("adc data val:\n\r");

while(1)

adcdata=A;

pc.printf("%f\n\r",adcdata);

wait(0.5);

}}
OUTPUT:

Task-2: Using 4 onboard LEDs, display potentiometer levels for 0.2 intervals
CODE:
#include "mbed.h"

Serial pc(USBTX,USBRX);

BusOut MYLED(LED1,LED2,LED3,LED4);

AnalogIn A(p20);

float adcdata;

int main()

pc.printf("adc data val:\n\r");


while(1)

adcdata=A;

pc.printf("%f\n\r",adcdata);

wait(0.5);

if (adcdata<=0.2)

MYLED=0;

else if(adcdata>0.2 && adcdata<=0.4)

MYLED=8;

else if(adcdata>0.4 && adcdata<=0.6)

MYLED=12;

else if(adcdata>0.6 && adcdata<=0.8)

MYLED=14;

else if(adcdata>0.8 && adcdata<=1.0)

MYLED=15;

else

MYLED=0;

}}

OUTPUT:
Task-3: Using LM35,display temperature on computer
Code:

#include "mbed.h"

Serial pc(USBTX,USBRX);

AnalogIn A(p20);

float adcdata;

float Temp;

int main()

while(1)

adcdata=A;
Temp=adcdata*330;

if(Temp>=55&&Temp<=150)

pc.printf("The temp %f\n\r",Temp);

else

pc.printf("The temp is exceeding : %f\n\r",Temp);

wait(0.5);

}}

OUTPUT:

RESULT:
EXPERIMENT-5

Aim:

 Write an mbed c code for the following task using AnalogIn function
 Task-1: Create a PWM signal which will generate a 100Hz pulse with a 50% duty cycle.
 Task-2: Change the duty cycle to 0.2 and check display
 Task-3: Controlling LED brightness with PWM.
 Task-4: Use a PWM signal to increase(u) and decrease(d) brightness of the onboard LED
use the Tera term to display brightness value.
 Task-5: Piezobuzzer o/p of PWM Set 50% duty cycle and freq=500Hz. Create sound,
change frequency and notice the change in the sound.

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 as shown in Fig. 1.

Fig. 1 : LPC11U24 Pin details

Specifications of LPC11U24:

 NXP LPC11U24 MCU


o Low power ARM® Cortex™-M0 Core
o 48MHz, 8KB RAM, 32KB FLASH
o USB Device, 2xSPI, I2C , UART, 6xADC, GPIO
 Prototyping form-factor
o 40-pin 0.1" pitch DIP package, 54x26mm
o 5V USB, 4.5-9V supply or 2.4-3.3V battery
o Built-in USB drag 'n' drop FLASH programmer
 mbed.org Developer Website
o Lightweight Online Compiler
o High level C/C++ SDK
o Cookbook of published libraries and projects

API Used:

Syntax used for AnalogIn:


AnalogIn ain(PinName pin)

For delay:
wait(t) ; where ‘t’ is in seconds

TASK1:
CODE:

#include “mbed.h”;

PwmOut led(p5);

int main(){

led.period(0.10f);

led.write(0.50f); //50%duty cycle

while(1);//led flashing

OUTPUT:

TASK-2:

#include "mbed.h"

PwmOut led(p5);

int main(){

led.period(0.10f);

led.write(0.20f); //20%duty cycle

while(1);//led flashing

OUTPUT:
TASK-3:

// host terminal LED dimmer control

#include "mbed.h"

PwmOut led(p5);

float brightness=0.0;

int main() {

while(1){

for(int i=0; i<1000; i++){

wait(0.01);

brightness += 0.001;

led = brightness;

for(int i=0; i<1000; i++){

wait(0.01);

brightness -= 0.001;

led = brightness;

OUTPUT:

TASK-4:
// host terminal LED dimmer control

#include "mbed.h"

Serial pc(USBTX, USBRX); // tx, rx

PwmOut led(p5);

float brightness=0.0;

int main() {

pc.printf("Control of LED dimmer by host terminal\n\r");

pc.printf("Press 'u' = brighter, 'd' = dimmer\n\r");

while(1) {

char c = pc.getc();

wait(0.001);

if((c == 'u') && (brightness < 0.1)) {

brightness += 0.001;

led = brightness; }

if((c == 'd') && (brightness > 0.0)) {

brightness -= 0.001;

led = brightness;

} pc.printf("%c %1.3f \n \r",c,brightness);

TASK-5:

#include "mbed.h"
PwmOut buzzer(p20);
float frequency[]={659,554,659,554,550,494,554,587,494,659,554,440};
//frequency array
float beat[]={1,1,1,1,1,0.5,0.5,1,1,1,1,2};
//beat array
int main() {
while (1) {
for (int i=0; i<=11; i++) {
buzzer.period(1/(frequency[i])); // set PWM period
buzzer=0.5; // set duty cycle
wait(0.5*beat[i]); // hold for beat period
}
}
}

RESULT:
Reg No: 17BEC1090 Mariyam Afshaan Ghani
EXPERIMENT -6

Controlling servo motor position with PWM

Date:16/08/19
Exp.No-. 6 Page No. : 1

AIM: Controlling servo motor position with PWM

SOFTWARE: Mbed OS Online Compiler

APPARATUS REQUIRED:

Bread board

NXP LPC11U24 microcontroller.

Servo motor

• A servo is a small rotary position control device, used for example in radio-controlled cars
and airplanes to position controllers such as steering, elevators and rudders

• The servo shaft can be positioned to specific angular positions by sending the servo a
PWM signal

• As long as the modulated signal exists on the input line, the servo will maintain the
angular position of the shaft
• As the modulated signal changes, the angular position of the shaft changes 13

Task 1:
Controlling servo motor position with PWM
#include "mbed.h"
PwmOut ser(p21);
float per=1.25;
int main(){
while(1){
ser.period(0.020f);
for(int i=0;i<100;i++){
int j=0.025;
ser.write(++j);
wait(1);
}
}
}

Task 2:
Light-tracking devices are very important for the capture of solar energy. Often they operate in
three dimensions, and tilt a solar panel so that it is facing the sun as accurately as possible. To start
rather more simply, create a two-dimensional light tracker by fitting two LDRs, angled away from
each other by around 90, to a servo. Connect the LDRs using the circuit of Figure in the next slide
to two ADC inputs. Write a program that reads the light value sensed by the two LDRs and rotates
the servo so that each is receiving equal light. The servo can only rotate 180. This is not, however,
unreasonable, as a sun-tracking system will be located to track the sun from sunrise to sunset, i.e.
not more than 180.
#include "mbed.h"
PwmOut servo(p24);
AnalogIn pot(p15);
int main() {
servo.period_ms(20);
int pw=1.5;
float ain=pot*3.3;
servo.pulsewidth_ms(pw);
while(1) {
if(ain<1.6) {
servo.pulsewidth_ms(pw++);
wait_ms(0.02);
}
else if(ain>=1.6) {
servo.pulsewidth_ms(pw--);
wait(0.02);
}
}
}
Task 3:

• Using the BusOut object, create a program to produce a “Knightrider” sweep effect with
the on-board LEDs.
• Use shift operators, << and >>.S
#include "mbed.h"
BusOut myleds(LED4, LED3, LED2, LED1);
char x=1;
int main()
{ while(1)
{ for(int i=0; i<3;i++){
x=x<<1;
myleds=x;
wait(0.2);
}
for(int i=0; i<3;i++){
x=x>>1;
myleds=x;
wait(0.2);
}}}
Result: Controlling servo motor with pwm experimented and verified.
Verification:
Reg No: 17BEC1090 Mariyam Afshaan Ghani
EXPERIMENT -7

HC-05
Date: 30/08/19 Exp.No- 7

AIM: Transmit any character or string from Smartphone via Bluetooth to the ARM
MBED

SOFTWARE: mbed OS Online Compiler

APPARATUS REQUIRED:

Bread board

NXP LPC11U24 microcontroller.

HC 05

Fig. 1 : HC05

Pin Pin Name Description


Number
1 Enable / Key This pin is used to toggle between Data Mode (set low) and AT command
mode (set high). By default it is in Data mode

2 Vcc Powers the module. Connect to +5V Supply voltage

3 Ground Ground pin of module, connect to system ground.

4 TX – Transmits Serial Data. Everything received via Bluetooth will be given out
Transmitter by this pin as serial data.

5 RX – Receiver Receive Serial Data. Every serial data given to this pin will be broadcasted
via Bluetooth

6 State The state pin is connected to on board LED, it can be used as a feedback
to check if Bluetooth is working properly.

7 LED Indicates the status of Module

 Blink once in 2 sec: Module has entered Command Mode


 Repeated Blinking: Waiting for connection in Data Mode
 Blink twice in 1 sec: Connection successful in Data Mode

8 Button Used to control the Key/Enable pin to toggle between Data and command
Mode

Task 1

Transmit any character or string from Smartphone via Bluetooth to the ARM MBED Download
and install a Bluetooth terminal application on your phone and use it to connect to the HC-05
Bluetooth module.

-Data is sent from the Smartphone using the Bluetooth terminal application.

Code;

#include "mbed.h"
Serial pc (USBTX,USBRX);

Serial bt(p9,p10);

int main(){

char ch;

pc.baud(9600);

bt.baud(9600);

pc.printf("Hello World!\n\r");

while(1){

if(bt.readable())

{ch=bt.getc();

pc.printf("%c",ch);}

else if(pc.readable()){

ch=pc.getc(); bt.printf("%c",ch);

Tera Term;

Android Application:
Task 2:

Using Bluetooth control LED on mbed

Code:
#include "mbed.h"

Serial pc(USBTX,USBRX);

Serial bt(p9,p10);

DigitalOut myled(LED1);

int main() {

char ch;

myled = 0;

pc.baud(9600);

bt.baud(9600);

while(1) {
if(bt.readable())

ch = bt.getc();

pc.printf("%c",ch);

myled = !(myled);

wait_ms(10);

3. #include "mbed.h"

Serial pc(USBTX,USBRX);

Serial bt(p9,p10);

AnalogIn lm(p20);

int main() {

float ain;

ain = lm;

pc.baud(9600);

bt.baud(9600);

while(1) {

if(bt.readable())

ain = lm.read()*3.3*100;

bt.printf("Temperature: %f\n\r",ain);

pc.printf("Temperature: %f\n\r",ain);

wait(5);
}

Result: Transmitting character or string and LED Control from Smartphone via Bluetooth
to the ARM MBED observed and verified.
Reg No: 17BEC1090 Mariyam Afshaan Ghani
EXPERIMENT -8
Date: 06/09/19

AIM: Implement Interrupts and timers:

Tasks:

1. With a help of the timer measure the time taken to write a message
to the screen, and displays the time taken as a message.
2. Create a square wave(400ms) output using scheduled programming
and verify the timing accuracy with an oscilloscope.
3. When the interrupt is activated, by this rising edge, the ISR executes,
and LED1 is toggled. This can occur at any time in program execution
execution. The program program has effectively effectively one time
triggered task, the switching of LED4, and one event-triggered task,
the switching of LED1.
4. Use the mbed InterruptIn library to toggle an LED whenever a digital
input goes high, implementing a debounce counter to avoid multiple
interrupts.

SOFTWARE: mbed OS Online Compiler

APPARATUS REQUIRED:

Bread board

NXP LPC11U24 microcontroller.

Oscilloscope, wires(for interrupts).

Task 1

Code:

#include "mbed.h"

Serial pc(USBTX,USBRX);

Timer t;
int main() {

while(1){

t.start();

pc.printf("Hello World!\n");

t.stop();

pc.printf("The time taken was %f seconds\n", t.read());

wait(5);

}}

Task 2:

Code:
#include "mbed.h"

Timer T1;

PwmOut out(p5);

int main()

{ T1.start();

while(1)

{ if(T1.read_ms()>=200)

{ out=!out;

T1.reset(); } } }
3. #include "mbed.h"

Timer T1;

DigitalOut out1(LED1);

DigitalOut out2(LED4);

InterruptIn in(p18);

void toggle()

{ out1=!out1; }

int main()

{ in.rise(&toggle);

T1.start();

while(1)

{ if(T1.read_ms()>=200)

{ out2=!out2;

T1.reset(); } } }
4.

#include "mbed.h"

InterruptIn button(p18);

DigitalOut led1(p5);

Timer debounce;

void toggle()

{ if(debounce.read_ms()>200)

{ led1=!led1; debounce.reset();

} } int main()

{ debounce.start();

while(1)

{ button.rise(&toggle); } }
Result:
Reg No: 17BEC1090 Mariyam Afshaan Ghani

EXPERIMENT -9

Serial Peripheral Interface

Date: 17/09/19

Task-1

Set up the Mbed board as Master, and exchange 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); // mosi, miso, sclk, ssel

DigitalOut led1(LED1);

DigitalOut led2(LED2);

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

Display the typed text into the MASTER terminal application on


to the SLAVE terminal application.

Code:

MASTER:

#include "mbed.h"

SPI ser_port(p11,p12,p13);

DigitalOut cs (p14);

Serial pc(USBTX,USBRX);

char recv;

char y;

int main() {

pc.printf(" word: ");

while(1) {

y=pc.getc();
cs=0;

pc.putc(y);

recv=ser_port.write(y);

cs=1;

wait(0.001);

}
SLAVE:

#include "mbed.h"

SPISlave ser_port(p11, p12, p13, p14); // mosi, miso, sclk, ssel

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:
VERIFICATION:
Reg No: 17BEC1090 Mariyam Afshaan Ghani

13/9/19

ECE4003: Embedded System Design

LAB-10: Inter IC Bus Communication

Task-1

Interface two Mbed boards to have communication on I2C with one as master
and another as slave. Receive data from serial monitor and transmit thru master
device and receive the same and display on serial monitor of slave I2C.

Code:

MASTER:

#include "mbed.h"

Serial pc(USBTX,USBRX);

I2C i2c(p28,p27);

DigitalOut myled(LED1);
int main() {

int address = 0xA0;

char data[20];

pc.printf("Enter the data to be sent:");

while(1){

pc.scanf("%s",&data);

pc.printf("%s\t",data);
int len = strlen(data);

i2c.write(address,data,len);

wait(10);

SLAVE:

#include <mbed.h>

Serial pc(USBTX, USBRX);

I2CSlave slave(p28, p27);

int main()

char buf[20];

char msg[] = "Slave!";

slave.address(0xA0);
while (1)

int i= slave.receive();

switch (i)

case I2CSlave::ReadAddressed:slave.write(msg, strlen(msg) + 1); // Includes null

char break;

case I2CSlave::WriteGeneral:slave.read(buf,

20); pc.printf("Read : %s\n", buf);


break;

case I2CSlave::WriteAddressed:

slave.read(buf, 20);

pc.printf("Read : %s\n", buf);

break;}

for(int i= 0; i< 20; i++)

buf[i] = 0;

OUTPUT:
Task-2

I2C Master, transfers switch state to second Mbed acting as slave,


and displays state of slave’s switches on its LEDs.
Code:

MASTER:

#include "mbed.h"

#include<string.h>

Serial pc(USBTX,USBRX);

I2C i2c_port(p28,p27);

DigitalOut myled(LED1);

DigitalIn switch_ip1(p7);

DigitalIn switch_ip2(p6);
char switch_word;

int main(){

const int addr = 0x52;

while(1){

switch_word=0xa0;

if(switch_ip1==1){

switch_word=switch_word|0x01;

if(switch_ip2==1){

switch_word=switch_word|0x02;

i2c_port.start(); //force a start condition

i2c_port.write(addr); //send the address

i2c_port.write(switch_word); //send one byte of data, ie

switch_word i2c_port.stop(); //force a stop condition wait(0.002);


}

SLAVE:

#include <mbed.h> I2CSlave

slave(p28, p27); DigitalOut

red_led(LED1); DigitalOut //Configure I2C Slave

green_led(LED2);
//red led

//green led
DigitalIn switch_ip1(LED3);

DigitalIn switch_ip2(LED4);

char switch_word ; //word we will send

char recd_val;

int main()

{slave.address(0x52);

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;

slave.write(switch_word); //load up word to send//test for I2C, and act accordingly

int i = slave.receive();

if (i == 3){ //Slave is addressed, Master will write


recd_val= slave.read();//now set leds according to received word

red_led=0; //preset both to 0

green_led=0;

recd_val=recd_val&0x03; //AND out unwanted bits

if (recd_val==1)

red_led=1;

if (recd_val==2)

green_led=1;

if (recd_val==3)
{red_led=1;green_led=1;

OUTPUT:
Verifictaion:

6
9
VIRTUAL LAB
TIMERS
Date:11-10-2019
Exp.No. 12
Page No. : 1
Aim:
• Give the introduction about Mbed Online Compiler and installation steps
• Write an mbed c code for the following task using DigitalOut,timer functions.
• Task1: Apply the Timer to run one function at one rate and another function at
another rate. Create four Timers at different rates. When the time value is
exceeded, a function is called,whichflipsthe associatedmbed LED.
• Task2: Find the maximum value that the Timer can reach. Tests Timer
duration and display the current time values to terminal.

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 as shown in Fig. 1.

Fig. 1 : LPC11U24 Pin details

Specificationsof LPC11U24:

• NXP LPC11U24 MCU o Low power ARM® Cortex™-M0 Core o 48MHz, 8KB RAM,
32KB FLASH

o USB Device, 2xSPI, I2C , UART, 6xADC, GPIO 7


0
• Prototyping form-factor

o 40-pin 0.1" pitch DIP package, 54x26mm o 5V USB, 4.5-9V


supply or 2.4-3.3V battery o Built-in USB drag 'n' drop FLASH
programmer

• mbed.org Developer Website o Lightweight Online


Compiler oHigh level C/C++ SDK

o Cookbook of published libraries and projects

Task-1: On-board LED Blinking at different time intervals

API Used:

Syntax used for digital output:

DigitalOut (PinName pin)

For LED blinking we’ve used:

DigitalOutvariable(LEDn); where n= 1,2,3,4

For starting timer: timername.start();

For resetting timer:

timername.reset();

For reading the time delay:

timername.read();
Code for timing task and LED Blinking :

#include "mbed.h"

Timer timer1;
7
1
Timer timer2;

Timer timer3;

Timer timer4;

DigitalOut ledA(LED1);

DigitalOut ledB(LED2);

DigitalOut ledC(LED3);

DigitalOut ledD(LED4);

void task1(void);

void task2(void); void

task3(void);

void task4(void);

7
2
int main() {

timer1.start();

timer2.start();

timer3.start();

timer4.start(); while

(1){ if

(timer1.read()>0.2){

task1();

timer1.reset();

if (timer2.read()>0.5){

task2();

timer2.reset();

if (timer3.read()>1){

task3();

timer3.reset();

if (timer4.read()>2){

7
3
task4();

timer4.reset();

void task1(void){

ledA = !ledA;

void task2(void){

ledB = !ledB;

void task3(void){

7
4
ledC = !ledC;

void task4(void){

ledD = !ledD;

Output:

Task-2: On-board LED Blinking at different time intervals

API Used:
7
5
Syntax used for digital output:

DigitalOut (PinName pin)

For LED blinking we’ve used:

DigitalOutvariable(LEDn); where n= 1,2,3,4

For displaying on serial window:

Serial pc(USBTX,USBRX)

For starting timer: timername.start();

For resetting timer:

timername.reset();

For reading the time delay:

timername.read();
4

7
6
Code for displaying timer value on serial window

#include "mbed.h" Timer

t;

float s=0;

float m=0;

DigitalOut diag (LED1);

Serial pc(USBTX, USBRX); int

main()

pc.printf("\r\nTimer Duration Test\n\r"); pc.printf("-------------------

\n\n\r");

t.reset();

t.start();

while(1){ if

(t.read()>=(s+1))

{ diag =

1;

wait (0.05);

diag = 0;

7
7
s++ ;

pc.printf("%1.0f seconds\r\n",(s-60*(m-1)));

if (t.read()>=60*m)

printf("%1.0f minutes \n\r",m);

m++ ;

if (t.read()<s)

7
8
{

pc.printf("\r\nTimer has overflowed!\n\r");

for(;;){}

Output:

Result: The timers tasks are executed and verified successfully.

79

Das könnte Ihnen auch gefallen