Mbed lab

Mbed Embedded System Design Experiment 1 LED Blinking

Aim: To perform the Following Using mbed LPC11U24 • To Glow an Internal LED • To Glow an External LED connected to an IO pin • To Glow the Internal LED’s in a sequential Order • Using Switch to Change the Order of Sequential Glowing Of Internal LED’s

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.

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 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

Program Code Code 1: Blink an LED

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

Code 2: Blink 4 LEDS

  1. include "mbed.h" DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4);

int main() { while(1) { myled1= 1; wait(0.2); myled1 = 0; myled2 = 1; wait(0.2); myled2= 0; myled3 = 1; wait(0.2); myled3= 0; myled4 = 1; wait(0.2); myled4= 0; wait(0.2); } } Output:

Code 3: External LEDS Blinking

  1. include "mbed.h" DigitalOut myled(p23); int main() { while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); } } Code 4: Switching
  2. include "mbed.h" DigitalOut myled(LED1); DigitalIn Switch(p23); int main() { while(1) { if(Switch==1) myled = 1; else myled=0; } } Output:

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

Experiment 2

Ashutosh Kumar 16BEC1245 Aim: 1)Program mbed board to lit two-two onboard LEDs, at a time. Using BusOut function 2)Display hexadecimal counting pattern from 0 to 15 by blinking the on board LED’s. Using BusOut function 3)Read the multiple switch values and display the status of the switches in multiple LEDs. If the first two switches are on then display 0*55 and if the second two switches are on then display 0*AA. (Use 4 switches and 8 LEDs) 4)connect a 7-segment display to the m-bed microcontroller and write a code to display a continuous count from 0-9. 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.

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 digital output: BUSOut (PinName,pin)

For multiple LED blinking we’ve used: BusOut variable(LEDn); where n= 1,2,3,4…….

For reading the status of the switches: BusIn variable(pn);where n=1,2,3,4…..

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

Code for Digital port access: 1)

  1. include "mbed.h" BusOut leds(LED1,LED2); int main() { while(1) { leds= 3; wait(0.2); leds= 0; wait(0.2); } }

Output:

2)

  1. include "mbed.h" BusOut leds(LED1,LED2,LED3,LED4); int main() { while(1) { int i; for (i=1;i<16;i++) { leds=i; wait(0.2); } } }

Output:

3)

  1. include "mbed.h" BusIn s(p17,p18,p19,p20); BusOut l(p21,p22,p23,p24,p25,p26,p27,p28); int main() { while(1) { if(s==12) l=0x55; else if(s==3) leds=0xAA; } }

Output:

4)

  1. include "mbed.h" BusOut leds(LED1,LED2,LED3,LED4,p23,p24,p25); int main() { while(1) { leds=0x7E; wait(0.5); leds=0x06; wait(0.5); leds=0x4d; wait(0.5); leds=0x79; wait(0.5); leds=0x33; wait(0.5); leds=0x5B; wait(0.5); leds=0x5F; wait(0.5); leds=0x70; wait(0.5); leds=0x7F; wait(0.5); leds=0x7B; wait(0.5); } } Output:

Verification Signature:

Result: Digital port access using mbed software was successfully executed and verified.

Experiment 3 Serial Communication Ashutosh Kumar 16BEC1245 Aim: To perform the Following Using mbed LPC11U24 1.Write a serial program to dispay a message to pc. 2. Write a program to dispay the corresponding character you type in serial window. 3.Monitor the status of a switch connected to pin5, if high write “hello” to the pc and blink led1 otherwise write “sorry” to the pc and led1 is off. 4. Write a serial program to use the ‘Y’ and ‘N’ keys from pc to make leds on the mbed board to display ‘A’ and ‘5’. 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.

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:

• Serial.pc(USBTX,USBRX) used for serial communication using Tx and Rx pins • Serial.device(p9,p10) p9 is Tx pin and p10 is Rx pin • putc() put character • getc() get character • printf() prints message

Code 1: Write a serial program to display a message to the pc

  1. include "mbed.h" Serial pc(USBTX, USBRX); int main() { pc.printf("Serial Comm"); while(1) { pc.putc(pc.getc()); } }

Code 2: Write a program to display the corresponding character you type in serial window

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

int main() { while(1) { pc.putc(pc.getc()); } }

Code 3: #include "mbed.h" Serial pc(USBTX, USBRX); DigitalIn myswitch(p5); DigitalOut myled(LED1); int main() { if(myswitch) {

pc.printf("Hello"); while(myswitch) { myled = 1; wait(0.2); myled = 0; wait(0.2); } } else { myled=0; pc.printf("Sorry"); } }

Code 4: #include "mbed.h" Serial pc(USBTX, USBRX); BusOut myled(LED1, LED2, LED3, LED4); int main() { while(1){ if(pc.getc()=='Y') {

myled = 0x0A;

} else if(pc.getc()=='N') {

myled = 0x05;

} else { myled=0; } } }

Experiment 4

AIM: To perform the Following Using mbed LPC11U24 • Attach a potentiometer output to mbed pin 20 to continuously display the analog input value when used with a host PC terminal application • Using the 4 onboard LEDs write a program that will use a potentiometer input on pin 20 to continuously control how many LEDs are on. • Design, build and program a simple embedded system using an LM35 sensor, which displays temperature on the computer screen. This device has an output of 10mV/C with operating temperature -55C to 150C.

SOFTWARE USED: Arm Mbed OS developer site 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.

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 serial input: USBSerial serial Serial pc(USBTX, USBRX)

Syntax used for Bus output: BusOut PinName (pin)

Syntax used for Bus input: BusIn switch/variableName (pin)

For LED blinking we’ve used: BusOut variable(LEDn); where n= 1,2,3,4 For getting character we’ve used: pc.getc()

For putting character we’ve used: pc.putc()

For delay: wait(t) ; where „t‟ is in seconds

For printing character we’ve used: printf()

For input/output we’ve used: DigitalIn variable(pin no.); DigitalOut variable(pin/led no.); For if-else loop we’ve used: if (“condition”) {} else if (“condition”) {}

else {}

For float data type we’ve used: float a;

CODES:

1) To attach a potentiometer output to mbed pin 20 to continuously display the analog input value when used with a host PC terminal application.

CODE:

  1. include "mbed.h" AnalogIn A(p20); Serial pc(USBTX,USBRX); int main() { while(A) { float Ain=3.3*A; pc.printf("%f\t",Ain); }

2)Using the 4 onboard LEDs write a program that will use a potentiometer input on pin 20 to continuously control how many LEDs are on. Use the following chart to define the LED control. X<=0.2 0 0 0 0 0.2<x<=0.4 1 0 0 0 0.4<x<=0.6 1 1 0 0 0.6<x<=0.8 1 1 1 0 0.8<x<=1.0 1 1 1 1

CODE:

  1. include "mbed.h" AnalogIn A(p20); BusOut leds(LED1,LED2,LED3,LED4); Serial pc(USBTX,USBRX); int main() { while(1) { if(A<=0.2) { leds = 0;

} else if(A>0.2 && A<=0.4) { leds = 0x8; } else if(A>0.4 && A<=0.6) { leds = 0xC; } else if(A>0.6 && A<=0.8) { leds = 0xE; } else if(A>0.8 && A<=1) { leds = 0xF; } float Ain=A; pc.printf("%f\t",Ain); } }

3)Design, build and program a simple embedded system using an LM35 sensor, which displays temperature on the computer screen. This device has an output of 10mV/C with operating temperature -55C to 150C.

CODE:

  1. include "mbed.h" AnalogIn A(p20); Serial pc(USBTX,USBRX); int main() { while(1) { float Ain = (A/1024)*5; float Aout = Ain*205*100*3.3; wait(1); pc.printf("%f\t",Aout); } }

Experiment 5 AIM :

Write a Mbed C Program for PWM generation

1. •Create a PWM signal which will generate a 100 Hz pulse with 50% duty cycle.

2. •Change the duty cycle to some different values, say 0.2 (20%) and 0.8 (80%) and check the correct display.

3. •Controlling LED brightness with PWM

4. •This exercises uses a pulse width modulation signal to increase and decrease the brightness of the onboard LED The program requires the use of a host terminal application to communicate the brightness value to the mbed, in this example by using the ‘u’ and ‘d’ keys.

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.

API Used:

Syntax used for digital output:

PwmOut (PinName pin)

For writing:

Led.write();

Led.read();

Led.period();

Code:

1) Create a PWM signal which will generate a 100 Hz pulse with 50% duty cycle.

  1. include"mbed.h" PwmOut led(p5); int main() { specify period first led.period(.10f); led.write(0.50f); 50% duty cycle, relative to period while(1); }

2) Change the duty cycle to some different values, say 0.2 (20%) and 0.8 (80%) and check the correct display.

  1. include"mbed.h" PwmOut led(p5); int main() { specify period first led.period(.10f); led.write(0.70f); 50% duty cycle, relative to period while(1); }

PC monitor

  1. include"mbed.h" Serial pc(USBTX,USBRX); AnalogIn ain (p20); PwmOut led(p5); int main() { specify period first led.period(.10f); led.write(0.70f); 50% duty cycle, relative to period while(1) { float value=ain; pc.printf("%f ",value);

wait(1); } }

3) Controlling LED brightness with PWM

  1. include "mbed.h" PwmOut PWM1(p6); AnalogIn Ain(p20); defines analog input on Pin 20 int main() { while(1){

PWM1.period(0.010); set PWM period to 10 ms PWM1=Ain; wait(0.1); } }

4) This exercises uses a pulse width modulation signal to increase and decrease the brightness of the onboard LED The program requires the use of a host terminal application to communicate the brightness value to the mbed, in this example by using the ‘u’ and ‘d’ keys.

  1. include "mbed.h" PwmOut PWM1(p6); Serial pc(USBTX,USBRX); PwmOut led(p5); AnalogIn Ain(p20); defines analog input on Pin 20 int main() { float x=0.5; led.period(.10f); led.write(x); while(1)

{

char y=pc.getc(); if (y=='u') { x=x+0.1; if(x>=1) x=1; led.write(x); } if(y=='d') { x-=0.1; if(x<=0) x=0; led.write(x);

} } }

Result: Hence, Program for PWM generation has been verified successfully using mbed software.

Experiment 6 Aim:

i) Program to control servo position with PWM

ii) Write a program that reads the light value sensed by the two LDR and rotates the servo so that each is receiving equal light.

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.

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 digital output: AnalogIn (PinName pin)

Syntax used for bus output: PWM (PinNames pins)

For Duty Cycle: led.write(dutycycle) ; where dutycyle = 0.05f for 50% duty cycle

1. Control servo position with

PWM Code:

  1. include"mbed.h" PwmOut pwm1 (p21);

int main() { pwm1.period_ms(20); while(1) { pwm1.pulsewidth_ms(1.75); wait(1); } }

2. Write a program that reads the light value sensed by the two LDR and rotates the servo so that each is receiving equal light.

  1. include"mbed.h" PwmOut pwm1 (p21); AnalogIn LDR(p16)

int main() { pwm1.period_ms(20); float ldr1=0,ldr; while(1) { ldr=LDR; if(ldr>ldr1) pwm1.pulsewidth_ms(1.75); else { pwm1.pulsewidth_ms(1.25); return(0); } ldr1=ldr; wait(1); } }

Result: A program that reads the light value sensed by the two LDR and rotates the servo so that each is receiving equal light.

Experiment 7 AIM To write C++ code to understand the following: 1. Write a code with a help if the timer to measure the time taken to write a message on the screen, and display the time taken as the message. 2. Create a square ware (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 the program execution. The program has effectively one time triggered task, the switching of the LED4 and one event triggered task, the switching of LEDs. 4. Use the mbed interrupt In library to toggle an LED whenever a digital input goes high, implementing a debounce counter to avoid multiple interrupts.

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.

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

SOFTWARE USED Arm Mbed OS developer site API Used:

Syntax used for digital input: Timer (PinName pin)

Syntax used for interrupt: InterruptIn (PinName pin)

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

For LED: Led.write(); Led.read(); Led.period();

CODE 1. #include "mbed.h" Timer T1; int main() { T1.start(); printf("Hello world\n"); T1.stop(); printf("\nThe time taken is: %.2f in secs\n",T1.read());

2. #include"mbed.h” Timer T1; DigitalOut 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); } }

The final hardware implementation was verified after dumping the code on the mbed and the observations were recorded.

Experiment 8 Aim: i) Communicate with Smart phone via Bluetooth module. ii) Transfer room temperature value wirelessly using Bluetooth module to the smartphone.

API Used:

Syntax used for digital output: AnalogIn (PinName pin)

Syntax used for USB bus output: Serial PC (USBTX,USBRX)

Syntax used for Bluetooth bus output: Serial PC (TX pin,RX pin)

1. Write a C++ code with mbed APIs to communicate with Smart phone via Bluetooth module. By transferring ASCII values between them. Values received by mbed module will be displayed in PC Tera term and value received by smartphone displayed on the app screen.

Code1:

  1. include "mbed.h" Serial pc(USBTX, USBRX); Serial blue(p9, p10); TX, RX

DigitalOut myled(LED1); DigitalOut myled4(LED4); int main() { blue.baud(9600); pc.baud(9600); pc.printf("Bluetooth Start\r\n"); char ch=0; echo back characters and toggle the LED while (1) { if (blue.readable()) { ch=blue.getc(); pc.printf("%c",ch); }

if (pc.readable()) { ch=pc.getc(); blue.printf("%c",ch); } } }

CODE 2:

  1. include "mbed.h" Serial pc(USBTX, USBRX); Serial blue(p9, p10); TX, RX AnalogIn ain(p20);

int main() { blue.baud(9600); pc.baud(9600); pc.printf("Bluetooth Start\r\n"); float v = 1; while (1) { { v=ain*330; pc.printf("%f",v); blue.printf("%f",v); wait(1); } } }

Result: A Bluetooth connection was established between HC05 - Bluetooth module and Smartphole using mbed NXP LPC11U24 Microcontroller.

Experiment 9

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.

MASTER:

  1. include <mbed.h> Serial pc(USBTX, USBRX); I2CSlave slave(p28, p27);

int main() { charbuf[20]; charmsg[] = "Slave!";

slave.address(0xA0); while (1) { inti = 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(inti = 0; i< 20; i++) buf[i] = 0; } }

Slave

  1. include "mbed.h" Serial pc (USBTX,USBRX); I2C i2c(p28, p27);

int main() { int address = 0xA0; char data[20]; pc.printf("enter data to be sent"); pc.scanf("%s",&data); pc.printf("%s",data); int l=strlen(data); i2c.write(address, data, l); wait(10); }

Ex.2: I2C Master, transfers switch state to second mbed acting as slave, and displays state of slave’s switches on its leds.

Master:

  1. include "mbed.h" I2C i2c_port(p27,p28); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalIn switch_ip1(p5); DigitalIn switch_ip2(p6); char switch_word; char recd_val const int addr=0x52; int main() { while(1) { switch_word=0xa0; if (switch_ip1==1) switch_word=switch_word|0x01; if (switch_ip1==2) 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); receive a single byte of data, in correct I2C package i2c_port.start(); i2c_port.write(addr|0x01); send address, with R/W bit set to Read recd_val=i2c_port.read(addr); Read and save the received byte i2c_port.stop(); force a stop condition led1=0;

led2=0; recd_val=recd_val&0x03; if(recd_value==1) led1=1; if (recd_val==2) led2=1; if (recd_val==3) { red_led1=1; green_led2=1; } } }

Ex.3: I2C Slave, when called transfers switch state to mbed acting as Master, and displays state of Master’s switches on its leds.

  1. include "mbed.h" I2C i2c_port(p27,p28); DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalIn switch_ip1(p5); DigitalIn switch_ip2(p6); char switch_word; char recd_val int main() { slave.address(0x52); while(1) { switch_word=0xa0; if (switch_ip1==1) switch_word=switch_word|0x01; if (switch_ip1==2) switch_word=switch_word|0x02; slave.write(switch_word); int i=slave.receive(): if(i==3) recd_val=slave.read(); led1=0; led2=0; recd_val=recd_val&0x03; if(recd_value==1) led1=1; if (recd_val==2) led2=1; if (recd_val==3) { red_led1=1; green_led2=1; } } }

Experiment 10 AIM To write m-bed code to understand the following:

Exercise 1 Write an moduler mbed c++ program display the message “HELLO” on LCD Display Exercise 2 Write a C++ program by using mbed TextLCD library to display a message “Hello World”. Exercise 3 Display a continuous count variable on the LCD display 2nd row and 5th digit.

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.

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

SOFTWARE USED Arm Mbed OS developer site API Used:

Syntax used for digital input: Timer (PinName pin)

Syntax used for interrupt: InterruptIn (PinName pin)

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

For LED: Led.write(); Led.read(); Led.period();

CODE: Ex.1 : Write an moduler mbed c++ program display the message “HELLO” on LCD Display

MAIN.CPP

  1. include “mbed.h”
  2. include “LCD.h" int main() { LCD_init(); display_to_LCD(0x48); ‘H’ display_to_LCD(0x45); ‘E’ display_to_LCD(0x4C); ‘L’ display_to_LCD(0x4C); ‘L’ display_to_LCD(0x4F); ‘O’ } Lcd.h LCD.h file
  • Ifndef LCD_H
  • define LCD _M
  1. Include "mbed.h" void display_to_LCD(char value): void toggle_enable(void); void LCD_init(void);
  2. endif function to display characters on the LCD function to toggle the enable bit function to initialise the LCD

LCD.CPP LCD.cpp file

  1. include <LCD.h>" DigitalOut RS(p19); DigitalOut E(p20); BusOut data(p21, p22, p23, p24); void todgle_enable(void)i E-1; wait (0.001): E=0; walt10.001); } initialise LCD function void LCD iniz(void) { wait(0.02); R3-0; E=0: function mode data=0x2; toggle enable(); data=0x8; toggle enable();}

diaplay mode data=0x0: eoggle_enable(); data=OxF: toggle enable ; clear display data=0x0; toddle_enable(): data-0x1; toggle enable(): } display function void display_to_LCD(char value ) ({R5=1; data=value>>4; toggle_enable(): data -valuefi0x0F; todole_enable(): }

Ex.2: Write a C++ program by using mbed TextLCD library to display a message “Hello World”. CODE:

  1. include "mbed.h"
  2. include "TextLCD.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); rs, e, d4-d7

int main() { lcd.printf("Hello World!\n"); }

EX 3. Display a continuous count variable on the LCD display 2nd row and 5th digit.

  1. include "mbed.h"
  2. include "TextLCD.h" TextLCD lcd(p19, p20, p21, p22, p23, p24); rs,e,d0,d1,d2,d3 int main() { lcd.locate(3,1); For(i=0;i<15;i++){ lcd.locate(5,2); lcd.printf(“counter”); lcd.print(i);} } OUTPUT:

Verification:

Result: All results are obtained and verified successfully.

Experiment 11

AIM To write m-bed code to thread programming.

Exercise 1

The thread class allow defining creating and controlling thread function in the system. Creating and controlling a simple thread. Exercise 2 Create two thread with different priorities & display priorities on the pc. Exercise 3 Write a program to use mutex to protect printf().

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.

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

SOFTWARE USED Arm Mbed OS developer site API Used:

Syntax used for digital input: Timer (PinName pin)

Syntax used for interrupt: InterruptIn (PinName pin)

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

For LED: Led.write(); Led.read(); Led.period();

Ex1: The thread class allow defining creating and controlling thread function in the system. Create and controlling a simple thread.

  1. include "mbed.h"
  2. include "rtos.h"

DigitalOut led1(LED1); DigitalOut led2(LED2); Thread thread;

void led2_thread() { while (true) {

led2 = !led2; Thread::wait(1000); } }

int main() { thread.start(led2_thread);

while (true) { led1 = !led1; Thread::wait(500); } }

Ex2:Create two thread with different priorities & display priorities on the pc.

  1. include "mbed.h"

DigitalOut led1(LED1); DigitalOut led2(LED2); Thread thread;

void led2_thread() { while (true) { led2 = !led2; wait(1); } }

int main() { thread.start(led2_thread);

while (true) { led1 = !led1; wait(0.5); } }

Ex3:Write a program to use muxtex to printf().

  1. include "mbed.h"
  2. include "rtos.h"

Mutex stdio_mutex;

void notify(const char* name, int state) { stdio_mutex.lock();

printf("%s: %d\n\r", name, state); stdio_mutex.unlock(); }

void test_thread(void const *args) { while (true) { notify((const char*)args, 0); Thread::wait(1000); notify((const char*)args, 1); Thread::wait(1000); } }

int main() { Thread t2; Thread t3;

t2.start(callback(test_thread, (void *)"Th 2")); t3.start(callback(test_thread, (void *)"Th 3"));

test_thread((void *)"Th 1"); }

................................................................................................................................................................

AIM

To write C++ code to understand the following: 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. 2. Set 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. 3. Display text typed into the terminal application on to the slave serial terminal. Use the # key to clear the screen of the text you have written.

API Used: Syntax used for digital input: Timer (PinName pin) Syntax used for interrupt: InterruptIn (PinName pin) For delay: wait(t) ; where ‘t’ is in seconds For LED: Led.write(); Led.read(); Led.period();

CODE

1.)

  1. include "mbed.h" Serial pc(USBTX,USBRX); SPI ser_port(p11,p12,p13); DigitalOut led1(LED3); DigitalOut led2(LED4); 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; } } }

2.)

  1. include "mbed.h" SPI spi(p5, p6, p7); mosi, miso, sclk DigitalOut chipSelect(p8); Serial pc(USBTX, USBRX); tx, rx int main() { int valueToSendToSlave = 20; Starting value only, this spi.format(8,3); Setup: bit data, high steady state clock,2nd edge capture spi.frequency(1000000); 1MHz pc.printf("=======\r\n"); pc.printf("Press any key to start...\r\n"); pc.getc(); wait for keyboard int counter = 1; while (1) { pc.printf("%d Value to send = %d ", counter++,valueToSendToMaster); chipSelect = 0; Select device int dataFromMaster = spi.write(valueToSendToMaster); chipSelect = 1; Deselect device pc.printf(" returns %d\r\n", dataFromMaster); valueToSendToMaster++; wait(5); Wait for 5 seconds for readability only } }

3.)

  1. include "mbed.h" SPISlave device(p11, p12, p13, p14); Serial pc(USBTX, USBRX); int main() { while (1) { if (device.receive()) { char v = device.read(); pc.printf("%c",v); if (v =='#') pc.printf("/b"); } } }

.


Please log in to post comments.