Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NOVEMBER 6, 2019
HARSH KULSHRESTHA
17BEC1043
Contents
S. No. Experiment Date
1 LED blinking using DigitalOut API 16/07/2019
2 LED blinking using BusOut function 23/07/2019
3 Serial communication 30/07/2019
4 PwmOut API(I) 06/08/2019
5 PwmOut API(II) 13/08/2019
6 Bluetooth Module 27/08/2019
7 LCD Interfacing 03/09/2019
8 SPI Interfacing 10/09/2019
9 Car Parking Sensor 17/09/2019
10 Pedometer using Accelerometer 08/10/2019
11 RTOS 15/10/2019
Experiment 1
LED BLINKING USING DIGITALOUT FUNCTION
Date: 16.07.19
Exp. No.: 1
17BEC1043
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.
API Used:
For delay:
wait(t) ; where ‘t’ is in seconds
Task - 2
#include "mbed.h"
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
int main() {
while(1) {
myled1 = 1;
wait(0.5);
myled1 = 0;
wait(0.5);
myled2 = 1;
wait(0.5);
myled2 = 0;
wait(0.5);
myled3 = 1;
wait(0.5);
myled3 = 0;
wait(0.5);
myled4 = 1;
wait(0.5);
myled4 = 0;
wait(0.5);
}
}
Task – 3
CODE
#include "mbed.h"
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
int main() {
while(1) {
myled1 = 1;
wait(0.5);
myled2 = 1;
wait(0.5);
myled3 = 1;
wait(0.5);
myled4 = 1;
wait(0.5);
myled4 = 0;
wait(0.5);
myled3= 0;
wait(0.5);
myled2= 0;
wait(0.5);
myled1= 0;
wait(0.5);
}
}
Output:
Fig. 1 LED Blinking
Result: LED Blinking using mbed software was successfully executed and verified.
Experiment 2
LED GLOWING USING BUSOUT FUNCTION
Date: 23.07.19
Exp. No.: 2
17BEC1043
AIM: To use BUSOUT function and glow on board LEDs.
Task 1:
code:
#include "mbed.h"
BusOut myled1(LED1,LED2);
BusOut myled2(LED3,LED4);
int main(){
while(1)
{ myled1=3;
myled2=0;
wait(1);
myled1=0;
myled2=3;
wait(1);
}
}
2.
Code:
#include "mbed.h"
BusOut myled1(LED1,LED2,LED3,LED4);
int i;
int main(){
while(1)
{ for(i=0;i<16;i++)
{ myled1=i;
wait(1);}
}
}
3.
Code:
#include "mbed.h"
BusIn pins(p15,p16,p17,p18);
BusOut myled2(LED1,LED2,LED3,LED4,p21,p22,p23,p24);
int main()
{
while(1)
{
if(pins==12)
{
myled2=0xAA;
}
else if(pins==3)
{
myled2=0x55;
}
}
}
Task 4.
#include "mbed.h"
BusOut leds(p21,p22,p23,p24,p25,p26,p27,p28);
int main()
{
while(1)
{
wait(0.5);
leds=0x3f;
wait(0.5);
leds=0x06;
wait(0.5);
leds=0x5b;
wait(0.5);
leds=0x4f;
wait(0.5);
leds=0x66;
wait(0.5);
leds=0x6d;
wait(0.5);
OUTPUTS:
Fig. 1 Hexadecimal counting pattern
RESULT: The API BUSOUT was used to light onboard and external LEDs and hexadecimal
pattern was displayed.
Experiment 3
Serial Communication
Date: 30.07.19
Exp. No.: 3
17BEC1043
Verification-
Task 1-
Write a c++ code with mbed APIS to print the current voltage value of a potentiometer on PC serial monitor using
ADC and serial power.
Code-
#include "mbed.h"
serial pc(USBTX,USBRX);
AnalogIn ain(p20)
int main()
{
while(1)
{
float value=ain*3.3;
printf("the voltage is %f",value)
wait(1)
}
}
Output-
Task 2-
Write a C++ code to interface an LM35 with mbed board and display the temperature values of
PC serial monitor.
Code-
#include "mbed.h"
Serial pc(USBTX,USBRX);
AnalogIn ain(p20);
int main()
{
while(1)
{
float value=ain*3.3*100;
printf("the temprature is %f",value);
wait(1);
}
}
Circuit-
Output-
Task 3-
Write the C++ code using mbed API to implement the table as shown in the lab.
Code-
#include "mbed.h"
Serial pc(USBTX,USBRX);
AnalogIn Ain(p20);
BusOut myled(LED1,LED2,LED3,LED4);
float ADCdata;
int main(){
pc.printf("ADC Data values...\n\r");
while(1)
{
ADCdata=Ain;
pc.printf("%f\n\r",ADCdata);
wait(0.5);
if(0.00000<ADCdata<=0.20000)
myled=0x00;
else if(0.20000<ADCdata<=0.400000)
myled=0x01;
else if(0.40000<ADCdata<=0.60000)
myled=0x03;
else if(0.60000<ADCdata<=0.800000)
myled=0x07;
else if(0.80000<=ADCdata<=1.00000)
myled=0xf;
}
Outputs-
}
EXPERIMENT 4
PwmOut API
Verification-
Task 1-
Write a C++ code with mbed API to generate a PWM signal with duty cycle 50%. Verify the
ouput using
(a). LED
(b). Digital CRO
Code
#include "mbed.h"
PwmOut PWM1(p21);
int main()
{
PWM1.period(0.010); // set PWM period to 10 ms
Task 2-
Repeat the task 1 with 70% duty cycle.
Code-
#include "mbed.h"
PwmOut PWM1(p21);
int main()
}
Experiment 5
PwmOut Function
Date:13 Aug, 2019
Week No. : 5 Reg No-17BEC1043
Verification-
Aim:
• Give the introduction about Mbed Online Compiler and installation steps
• Write an mbed c code for the following task using PwmOut function
1. Write the c++ code to vary the intensity of the LED using the values of
potentiometer.
2. Generate a PWM signal to increase or decrease the brightness. If ‘i’ is
pressed then increase the brightness, do the opposite if ‘d’ is pressed.
APIs used:
PwmOut,DigitalOut,AnalogIn
Code 1:
#include "mbed.h"
DigitalOut dout(p22);
AnalogIn ain(p20);
int main() {
while(1) {
dout=ain;
wait(1);
Bright LED-
Dim LED-
Code 2:
#include "mbed.h"
PwmOut led(p20);
float brightness=0.0;
int main() {
while(1) {
char c = pc.getc();
wait(0.001);
led = brightness;
brightness -= 0.001;
led = brightness;
}
Experiment 6
BLUETOOTH MODULE
Date: 27.08.19
17BEC1043
Verification-
TASK 1:
Bluetooth modem used:Hc05
#include "mbed.h"
Serial pc(USBTX,USBRX);
Serial bt(p9,p10);
int main()
{
pc.baud(9600);
bt.baud(9600);
pc.printf("Hello World\n\r");
wait(100);
}
TASK 2:
#include "mbed.h"
Serial pc(USBTX,USBRX);
Serial bt(p9,p10);
int main()
{
char ch;
pc.baud(9600);
bt.baud(9600);
while(1)
{
if(bt.readable())
{
ch=bt.getc();
pc.printf("%c",ch);
}
else if(pc.readable())
{
ch=pc.getc();
bt.printf("%c",ch);}
}}
TASk3:
#include "mbed.h"
Serial pc(USBTX,USBRX);
Serial bt(p9,p10);
BusOut myled(LED1,LED2);
int main()
{
char ch;
pc.baud(9600);
bt.baud(9600);
pc.printf("Hello World!\n\r");
while(1)
{
if(bt.readable())
{
ch=bt.getc();
if(ch=='1')
{
myled=01;
wait(0.2);
}
if(ch=='0'){
myled=10;
wait(0.2);
}
}
else if(pc.readable())
{
ch=pc.getc();
if(ch=='1')
{
myled=01;
wait(0.2);
}
if(ch=='0'){
myled=10;
wait(0.2);
}
}
}
}
––
TASK 4:
#include "mbed.h"
Serial pc(USBTX,USBRX);
AnalogIn a(p20);
Serial bt(p9, p10);
int main() {
char ch;
pc.baud(9600);
bt.baud(9600);
pc.printf("Hello World!\n\r");
bt.printf("Hello World!\r\n");
while(1) {
float value=a*(3.3)*100;
pc.printf("the temperature value is %f\n",value);
bt.printf("the temperature value is %f\n",value);
wait(1);
}
}
OUTPUTS:
RESULT:
Aim:
Task2 - To print the average temperate values using LM35 and print it on an LCD.
General Information-
LCD 16x2
• LCDs (Liquid Crystal Displays) are used in embedded system applications for
displaying various parameters and status of the system.
• LCD 16x2 is a 16-pin device that has 2 rows that can accommodate 16 characters each.
• LCD 16x2 can be used in 4-bit mode or 8-bit mode.
• It is also possible to create custom characters.
• It has 8 data lines and 3 control lines that can be used for control purposes.
•
• Task-1: Printing “Hello world”
• API Used:
TextLCD(),
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p5, p6, p7, p8, p9, p10);//RS, RW, D4, D5, D6, D7
int main() {
lcd.printf(“Hello World”);
• API Used:
TextLCD(), AnalogIn
wait(t) ; where ‘t’ is in seconds
Code :
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p5, p6, p7, p8, p9, p10);//RS, RW, D4, D5, D6, D7
AnalogIn ain(p11);
int i;
float temp,mean;
int main() {
while(1)
int count=0;
float sum=0;
for(i=0;i<100;i++)
temp=ain;
sum+=temp;
count=count+1;
wait(0.1);
mean=sum/count;
lcd.printf("%f",mean);
}
LCD showing temperature
EXPERIMENT 8
SPI Interfacing
Verification-
TASK 1:
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.
Interfacing between MASTER and SLAVE:
Master code:
#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_wrd |= 0x01;
if(switch_ip2 == 1)
switch_word = switch_wrd |= 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 code:
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14);
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 text typed into the MASTER terminal application on to the slave serial terminal.
Code:
Master Mbed Code:
#include "mbed.h"
SPI ser_port(p11,p12,p13);
Serial pc(USBTX,USBRX);
DigitalOut cs(p14);
char recd_val;
char ch;
int main()
{
ch = 0xa0;
while(1)
{
if(pc.readable())
pc.printf(“%c”,ch);
cs = 0;
recd_val = ser_port.write(ch);
cs = 1;
wait(0.01);
recd_val = recd_val & 0x03;
}
}
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14);
Serial pc(USBTX,USBRX);
char recd_val;
char ch;
int main()
{
ch = 0xa0;
while(1)
{
if(ser_port.receive())
{
recd_val = ser_port.read();
ser_port.reply(ch);
}
pc.printf(“%
c”,ch);
recd_val =
recd_val &
0x03;
}
}
Output:
Characters typed
in the Master
and Slave Serials
appears on each
others
Terminals.
Inference:
All the tasks have
been executed
and verified.
WEEK-9
CAR PARKING SENSOR
Verification-
Code:
#include "mbed.h"
DigitalOut trigger(p6);
DigitalOut myled(LED1); //monitor trigger
DigitalOut myled2(LED2); //monitor echo
DigitalIn echo(p7);
DigitalOut buzzer(p20);
Serial pc(USBTX,USBRX);
int distance = 0;
int correction = 0;
Timer sonar;
int main()
{
sonar.reset();
// measure actual software polling timer delays
// delay used later in time correction
// start timer
sonar.start();
// min software polling delay to read echo pin
while (echo==2) {};
myled2 = 0;
// stop timer
sonar.stop();
// read timer
correction = sonar.read_us();
printf("Approximate software overhead timer delay is %d uS\n\r",correction);
Output:
Output:
WEEK-10
Pedometer
Verification:
Code:
SPI acc(p11,p12,p13);
DigitalOut cs(p14);
Serial pc(USBTX,USBRX);
char buffer[6];
int16_t data[3];
float x,y,z,temp,temp1;
int Vref=5;
int a=0,b;
int main(){
cs=1;
acc.format(8,3);
acc.frequency(2000000);
cs=0;
acc.write(0x31);
acc.write(0x0B);
cs=1;
cs=0;
acc.write(0x2D);
acc.write(0x08);
cs=1;
while(1)
{
y=0;
wait(0.2);
cs=0;
acc.write(0x80|0x40|0x32);
for(int i=0;i<6;i++){
buffer[i]=acc.write(0x00);
}
cs=1;
temp=y;
temp1=x;
int Vref=5;
data[0]=buffer[1]<<8|buffer[0];
data[1]=buffer[3]<<8|buffer[2];
data[2]=buffer[5]<<8|buffer[4];
x=0.004*data[0];
y=0.004*data[1];
z=0.04*data[2];
if((y-temp)!=0 && (x-temp1)!=0)
{
a+=1;
Inference:
We have designed a pedometer.
Date: 18/10/2019
Experiment 11
RTOS
Verification:
Aim:
1. To toggle 2 LEDs using an RTOS thread.
2. To toggle 4 LEDs using an RTOD thread.
Code:
Task 1:
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Task 2:
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
}
void led3_thread(void const *args)
{
while(1)
{
led3=!led3;
Thread::wait(1500);
}
}
void led4_thread(void const *args)
{
while(1)
{
led4=!led4;
Thread::wait(2000);
}
int main() {
Thread thread2(led2_thread);
Thread thread3(led3_thread);
Thread thread4(led4_thread);
while(1)
{
led1=!led1;
Thread::wait(500);
}
}
Result:
Hence, we have performed the required
experiment.