Pushyanth Damarapati / Mbed 2 deprecated mbed

Dependencies:   mbed

Committer:
Pushyanth_D
Date:
Sun Oct 28 19:24:14 2018 +0000
Revision:
0:888f200c7271
LAB EXAM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pushyanth_D 0:888f200c7271 1 BLUETOOTH HC 05
Pushyanth_D 0:888f200c7271 2
Pushyanth_D 0:888f200c7271 3
Pushyanth_D 0:888f200c7271 4 #include "mbed.h"
Pushyanth_D 0:888f200c7271 5 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 6 Serial blue(p9, p10);
Pushyanth_D 0:888f200c7271 7 DigitalOut myled(LED1);
Pushyanth_D 0:888f200c7271 8 int main()
Pushyanth_D 0:888f200c7271 9 {
Pushyanth_D 0:888f200c7271 10 blue.baud(115200);
Pushyanth_D 0:888f200c7271 11 pc.baud(115200);
Pushyanth_D 0:888f200c7271 12 pc.printf("Bluetooth Start\r\n");
Pushyanth_D 0:888f200c7271 13 // echo back characters and toggle the LED
Pushyanth_D 0:888f200c7271 14 while (1)
Pushyanth_D 0:888f200c7271 15 {
Pushyanth_D 0:888f200c7271 16 if (blue.readable())
Pushyanth_D 0:888f200c7271 17 {
Pushyanth_D 0:888f200c7271 18 pc.putc(blue.getc());
Pushyanth_D 0:888f200c7271 19 myled = !myled;
Pushyanth_D 0:888f200c7271 20 }
Pushyanth_D 0:888f200c7271 21 }
Pushyanth_D 0:888f200c7271 22 }
Pushyanth_D 0:888f200c7271 23
Pushyanth_D 0:888f200c7271 24
Pushyanth_D 0:888f200c7271 25
Pushyanth_D 0:888f200c7271 26
Pushyanth_D 0:888f200c7271 27 #include "mbed.h"
Pushyanth_D 0:888f200c7271 28 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 29 Serial blue(p9, p10);
Pushyanth_D 0:888f200c7271 30 AnalogIn temp(p5);
Pushyanth_D 0:888f200c7271 31 int main()
Pushyanth_D 0:888f200c7271 32 {
Pushyanth_D 0:888f200c7271 33 blue.baud(9600);
Pushyanth_D 0:888f200c7271 34 pc.baud(9600);
Pushyanth_D 0:888f200c7271 35 while (1)
Pushyanth_D 0:888f200c7271 36 { float t=temp*3.3*100;
Pushyanth_D 0:888f200c7271 37 if (pc.readable())
Pushyanth_D 0:888f200c7271 38 { blue.printf("The temperature is ");
Pushyanth_D 0:888f200c7271 39 blue.putc(t);
Pushyanth_D 0:888f200c7271 40 }
Pushyanth_D 0:888f200c7271 41 }
Pushyanth_D 0:888f200c7271 42 }
Pushyanth_D 0:888f200c7271 43
Pushyanth_D 0:888f200c7271 44 ===================================
Pushyanth_D 0:888f200c7271 45 BUSOUT FUNCTION
Pushyanth_D 0:888f200c7271 46
Pushyanth_D 0:888f200c7271 47 /// To write C++ Source Code to understand the digital port access
Pushyanth_D 0:888f200c7271 48 /Program mbed Board to light two-two LED’s at a time using BusOut function.
Pushyanth_D 0:888f200c7271 49 //Display hexadecimal Coding pattern from 0 to 15 by blinking the on-board LED’s using BusOut function.
Pushyanth_D 0:888f200c7271 50 //Read the Multiple Switches values and display the status of switches in Multiple LED’s .If the first two switches are on display 0x55 and if second two switches are on then display 0xAA. Use 4 switches, 8 LED’s. Connect a 7 Segment Display to the mbed Board and write a code to display count from 0 to 9
Pushyanth_D 0:888f200c7271 51 #include "mbed.h"
Pushyanth_D 0:888f200c7271 52 BusOut leds1(LED1,LED2);
Pushyanth_D 0:888f200c7271 53 BusOut leds2(LED3,LED4);
Pushyanth_D 0:888f200c7271 54 int main() {
Pushyanth_D 0:888f200c7271 55 while(1) {
Pushyanth_D 0:888f200c7271 56 leds1 = 0xFF;
Pushyanth_D 0:888f200c7271 57 wait(0.2);
Pushyanth_D 0:888f200c7271 58 leds1 = 0;
Pushyanth_D 0:888f200c7271 59 wait(0.2);
Pushyanth_D 0:888f200c7271 60 leds2 = 0xFF;
Pushyanth_D 0:888f200c7271 61 wait(0.2);
Pushyanth_D 0:888f200c7271 62 leds2 = 0;
Pushyanth_D 0:888f200c7271 63 wait(0.2)
Pushyanth_D 0:888f200c7271 64 }
Pushyanth_D 0:888f200c7271 65 }
Pushyanth_D 0:888f200c7271 66
Pushyanth_D 0:888f200c7271 67
Pushyanth_D 0:888f200c7271 68
Pushyanth_D 0:888f200c7271 69
Pushyanth_D 0:888f200c7271 70 #include "mbed.h"
Pushyanth_D 0:888f200c7271 71 BusOut leds(LED1,LED2,LED3,LED4);
Pushyanth_D 0:888f200c7271 72 int main() {
Pushyanth_D 0:888f200c7271 73 while(1) {
Pushyanth_D 0:888f200c7271 74 int i;
Pushyanth_D 0:888f200c7271 75 for (i=0;i<15;i++)
Pushyanth_D 0:888f200c7271 76 {
Pushyanth_D 0:888f200c7271 77 leds=i;
Pushyanth_D 0:888f200c7271 78 wait(0.5);
Pushyanth_D 0:888f200c7271 79 }
Pushyanth_D 0:888f200c7271 80 }
Pushyanth_D 0:888f200c7271 81 }
Pushyanth_D 0:888f200c7271 82
Pushyanth_D 0:888f200c7271 83
Pushyanth_D 0:888f200c7271 84
Pushyanth_D 0:888f200c7271 85
Pushyanth_D 0:888f200c7271 86 #include "mbed.h"
Pushyanth_D 0:888f200c7271 87 BusIn switches1(p22,p23);
Pushyanth_D 0:888f200c7271 88 BusIn switches2(p24,p25);
Pushyanth_D 0:888f200c7271 89 BusOut leds(LED1,LED2,LED3,LED4);
Pushyanth_D 0:888f200c7271 90 int main() {
Pushyanth_D 0:888f200c7271 91 while(1) {
Pushyanth_D 0:888f200c7271 92 if(switches1==0x03)
Pushyanth_D 0:888f200c7271 93 leds=0x55;
Pushyanth_D 0:888f200c7271 94 else if(switches2==0x03)
Pushyanth_D 0:888f200c7271 95 leds=0xAA;
Pushyanth_D 0:888f200c7271 96 }
Pushyanth_D 0:888f200c7271 97 }
Pushyanth_D 0:888f200c7271 98
Pushyanth_D 0:888f200c7271 99
Pushyanth_D 0:888f200c7271 100
Pushyanth_D 0:888f200c7271 101 #include "mbed.h"
Pushyanth_D 0:888f200c7271 102 BusOut leds(LED1,LED2,LED3,LED4,p23,p24,p25);
Pushyanth_D 0:888f200c7271 103 int main() {
Pushyanth_D 0:888f200c7271 104 while(1) {
Pushyanth_D 0:888f200c7271 105 leds=0x7E;
Pushyanth_D 0:888f200c7271 106 wait(0.5);
Pushyanth_D 0:888f200c7271 107 leds=0x06;
Pushyanth_D 0:888f200c7271 108 wait(0.5);
Pushyanth_D 0:888f200c7271 109 leds=0x4d;
Pushyanth_D 0:888f200c7271 110 wait(0.5);
Pushyanth_D 0:888f200c7271 111 leds=0x79;
Pushyanth_D 0:888f200c7271 112 wait(0.5);
Pushyanth_D 0:888f200c7271 113 leds=0x33;
Pushyanth_D 0:888f200c7271 114 wait(0.5);
Pushyanth_D 0:888f200c7271 115 leds=0x5B;
Pushyanth_D 0:888f200c7271 116 wait(0.5);
Pushyanth_D 0:888f200c7271 117 leds=0x5F;
Pushyanth_D 0:888f200c7271 118 wait(0.5);
Pushyanth_D 0:888f200c7271 119 leds=0x70;
Pushyanth_D 0:888f200c7271 120 wait(0.5);
Pushyanth_D 0:888f200c7271 121 leds=0x7F;
Pushyanth_D 0:888f200c7271 122 wait(0.5);
Pushyanth_D 0:888f200c7271 123 leds=0x7B;
Pushyanth_D 0:888f200c7271 124 wait(0.5);
Pushyanth_D 0:888f200c7271 125 }
Pushyanth_D 0:888f200c7271 126 }
Pushyanth_D 0:888f200c7271 127 =================================================
Pushyanth_D 0:888f200c7271 128
Pushyanth_D 0:888f200c7271 129 SERVO MOTOR
Pushyanth_D 0:888f200c7271 130
Pushyanth_D 0:888f200c7271 131 ///To control a server motor using PWM.
Pushyanth_D 0:888f200c7271 132 #include "mbed.h"
Pushyanth_D 0:888f200c7271 133 PwmOut ser(p6);
Pushyanth_D 0:888f200c7271 134 AnalogIn pot(p5);
Pushyanth_D 0:888f200c7271 135 int main()
Pushyanth_D 0:888f200c7271 136 {
Pushyanth_D 0:888f200c7271 137 float i;
Pushyanth_D 0:888f200c7271 138 ser.period(20);
Pushyanth_D 0:888f200c7271 139 while(1)
Pushyanth_D 0:888f200c7271 140 {
Pushyanth_D 0:888f200c7271 141 if(pot==0)
Pushyanth_D 0:888f200c7271 142 {
Pushyanth_D 0:888f200c7271 143 for(i=0;i<1;i=i+0.25)
Pushyanth_D 0:888f200c7271 144 ser.pulsewidth_ms(1.25*i);
Pushyanth_D 0:888f200c7271 145 }
Pushyanth_D 0:888f200c7271 146 else
Pushyanth_D 0:888f200c7271 147 {
Pushyanth_D 0:888f200c7271 148 for(i=1;i>0;i=i-0.25)
Pushyanth_D 0:888f200c7271 149 ser.pulsewidth_ms(1.25*i);
Pushyanth_D 0:888f200c7271 150 }
Pushyanth_D 0:888f200c7271 151
Pushyanth_D 0:888f200c7271 152 }
Pushyanth_D 0:888f200c7271 153
Pushyanth_D 0:888f200c7271 154
Pushyanth_D 0:888f200c7271 155
Pushyanth_D 0:888f200c7271 156 // Light tracking devices are very important for the capture of solar energy.
Pushyanth_D 0:888f200c7271 157 //Often, they operate in 3 dimensions, and tilt a solar panel so that it is facing the sun as accurately as possible.
Pushyanth_D 0:888f200c7271 158 // To start rather more simply, create a 2D light tracker by fitting 2 LDRs, angled array from each other by 90 to 180 by a servo motor
Pushyanth_D 0:888f200c7271 159 //connected to the LDRs using the circuit in the figure to two ADC inputs.
Pushyanth_D 0:888f200c7271 160 //Write a program that reads the light value sensed by the 2 LDRs and rotates the servo motor such that each is receiving equal light. The servo can only rotate 180o. This is not, however, unrenewable, as a sun tracking system will be located to track the sun from sunrise to sunset, i.e, not more than 180o
Pushyanth_D 0:888f200c7271 161
Pushyanth_D 0:888f200c7271 162 #include "mbed.h"
Pushyanth_D 0:888f200c7271 163 AnalogIn ldr1(p7);
Pushyanth_D 0:888f200c7271 164 AnalogIn ldr2(p6);
Pushyanth_D 0:888f200c7271 165 PwmOut servo(p20);
Pushyanth_D 0:888f200c7271 166
Pushyanth_D 0:888f200c7271 167 int main() {
Pushyanth_D 0:888f200c7271 168
Pushyanth_D 0:888f200c7271 169 servo.period_ms(20);
Pushyanth_D 0:888f200c7271 170 float i = 1.25;
Pushyanth_D 0:888f200c7271 171 while(1) {
Pushyanth_D 0:888f200c7271 172 //servo.pulsewidth(1.25);
Pushyanth_D 0:888f200c7271 173 while(ldr1!=ldr2) {
Pushyanth_D 0:888f200c7271 174 if(ldr1<ldr2)
Pushyanth_D 0:888f200c7271 175 servo.pulsewidth(i+0.25);
Pushyanth_D 0:888f200c7271 176 else
Pushyanth_D 0:888f200c7271 177 servo.pulsewidth(i-0.25);
Pushyanth_D 0:888f200c7271 178
Pushyanth_D 0:888f200c7271 179 }
Pushyanth_D 0:888f200c7271 180 }
Pushyanth_D 0:888f200c7271 181 }
Pushyanth_D 0:888f200c7271 182 ===================================================================
Pushyanth_D 0:888f200c7271 183 I2C
Pushyanth_D 0:888f200c7271 184 MASTER AND SLAVE
Pushyanth_D 0:888f200c7271 185
Pushyanth_D 0:888f200c7271 186 //Interface two mbed boards to have communication on I2C with one as master and another as slave.
Pushyanth_D 0:888f200c7271 187 //Receive data from serial monitor and transmit thru master device and receive the same and display on serial monitor of slave I2C.
Pushyanth_D 0:888f200c7271 188
Pushyanth_D 0:888f200c7271 189 MASTER:
Pushyanth_D 0:888f200c7271 190 //Master
Pushyanth_D 0:888f200c7271 191 #include <mbed.h>
Pushyanth_D 0:888f200c7271 192 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 193 I2CSlave slave(p28, p27);
Pushyanth_D 0:888f200c7271 194 int main()
Pushyanth_D 0:888f200c7271 195 {
Pushyanth_D 0:888f200c7271 196 char buf[20];
Pushyanth_D 0:888f200c7271 197 char msg[] = "Slave!";
Pushyanth_D 0:888f200c7271 198 slave.address(0xA0);
Pushyanth_D 0:888f200c7271 199 while (1) {
Pushyanth_D 0:888f200c7271 200 int i = slave.receive();
Pushyanth_D 0:888f200c7271 201 switch (i) {
Pushyanth_D 0:888f200c7271 202 case I2CSlave::ReadAddressed:
Pushyanth_D 0:888f200c7271 203 slave.write(msg, strlen(msg) + 1); // Includes null char
Pushyanth_D 0:888f200c7271 204 break;
Pushyanth_D 0:888f200c7271 205 case I2CSlave::WriteGeneral:
Pushyanth_D 0:888f200c7271 206 slave.read(buf, 20);
Pushyanth_D 0:888f200c7271 207 pc.printf("Read : %s\n", buf);
Pushyanth_D 0:888f200c7271 208 break;
Pushyanth_D 0:888f200c7271 209 case I2CSlave::WriteAddressed:
Pushyanth_D 0:888f200c7271 210 slave.read(buf, 20);
Pushyanth_D 0:888f200c7271 211 pc.printf("Read : %s\n", buf);
Pushyanth_D 0:888f200c7271 212 break;
Pushyanth_D 0:888f200c7271 213 }
Pushyanth_D 0:888f200c7271 214 for(int i = 0; i< 20; i++)
Pushyanth_D 0:888f200c7271 215 buf[i] = 0;
Pushyanth_D 0:888f200c7271 216 }
Pushyanth_D 0:888f200c7271 217
Pushyanth_D 0:888f200c7271 218
Pushyanth_D 0:888f200c7271 219
Pushyanth_D 0:888f200c7271 220 //Slave
Pushyanth_D 0:888f200c7271 221 #include "mbed.h"
Pushyanth_D 0:888f200c7271 222 Serial pc (USBTX,USBRX);
Pushyanth_D 0:888f200c7271 223 I2C i2c(p28, p27);
Pushyanth_D 0:888f200c7271 224
Pushyanth_D 0:888f200c7271 225 int main()
Pushyanth_D 0:888f200c7271 226 {
Pushyanth_D 0:888f200c7271 227 int address = 0xA0;
Pushyanth_D 0:888f200c7271 228 char data[20];
Pushyanth_D 0:888f200c7271 229 pc.printf("enter data to be sent");
Pushyanth_D 0:888f200c7271 230 pc.scanf("%s",&data);
Pushyanth_D 0:888f200c7271 231 pc.printf("%s",data);
Pushyanth_D 0:888f200c7271 232 int l=strlen(data);
Pushyanth_D 0:888f200c7271 233 i2c.write(address, data, l);
Pushyanth_D 0:888f200c7271 234 wait(10);
Pushyanth_D 0:888f200c7271 235 }
Pushyanth_D 0:888f200c7271 236
Pushyanth_D 0:888f200c7271 237
Pushyanth_D 0:888f200c7271 238
Pushyanth_D 0:888f200c7271 239
Pushyanth_D 0:888f200c7271 240 ///I2C Master, transfers switch state to second mbed acting as slave, and displays state of slave’s switches on its leds.
Pushyanth_D 0:888f200c7271 241 Master:
Pushyanth_D 0:888f200c7271 242 #include "mbed.h"
Pushyanth_D 0:888f200c7271 243 I2C i2c_port(p27,p28);
Pushyanth_D 0:888f200c7271 244 DigitalOut led1(LED1);
Pushyanth_D 0:888f200c7271 245 DigitalOut led2(LED2);
Pushyanth_D 0:888f200c7271 246 DigitalIn switch_ip1(p5);
Pushyanth_D 0:888f200c7271 247 DigitalIn switch_ip2(p6);
Pushyanth_D 0:888f200c7271 248 char switch_word;
Pushyanth_D 0:888f200c7271 249 char recd_val
Pushyanth_D 0:888f200c7271 250 const int addr=0x52;
Pushyanth_D 0:888f200c7271 251 int main()
Pushyanth_D 0:888f200c7271 252 {
Pushyanth_D 0:888f200c7271 253 while(1) {
Pushyanth_D 0:888f200c7271 254 switch_word=0xa0;
Pushyanth_D 0:888f200c7271 255 if (switch_ip1==1)
Pushyanth_D 0:888f200c7271 256 switch_word=switch_word|0x01;
Pushyanth_D 0:888f200c7271 257 if (switch_ip1==2)
Pushyanth_D 0:888f200c7271 258 switch_word=switch_word|0x02;
Pushyanth_D 0:888f200c7271 259 i2c_port.start(); //force a start condition
Pushyanth_D 0:888f200c7271 260 i2c_port.write(addr); //send the address
Pushyanth_D 0:888f200c7271 261 i2c_port.write(switch_word); //send one byte of data, ie switch_word
Pushyanth_D 0:888f200c7271 262 i2c_port.stop(); //force a stop condition
Pushyanth_D 0:888f200c7271 263 wait(0.002);
Pushyanth_D 0:888f200c7271 264 //receive a single byte of data, in correct I2C package
Pushyanth_D 0:888f200c7271 265 i2c_port.start();
Pushyanth_D 0:888f200c7271 266 i2c_port.write(addr|0x01); //send address, with R/W bit set to Read
Pushyanth_D 0:888f200c7271 267 recd_val=i2c_port.read(addr); //Read and save the received byte
Pushyanth_D 0:888f200c7271 268 i2c_port.stop(); //force a stop condition
Pushyanth_D 0:888f200c7271 269 led1=0;
Pushyanth_D 0:888f200c7271 270 led2=0;
Pushyanth_D 0:888f200c7271 271 recd_val=recd_val&0x03;
Pushyanth_D 0:888f200c7271 272 if(recd_value==1)
Pushyanth_D 0:888f200c7271 273 led1=1;
Pushyanth_D 0:888f200c7271 274 if (recd_val==2)
Pushyanth_D 0:888f200c7271 275 led2=1;
Pushyanth_D 0:888f200c7271 276 if (recd_val==3) {
Pushyanth_D 0:888f200c7271 277 led1=1;
Pushyanth_D 0:888f200c7271 278 led2=1;
Pushyanth_D 0:888f200c7271 279 }
Pushyanth_D 0:888f200c7271 280 }
Pushyanth_D 0:888f200c7271 281 }
Pushyanth_D 0:888f200c7271 282
Pushyanth_D 0:888f200c7271 283
Pushyanth_D 0:888f200c7271 284
Pushyanth_D 0:888f200c7271 285 //I2C Slave, when called transfers switch state to mbed acting as Master, and displays state of Master’s switches on its leds.
Pushyanth_D 0:888f200c7271 286
Pushyanth_D 0:888f200c7271 287 #include "mbed.h"
Pushyanth_D 0:888f200c7271 288 I2CSlave i2c_port(p27,p28);
Pushyanth_D 0:888f200c7271 289 DigitalOut led1(LED1);
Pushyanth_D 0:888f200c7271 290 DigitalOut led2(LED2);
Pushyanth_D 0:888f200c7271 291 DigitalIn switch_ip1(p5);
Pushyanth_D 0:888f200c7271 292 DigitalIn switch_ip2(p6);
Pushyanth_D 0:888f200c7271 293 char switch_word;
Pushyanth_D 0:888f200c7271 294 char recd_val
Pushyanth_D 0:888f200c7271 295 int main()
Pushyanth_D 0:888f200c7271 296 { slave.address(0x52);
Pushyanth_D 0:888f200c7271 297 while(1) {
Pushyanth_D 0:888f200c7271 298 switch_word=0xa0;
Pushyanth_D 0:888f200c7271 299 if (switch_ip1==1)
Pushyanth_D 0:888f200c7271 300 switch_word=switch_word|0x01;
Pushyanth_D 0:888f200c7271 301 if (switch_ip1==2)
Pushyanth_D 0:888f200c7271 302 switch_word=switch_word|0x02;
Pushyanth_D 0:888f200c7271 303 slave.write(switch_word);
Pushyanth_D 0:888f200c7271 304 int i=slave.receive():
Pushyanth_D 0:888f200c7271 305 if(i==3)
Pushyanth_D 0:888f200c7271 306 recd_val=slave.read();
Pushyanth_D 0:888f200c7271 307 led1=0;
Pushyanth_D 0:888f200c7271 308 led2=0;
Pushyanth_D 0:888f200c7271 309 recd_val=recd_val&0x03;
Pushyanth_D 0:888f200c7271 310 if(recd_value==1)
Pushyanth_D 0:888f200c7271 311 led1=1;
Pushyanth_D 0:888f200c7271 312 if (recd_val==2)
Pushyanth_D 0:888f200c7271 313 led2=1;
Pushyanth_D 0:888f200c7271 314 if (recd_val==3) {
Pushyanth_D 0:888f200c7271 315 led1=1;
Pushyanth_D 0:888f200c7271 316 led2=1;
Pushyanth_D 0:888f200c7271 317 }
Pushyanth_D 0:888f200c7271 318 }
Pushyanth_D 0:888f200c7271 319 }
Pushyanth_D 0:888f200c7271 320 ===============================================================
Pushyanth_D 0:888f200c7271 321 LCD INTERFACING
Pushyanth_D 0:888f200c7271 322
Pushyanth_D 0:888f200c7271 323 // LCD.cpp file
Pushyanth_D 0:888f200c7271 324 #include "LCD.h"
Pushyanth_D 0:888f200c7271 325 DigitalOut RS(p19);
Pushyanth_D 0:888f200c7271 326 DigitalOut E(p20);
Pushyanth_D 0:888f200c7271 327 BusOut data(p21, p22, p23, p24);
Pushyanth_D 0:888f200c7271 328 void toggle_enable(void){
Pushyanth_D 0:888f200c7271 329 E=1;
Pushyanth_D 0:888f200c7271 330 wait(0.001);
Pushyanth_D 0:888f200c7271 331 E=0;
Pushyanth_D 0:888f200c7271 332 wait(0.001);
Pushyanth_D 0:888f200c7271 333 }//initialise LCD function
Pushyanth_D 0:888f200c7271 334 void LCD_init(void)
Pushyanth_D 0:888f200c7271 335 {
Pushyanth_D 0:888f200c7271 336 wait(0.02);
Pushyanth_D 0:888f200c7271 337 RS=0;
Pushyanth_D 0:888f200c7271 338 E=0;//function node
Pushyanth_D 0:888f200c7271 339 data=0x2;
Pushyanth_D 0:888f200c7271 340 toggle_enable(); //display mode
Pushyanth_D 0:888f200c7271 341 data=0x0;
Pushyanth_D 0:888f200c7271 342 toggle_enable();
Pushyanth_D 0:888f200c7271 343 data=0xF;
Pushyanth_D 0:888f200c7271 344 toggle_enable(); //clear display
Pushyanth_D 0:888f200c7271 345 data=0x0;
Pushyanth_D 0:888f200c7271 346 toggle_enable() ;
Pushyanth_D 0:888f200c7271 347 data=0x1;
Pushyanth_D 0:888f200c7271 348 toggle_enable();
Pushyanth_D 0:888f200c7271 349 }
Pushyanth_D 0:888f200c7271 350 void display_to_LCD(char value) //display function
Pushyanth_D 0:888f200c7271 351 {
Pushyanth_D 0:888f200c7271 352 RS=1;
Pushyanth_D 0:888f200c7271 353 data=value>>4;
Pushyanth_D 0:888f200c7271 354 toggle_enable();
Pushyanth_D 0:888f200c7271 355 data=value&0x0F;
Pushyanth_D 0:888f200c7271 356 toggle_enable();
Pushyanth_D 0:888f200c7271 357 }
Pushyanth_D 0:888f200c7271 358
Pushyanth_D 0:888f200c7271 359
Pushyanth_D 0:888f200c7271 360
Pushyanth_D 0:888f200c7271 361 //LCD.h FIle
Pushyanth_D 0:888f200c7271 362 #ifndef LCD_H
Pushyanth_D 0:888f200c7271 363 #define LCD_H
Pushyanth_D 0:888f200c7271 364 #include "mbed.h"
Pushyanth_D 0:888f200c7271 365
Pushyanth_D 0:888f200c7271 366
Pushyanth_D 0:888f200c7271 367 void display_to_LCD(char value);
Pushyanth_D 0:888f200c7271 368 void toggle_enable(void);
Pushyanth_D 0:888f200c7271 369 void LCD_init(void);
Pushyanth_D 0:888f200c7271 370
Pushyanth_D 0:888f200c7271 371 #endif
Pushyanth_D 0:888f200c7271 372
Pushyanth_D 0:888f200c7271 373
Pushyanth_D 0:888f200c7271 374
Pushyanth_D 0:888f200c7271 375
Pushyanth_D 0:888f200c7271 376 #include "mbed.h"
Pushyanth_D 0:888f200c7271 377 #include "LCD.h"
Pushyanth_D 0:888f200c7271 378 //DigitalOut vo(p18);
Pushyanth_D 0:888f200c7271 379
Pushyanth_D 0:888f200c7271 380 int main()
Pushyanth_D 0:888f200c7271 381 {
Pushyanth_D 0:888f200c7271 382 //vo = 0.5;
Pushyanth_D 0:888f200c7271 383 LCD_init();
Pushyanth_D 0:888f200c7271 384 display_to_LCD(0x48); // ‘H’
Pushyanth_D 0:888f200c7271 385 display_to_LCD(0x45); // ‘E’
Pushyanth_D 0:888f200c7271 386 display_to_LCD(0x4C); // ‘L’
Pushyanth_D 0:888f200c7271 387 display_to_LCD(0x4C); // ‘L’
Pushyanth_D 0:888f200c7271 388 display_to_LCD(0x4F); // ‘O’
Pushyanth_D 0:888f200c7271 389 }
Pushyanth_D 0:888f200c7271 390
Pushyanth_D 0:888f200c7271 391
Pushyanth_D 0:888f200c7271 392
Pushyanth_D 0:888f200c7271 393 //Write a C++ program by using TextLCD library to display a message “Hello World”.
Pushyanth_D 0:888f200c7271 394 CODE:
Pushyanth_D 0:888f200c7271 395 #include "mbed.h"
Pushyanth_D 0:888f200c7271 396 #include "TextLCD.h"
Pushyanth_D 0:888f200c7271 397 TextLCD lcd(p19, p20, p21, p22, p23, p24); //rs,e,d0,d1,d2,d3
Pushyanth_D 0:888f200c7271 398 int main()
Pushyanth_D 0:888f200c7271 399 {
Pushyanth_D 0:888f200c7271 400 lcd.printf("Hello World!");
Pushyanth_D 0:888f200c7271 401 }
Pushyanth_D 0:888f200c7271 402
Pushyanth_D 0:888f200c7271 403 //Display a continuous count variable o the LCD display 2nd row 5th digit.
Pushyanth_D 0:888f200c7271 404 CODE:
Pushyanth_D 0:888f200c7271 405 #include "mbed.h"
Pushyanth_D 0:888f200c7271 406 #include "TextLCD.h"
Pushyanth_D 0:888f200c7271 407 TextLCD lcd(p19, p20, p21, p22, p23, p24);
Pushyanth_D 0:888f200c7271 408 //rs,e,d0,d1,d2,d3
Pushyanth_D 0:888f200c7271 409 int t=0;
Pushyanth_D 0:888f200c7271 410 int main()
Pushyanth_D 0:888f200c7271 411 {
Pushyanth_D 0:888f200c7271 412 while(t<=50)
Pushyanth_D 0:888f200c7271 413 { lcd.locate(4,2);
Pushyanth_D 0:888f200c7271 414
Pushyanth_D 0:888f200c7271 415 t=t+1;
Pushyanth_D 0:888f200c7271 416 lcd.printf("%d",t);
Pushyanth_D 0:888f200c7271 417 wait(0.5);
Pushyanth_D 0:888f200c7271 418 }
Pushyanth_D 0:888f200c7271 419 }
Pushyanth_D 0:888f200c7271 420 =======================================================================
Pushyanth_D 0:888f200c7271 421 LED BLINKING EXP1
Pushyanth_D 0:888f200c7271 422
Pushyanth_D 0:888f200c7271 423 #include "mbed.h"
Pushyanth_D 0:888f200c7271 424
Pushyanth_D 0:888f200c7271 425 DigitalOut myled(LED1);
Pushyanth_D 0:888f200c7271 426
Pushyanth_D 0:888f200c7271 427 int main() {
Pushyanth_D 0:888f200c7271 428 while(1) {
Pushyanth_D 0:888f200c7271 429 myled = 1;
Pushyanth_D 0:888f200c7271 430 wait(0.2);
Pushyanth_D 0:888f200c7271 431 myled = 0;
Pushyanth_D 0:888f200c7271 432 wait(0.2);
Pushyanth_D 0:888f200c7271 433 }
Pushyanth_D 0:888f200c7271 434 }
Pushyanth_D 0:888f200c7271 435
Pushyanth_D 0:888f200c7271 436
Pushyanth_D 0:888f200c7271 437
Pushyanth_D 0:888f200c7271 438 //4 LEDS BLINKING
Pushyanth_D 0:888f200c7271 439 #include "mbed.h"
Pushyanth_D 0:888f200c7271 440 DigitalOut myled1(LED1);
Pushyanth_D 0:888f200c7271 441 DigitalOut myled2(LED2);
Pushyanth_D 0:888f200c7271 442 DigitalOut myled3(LED3);
Pushyanth_D 0:888f200c7271 443 DigitalOut myled4(LED4);
Pushyanth_D 0:888f200c7271 444
Pushyanth_D 0:888f200c7271 445 int main() {
Pushyanth_D 0:888f200c7271 446 while(1) {
Pushyanth_D 0:888f200c7271 447 myled1= 1;
Pushyanth_D 0:888f200c7271 448 wait(0.2);
Pushyanth_D 0:888f200c7271 449 myled1 = 0;
Pushyanth_D 0:888f200c7271 450 myled2 = 1;
Pushyanth_D 0:888f200c7271 451 wait(0.2);
Pushyanth_D 0:888f200c7271 452 myled2= 0;
Pushyanth_D 0:888f200c7271 453 myled3 = 1;
Pushyanth_D 0:888f200c7271 454 wait(0.2);
Pushyanth_D 0:888f200c7271 455 myled3= 0;
Pushyanth_D 0:888f200c7271 456 myled4 = 1;
Pushyanth_D 0:888f200c7271 457 wait(0.2);
Pushyanth_D 0:888f200c7271 458 myled4= 0;
Pushyanth_D 0:888f200c7271 459 wait(0.2);
Pushyanth_D 0:888f200c7271 460 }
Pushyanth_D 0:888f200c7271 461 }
Pushyanth_D 0:888f200c7271 462
Pushyanth_D 0:888f200c7271 463
Pushyanth_D 0:888f200c7271 464 /////EXTERNAL LEDS BLINKING
Pushyanth_D 0:888f200c7271 465 #include "mbed.h"
Pushyanth_D 0:888f200c7271 466 DigitalOut myled(p23);
Pushyanth_D 0:888f200c7271 467 int main() {
Pushyanth_D 0:888f200c7271 468 while(1) {
Pushyanth_D 0:888f200c7271 469 myled = 1;
Pushyanth_D 0:888f200c7271 470 wait(0.2);
Pushyanth_D 0:888f200c7271 471 myled = 0;
Pushyanth_D 0:888f200c7271 472 wait(0.2);
Pushyanth_D 0:888f200c7271 473 }
Pushyanth_D 0:888f200c7271 474 }
Pushyanth_D 0:888f200c7271 475
Pushyanth_D 0:888f200c7271 476
Pushyanth_D 0:888f200c7271 477 ///SWITCHING
Pushyanth_D 0:888f200c7271 478 #include "mbed.h"
Pushyanth_D 0:888f200c7271 479 DigitalOut myled(LED1);
Pushyanth_D 0:888f200c7271 480 DigitalIn Switch(p23);
Pushyanth_D 0:888f200c7271 481 int main() {
Pushyanth_D 0:888f200c7271 482 while(1) {
Pushyanth_D 0:888f200c7271 483 if(Switch==1)
Pushyanth_D 0:888f200c7271 484 myled = 1;
Pushyanth_D 0:888f200c7271 485 else
Pushyanth_D 0:888f200c7271 486 myled=0;
Pushyanth_D 0:888f200c7271 487 }
Pushyanth_D 0:888f200c7271 488 }
Pushyanth_D 0:888f200c7271 489 =================================================================
Pushyanth_D 0:888f200c7271 490 POT_LM35 SENSORS
Pushyanth_D 0:888f200c7271 491
Pushyanth_D 0:888f200c7271 492 //.attach a potentiometer to mbedpin 20.
Pushyanth_D 0:888f200c7271 493 //Start a new mbed project and enter the code below.this code will continuously display the analog input value when used with a host pc terminal application.
Pushyanth_D 0:888f200c7271 494 //.using the 4 onboard mbed leds,write a program that will use potentiometer input on pin20 to continuously control how many leds are on.use the following chart to define the led
Pushyanth_D 0:888f200c7271 495 // 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 10 mV/C, with operating temperature from 55C to 150C
Pushyanth_D 0:888f200c7271 496
Pushyanth_D 0:888f200c7271 497
Pushyanth_D 0:888f200c7271 498 #include "mbed.h"
Pushyanth_D 0:888f200c7271 499 Serial pc(USBTX ,USBRX);
Pushyanth_D 0:888f200c7271 500 AnalogIn A(p20);
Pushyanth_D 0:888f200c7271 501 float Ain=A;
Pushyanth_D 0:888f200c7271 502 int main()
Pushyanth_D 0:888f200c7271 503 {
Pushyanth_D 0:888f200c7271 504 while(1)
Pushyanth_D 0:888f200c7271 505 {
Pushyanth_D 0:888f200c7271 506 pc.printf("%f the value is ",Ain*3.3);
Pushyanth_D 0:888f200c7271 507 wait(0.5);
Pushyanth_D 0:888f200c7271 508 }
Pushyanth_D 0:888f200c7271 509 }
Pushyanth_D 0:888f200c7271 510
Pushyanth_D 0:888f200c7271 511
Pushyanth_D 0:888f200c7271 512
Pushyanth_D 0:888f200c7271 513 #include "mbed.h"
Pushyanth_D 0:888f200c7271 514 AnalogIn A(p20);
Pushyanth_D 0:888f200c7271 515 BusOut leds (LED1,LED2,LED3,LED4);
Pushyanth_D 0:888f200c7271 516 int main()
Pushyanth_D 0:888f200c7271 517 {
Pushyanth_D 0:888f200c7271 518 while(1)
Pushyanth_D 0:888f200c7271 519 {
Pushyanth_D 0:888f200c7271 520 float Ain=A;
Pushyanth_D 0:888f200c7271 521 wait(0.5);
Pushyanth_D 0:888f200c7271 522 if(Ain<=0.2)
Pushyanth_D 0:888f200c7271 523 leds=0;
Pushyanth_D 0:888f200c7271 524 else if(Ain>0.2 & Ain<=0.4)
Pushyanth_D 0:888f200c7271 525 leds=0x08;
Pushyanth_D 0:888f200c7271 526 else if(Ain>0.4 & Ain<=0.6)
Pushyanth_D 0:888f200c7271 527 leds=0x0C;
Pushyanth_D 0:888f200c7271 528 else if(Ain>0.6 & Ain<=0.8)
Pushyanth_D 0:888f200c7271 529 leds=0x0E;
Pushyanth_D 0:888f200c7271 530 else if(Ain>0.8 & Ain<=1.0)
Pushyanth_D 0:888f200c7271 531 leds=0x0F;
Pushyanth_D 0:888f200c7271 532 }
Pushyanth_D 0:888f200c7271 533 }
Pushyanth_D 0:888f200c7271 534
Pushyanth_D 0:888f200c7271 535
Pushyanth_D 0:888f200c7271 536
Pushyanth_D 0:888f200c7271 537 #include "mbed.h"
Pushyanth_D 0:888f200c7271 538 Serial pc(USBTX ,USBRX);
Pushyanth_D 0:888f200c7271 539 AnalogIn A(p20);
Pushyanth_D 0:888f200c7271 540 int main()
Pushyanth_D 0:888f200c7271 541 {
Pushyanth_D 0:888f200c7271 542 while(1)
Pushyanth_D 0:888f200c7271 543 {
Pushyanth_D 0:888f200c7271 544 float Ain=A;
Pushyanth_D 0:888f200c7271 545 pc.printf("%f the value is ",(Ain*3.3)*100);
Pushyanth_D 0:888f200c7271 546 wait(0.2);
Pushyanth_D 0:888f200c7271 547 }
Pushyanth_D 0:888f200c7271 548 }
Pushyanth_D 0:888f200c7271 549 =====================================================================
Pushyanth_D 0:888f200c7271 550 RTOS
Pushyanth_D 0:888f200c7271 551 //Creating and Controlling a Simple thread.
Pushyanth_D 0:888f200c7271 552 #include "mbed.h"
Pushyanth_D 0:888f200c7271 553 #include "rtos.h"
Pushyanth_D 0:888f200c7271 554 DigitalOut led1(LED1);
Pushyanth_D 0:888f200c7271 555 DigitalOut led2(LED2);
Pushyanth_D 0:888f200c7271 556 Thread thread;
Pushyanth_D 0:888f200c7271 557 void led2_thread() {
Pushyanth_D 0:888f200c7271 558 while (true) {
Pushyanth_D 0:888f200c7271 559 led2 = !led2;
Pushyanth_D 0:888f200c7271 560 wait(0.5);
Pushyanth_D 0:888f200c7271 561 }
Pushyanth_D 0:888f200c7271 562 }
Pushyanth_D 0:888f200c7271 563 int main() {
Pushyanth_D 0:888f200c7271 564
Pushyanth_D 0:888f200c7271 565
Pushyanth_D 0:888f200c7271 566 thread.start(led2_thread);
Pushyanth_D 0:888f200c7271 567
Pushyanth_D 0:888f200c7271 568 while (true) {
Pushyanth_D 0:888f200c7271 569 led1 = !led1;
Pushyanth_D 0:888f200c7271 570 wait(0.5);
Pushyanth_D 0:888f200c7271 571 }
Pushyanth_D 0:888f200c7271 572 }
Pushyanth_D 0:888f200c7271 573
Pushyanth_D 0:888f200c7271 574
Pushyanth_D 0:888f200c7271 575 //Create two threads (blinking two sets of LEDS) with different priorities and display the priorities on PC.
Pushyanth_D 0:888f200c7271 576 #include "mbed.h"
Pushyanth_D 0:888f200c7271 577 #include "rtos.h"
Pushyanth_D 0:888f200c7271 578 DigitalOut led1(LED1);
Pushyanth_D 0:888f200c7271 579 DigitalOut led2(LED2);
Pushyanth_D 0:888f200c7271 580 DigitalOut led3(LED3);
Pushyanth_D 0:888f200c7271 581 DigitalOut led4(LED4);
Pushyanth_D 0:888f200c7271 582 Serial pc(USBTX, USBRX); // tx, rx
Pushyanth_D 0:888f200c7271 583 Thread *(test);
Pushyanth_D 0:888f200c7271 584 void led2_thread(void const *args) {
Pushyanth_D 0:888f200c7271 585 while (true) {
Pushyanth_D 0:888f200c7271 586 led2 = !led2;
Pushyanth_D 0:888f200c7271 587
Pushyanth_D 0:888f200c7271 588 Thread::wait(1000);
Pushyanth_D 0:888f200c7271 589 }
Pushyanth_D 0:888f200c7271 590 }
Pushyanth_D 0:888f200c7271 591 void led4_thread1(void const *args) {
Pushyanth_D 0:888f200c7271 592 while (true) {
Pushyanth_D 0:888f200c7271 593 led4 = !led4;
Pushyanth_D 0:888f200c7271 594
Pushyanth_D 0:888f200c7271 595 Thread::wait(1000);
Pushyanth_D 0:888f200c7271 596 }
Pushyanth_D 0:888f200c7271 597 }
Pushyanth_D 0:888f200c7271 598 int main() {
Pushyanth_D 0:888f200c7271 599 char ch;
Pushyanth_D 0:888f200c7271 600 Thread thread(led2_thread);
Pushyanth_D 0:888f200c7271 601 test = &thread;
Pushyanth_D 0:888f200c7271 602 test->set_priority(osPriorityHigh);
Pushyanth_D 0:888f200c7271 603 ch=test->get_priority();
Pushyanth_D 0:888f200c7271 604 pc.printf("%i\n\r",ch);
Pushyanth_D 0:888f200c7271 605
Pushyanth_D 0:888f200c7271 606 Thread thread1(led4_thread1);
Pushyanth_D 0:888f200c7271 607 test = &thread;
Pushyanth_D 0:888f200c7271 608 test->set_priority(osPriorityLow);
Pushyanth_D 0:888f200c7271 609 pc.printf("%i\n\r",test->get_priority());
Pushyanth_D 0:888f200c7271 610
Pushyanth_D 0:888f200c7271 611 while (true) {
Pushyanth_D 0:888f200c7271 612 led1 = !led1;
Pushyanth_D 0:888f200c7271 613 led3 = !led3;
Pushyanth_D 0:888f200c7271 614 Thread::wait(500);
Pushyanth_D 0:888f200c7271 615 }
Pushyanth_D 0:888f200c7271 616
Pushyanth_D 0:888f200c7271 617
Pushyanth_D 0:888f200c7271 618 }
Pushyanth_D 0:888f200c7271 619
Pushyanth_D 0:888f200c7271 620
Pushyanth_D 0:888f200c7271 621 /// Write a program to use MUTEX to protect printf().
Pushyanth_D 0:888f200c7271 622 #include "mbed.h"
Pushyanth_D 0:888f200c7271 623 #include "rtos.h"
Pushyanth_D 0:888f200c7271 624 Mutex stdio_mutex;
Pushyanth_D 0:888f200c7271 625 Thread t2;
Pushyanth_D 0:888f200c7271 626 Thread t3;
Pushyanth_D 0:888f200c7271 627 void notify(const char* name, int state) {
Pushyanth_D 0:888f200c7271 628 stdio_mutex.lock();
Pushyanth_D 0:888f200c7271 629 printf("%s: %d\n\r", name, state);
Pushyanth_D 0:888f200c7271 630 stdio_mutex.unlock();
Pushyanth_D 0:888f200c7271 631 }
Pushyanth_D 0:888f200c7271 632 void test_thread(void const *args) {
Pushyanth_D 0:888f200c7271 633 while (true) {
Pushyanth_D 0:888f200c7271 634 notify((const char*)args, 0); wait(1);
Pushyanth_D 0:888f200c7271 635 notify((const char*)args, 1); wait(1);
Pushyanth_D 0:888f200c7271 636 }
Pushyanth_D 0:888f200c7271 637 }
Pushyanth_D 0:888f200c7271 638
Pushyanth_D 0:888f200c7271 639 int main() {
Pushyanth_D 0:888f200c7271 640 t2.start(callback(test_thread, (void *)"Th 2"));
Pushyanth_D 0:888f200c7271 641 t3.start(callback(test_thread, (void *)"Th 3"));
Pushyanth_D 0:888f200c7271 642 test_thread((void *)"Th 1");
Pushyanth_D 0:888f200c7271 643 }
Pushyanth_D 0:888f200c7271 644 ======================================================
Pushyanth_D 0:888f200c7271 645 PWM
Pushyanth_D 0:888f200c7271 646 // Create a PWM signal which will generate a 100 Hz pulse with 50% duty cycle.
Pushyanth_D 0:888f200c7271 647 //CODE:
Pushyanth_D 0:888f200c7271 648 #include "mbed.h"
Pushyanth_D 0:888f200c7271 649
Pushyanth_D 0:888f200c7271 650 PwmOut led(p5);
Pushyanth_D 0:888f200c7271 651
Pushyanth_D 0:888f200c7271 652 int main() {
Pushyanth_D 0:888f200c7271 653 //specify time period first, then everything else
Pushyanth_D 0:888f200c7271 654 led.period(0.5);
Pushyanth_D 0:888f200c7271 655 led.write(0.5);
Pushyanth_D 0:888f200c7271 656 while(1);
Pushyanth_D 0:888f200c7271 657 }
Pushyanth_D 0:888f200c7271 658 }
Pushyanth_D 0:888f200c7271 659
Pushyanth_D 0:888f200c7271 660
Pushyanth_D 0:888f200c7271 661 //Change the duty cycle to some different values, say 0.2 (20%) and 0.8 (80%) and check the correct display.
Pushyanth_D 0:888f200c7271 662 //80 % duty cycle
Pushyanth_D 0:888f200c7271 663 #include "mbed.h"
Pushyanth_D 0:888f200c7271 664
Pushyanth_D 0:888f200c7271 665 PwmOut led(p5);
Pushyanth_D 0:888f200c7271 666
Pushyanth_D 0:888f200c7271 667 int main() {
Pushyanth_D 0:888f200c7271 668
Pushyanth_D 0:888f200c7271 669 led.period(0.1);
Pushyanth_D 0:888f200c7271 670 led.pulsewidth(0.8);
Pushyanth_D 0:888f200c7271 671 while(1);
Pushyanth_D 0:888f200c7271 672 }
Pushyanth_D 0:888f200c7271 673
Pushyanth_D 0:888f200c7271 674
Pushyanth_D 0:888f200c7271 675
Pushyanth_D 0:888f200c7271 676 //20 % duty cycle
Pushyanth_D 0:888f200c7271 677 #include "mbed.h"
Pushyanth_D 0:888f200c7271 678
Pushyanth_D 0:888f200c7271 679 PwmOut led(p5);
Pushyanth_D 0:888f200c7271 680
Pushyanth_D 0:888f200c7271 681 int main() {
Pushyanth_D 0:888f200c7271 682
Pushyanth_D 0:888f200c7271 683 led.period(0.1);
Pushyanth_D 0:888f200c7271 684 led.pulsewidth(0.2);
Pushyanth_D 0:888f200c7271 685 while(1);
Pushyanth_D 0:888f200c7271 686 }
Pushyanth_D 0:888f200c7271 687
Pushyanth_D 0:888f200c7271 688
Pushyanth_D 0:888f200c7271 689 //3. Controlling LED brightness with PWM.
Pushyanth_D 0:888f200c7271 690 #include "mbed.h"
Pushyanth_D 0:888f200c7271 691
Pushyanth_D 0:888f200c7271 692 PwmOut led(p5);
Pushyanth_D 0:888f200c7271 693 float brightness=0.0;
Pushyanth_D 0:888f200c7271 694
Pushyanth_D 0:888f200c7271 695 int main() {
Pushyanth_D 0:888f200c7271 696 while(1)
Pushyanth_D 0:888f200c7271 697 {
Pushyanth_D 0:888f200c7271 698 while(brightness<1)
Pushyanth_D 0:888f200c7271 699 {
Pushyanth_D 0:888f200c7271 700 brightness+=0.1;
Pushyanth_D 0:888f200c7271 701 led=brightness;
Pushyanth_D 0:888f200c7271 702 wait(0.2);
Pushyanth_D 0:888f200c7271 703 }
Pushyanth_D 0:888f200c7271 704 while (brightness>0)
Pushyanth_D 0:888f200c7271 705 {
Pushyanth_D 0:888f200c7271 706 brightness-=0.1;
Pushyanth_D 0:888f200c7271 707 led=brightness;
Pushyanth_D 0:888f200c7271 708 wait(0.2);
Pushyanth_D 0:888f200c7271 709 }
Pushyanth_D 0:888f200c7271 710 }
Pushyanth_D 0:888f200c7271 711 }
Pushyanth_D 0:888f200c7271 712
Pushyanth_D 0:888f200c7271 713
Pushyanth_D 0:888f200c7271 714 //. This exercise 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.
Pushyanth_D 0:888f200c7271 715 #include "mbed.h"
Pushyanth_D 0:888f200c7271 716 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 717 PwmOut led(p5);
Pushyanth_D 0:888f200c7271 718 float brightness=0.0;
Pushyanth_D 0:888f200c7271 719
Pushyanth_D 0:888f200c7271 720 int main() {
Pushyanth_D 0:888f200c7271 721 while(1)
Pushyanth_D 0:888f200c7271 722 {
Pushyanth_D 0:888f200c7271 723 if(pc.getc()=='u')
Pushyanth_D 0:888f200c7271 724 {
Pushyanth_D 0:888f200c7271 725 brightness+=0.2;
Pushyanth_D 0:888f200c7271 726 led=brightness;
Pushyanth_D 0:888f200c7271 727 }
Pushyanth_D 0:888f200c7271 728 else if(pc.getc()=='d')
Pushyanth_D 0:888f200c7271 729 {
Pushyanth_D 0:888f200c7271 730 brightness-=0.2;
Pushyanth_D 0:888f200c7271 731 led=brightness;
Pushyanth_D 0:888f200c7271 732 }
Pushyanth_D 0:888f200c7271 733 }
Pushyanth_D 0:888f200c7271 734 }
Pushyanth_D 0:888f200c7271 735 ===========================================================
Pushyanth_D 0:888f200c7271 736 SERIAL PROGRAMMING
Pushyanth_D 0:888f200c7271 737 //1.Write a serial program to dispay a message to pc.
Pushyanth_D 0:888f200c7271 738 //. Write a program to dispay the corresponding character you type in serial window.
Pushyanth_D 0:888f200c7271 739 //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.
Pushyanth_D 0:888f200c7271 740 //. 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’.
Pushyanth_D 0:888f200c7271 741
Pushyanth_D 0:888f200c7271 742 #include "mbed.h"
Pushyanth_D 0:888f200c7271 743 Serial pc(USBTX,USBRX);
Pushyanth_D 0:888f200c7271 744
Pushyanth_D 0:888f200c7271 745 int main()
Pushyanth_D 0:888f200c7271 746 {
Pushyanth_D 0:888f200c7271 747 pc.printf("sale please");
Pushyanth_D 0:888f200c7271 748 while(1)
Pushyanth_D 0:888f200c7271 749 {
Pushyanth_D 0:888f200c7271 750 pc.putc(pc.getc());
Pushyanth_D 0:888f200c7271 751 // pc.printf("sale please");
Pushyanth_D 0:888f200c7271 752 }
Pushyanth_D 0:888f200c7271 753
Pushyanth_D 0:888f200c7271 754 }
Pushyanth_D 0:888f200c7271 755
Pushyanth_D 0:888f200c7271 756
Pushyanth_D 0:888f200c7271 757 #include "mbed.h"
Pushyanth_D 0:888f200c7271 758 Serial pc(USBTX,USBRX);
Pushyanth_D 0:888f200c7271 759
Pushyanth_D 0:888f200c7271 760 int main()
Pushyanth_D 0:888f200c7271 761 {
Pushyanth_D 0:888f200c7271 762 pc.printf("sale please");
Pushyanth_D 0:888f200c7271 763 while(1)
Pushyanth_D 0:888f200c7271 764 {
Pushyanth_D 0:888f200c7271 765 pc.putc(pc.getc());
Pushyanth_D 0:888f200c7271 766 // pc.printf("sale please");
Pushyanth_D 0:888f200c7271 767 }
Pushyanth_D 0:888f200c7271 768
Pushyanth_D 0:888f200c7271 769 }
Pushyanth_D 0:888f200c7271 770
Pushyanth_D 0:888f200c7271 771
Pushyanth_D 0:888f200c7271 772
Pushyanth_D 0:888f200c7271 773
Pushyanth_D 0:888f200c7271 774 #include "mbed.h"
Pushyanth_D 0:888f200c7271 775 Serial pc(USBTX,USBRX);
Pushyanth_D 0:888f200c7271 776 DigitalOut led(LED1);
Pushyanth_D 0:888f200c7271 777 DigitalIn sw(p5);
Pushyanth_D 0:888f200c7271 778 int main()
Pushyanth_D 0:888f200c7271 779 {
Pushyanth_D 0:888f200c7271 780 while(1)
Pushyanth_D 0:888f200c7271 781 {
Pushyanth_D 0:888f200c7271 782 if(sw==1)
Pushyanth_D 0:888f200c7271 783 {
Pushyanth_D 0:888f200c7271 784 led=1;
Pushyanth_D 0:888f200c7271 785 wait(0.5);
Pushyanth_D 0:888f200c7271 786 led=0;
Pushyanth_D 0:888f200c7271 787
Pushyanth_D 0:888f200c7271 788 pc.printf("Hello");
Pushyanth_D 0:888f200c7271 789 }
Pushyanth_D 0:888f200c7271 790 else{
Pushyanth_D 0:888f200c7271 791 led=0;
Pushyanth_D 0:888f200c7271 792
Pushyanth_D 0:888f200c7271 793 pc.printf("sorry");
Pushyanth_D 0:888f200c7271 794 }
Pushyanth_D 0:888f200c7271 795
Pushyanth_D 0:888f200c7271 796 }
Pushyanth_D 0:888f200c7271 797
Pushyanth_D 0:888f200c7271 798 }
Pushyanth_D 0:888f200c7271 799
Pushyanth_D 0:888f200c7271 800
Pushyanth_D 0:888f200c7271 801 #include "mbed.h"
Pushyanth_D 0:888f200c7271 802
Pushyanth_D 0:888f200c7271 803 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 804 BusOut myled(LED1, LED2, LED3, LED4);
Pushyanth_D 0:888f200c7271 805
Pushyanth_D 0:888f200c7271 806 int main() {
Pushyanth_D 0:888f200c7271 807 while(1){
Pushyanth_D 0:888f200c7271 808 if(pc.getc()=='Y')
Pushyanth_D 0:888f200c7271 809 {
Pushyanth_D 0:888f200c7271 810
Pushyanth_D 0:888f200c7271 811 myled = 0x0A;
Pushyanth_D 0:888f200c7271 812
Pushyanth_D 0:888f200c7271 813 }
Pushyanth_D 0:888f200c7271 814 else if(pc.getc()=='N')
Pushyanth_D 0:888f200c7271 815 {
Pushyanth_D 0:888f200c7271 816
Pushyanth_D 0:888f200c7271 817 myled = 0x05;
Pushyanth_D 0:888f200c7271 818
Pushyanth_D 0:888f200c7271 819 }
Pushyanth_D 0:888f200c7271 820 else
Pushyanth_D 0:888f200c7271 821 {
Pushyanth_D 0:888f200c7271 822 myled=0;
Pushyanth_D 0:888f200c7271 823 }
Pushyanth_D 0:888f200c7271 824 }
Pushyanth_D 0:888f200c7271 825 }
Pushyanth_D 0:888f200c7271 826 =================================================================
Pushyanth_D 0:888f200c7271 827 SERVO PROGRAM
Pushyanth_D 0:888f200c7271 828 #include "mbed.h"
Pushyanth_D 0:888f200c7271 829 #include "Servo.h"
Pushyanth_D 0:888f200c7271 830
Pushyanth_D 0:888f200c7271 831 Servo myservo(p21);
Pushyanth_D 0:888f200c7271 832 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 833
Pushyanth_D 0:888f200c7271 834 int main() {
Pushyanth_D 0:888f200c7271 835 printf("Servo Calibration Controls:\n");
Pushyanth_D 0:888f200c7271 836 printf("1,2,3 - Position Servo (full left, middle, full right)\n");
Pushyanth_D 0:888f200c7271 837 printf("4,5 - Decrease or Increase range\n");
Pushyanth_D 0:888f200c7271 838
Pushyanth_D 0:888f200c7271 839 float range = 0.0005;
Pushyanth_D 0:888f200c7271 840 float position = 0.5;
Pushyanth_D 0:888f200c7271 841
Pushyanth_D 0:888f200c7271 842 while(1) {
Pushyanth_D 0:888f200c7271 843 switch(pc.getc()) {
Pushyanth_D 0:888f200c7271 844 case '1': position = 0.0; break;
Pushyanth_D 0:888f200c7271 845 case '2': position = 0.5; break;
Pushyanth_D 0:888f200c7271 846 case '3': position = 1.0; break;
Pushyanth_D 0:888f200c7271 847 case '4': range += 0.0001; break;
Pushyanth_D 0:888f200c7271 848 case '5': range -= 0.0001; break;
Pushyanth_D 0:888f200c7271 849 }
Pushyanth_D 0:888f200c7271 850 printf("position = %.1f, range = +/-%0.4f\n", position, range);
Pushyanth_D 0:888f200c7271 851 myservo.calibrate(range, 45.0);
Pushyanth_D 0:888f200c7271 852 myservo = position;
Pushyanth_D 0:888f200c7271 853 }
Pushyanth_D 0:888f200c7271 854 }
Pushyanth_D 0:888f200c7271 855 =========================================================================
Pushyanth_D 0:888f200c7271 856 SPI
Pushyanth_D 0:888f200c7271 857 //Set the mbed board as a master and exchange data with a slave, sending its own switch position and display that of slave.
Pushyanth_D 0:888f200c7271 858 //Set the mbed board as a slave and exchange data with a master, sending its own switch position and display that of master.
Pushyanth_D 0:888f200c7271 859 //Display the text typed into the terminal application of the slave terminal. Use # key to clear the screen of the text u have written
Pushyanth_D 0:888f200c7271 860
Pushyanth_D 0:888f200c7271 861 //MASTER
Pushyanth_D 0:888f200c7271 862 #include "mbed.h"
Pushyanth_D 0:888f200c7271 863 SPI ser_port(p11,p12,p13);//mosi,miso,sclk
Pushyanth_D 0:888f200c7271 864 DigitalOut led1(LED1);//led
Pushyanth_D 0:888f200c7271 865 DigitalOut led2(LED2);//led
Pushyanth_D 0:888f200c7271 866 DigitalOut cs(p14);//this acts as "slave select"
Pushyanth_D 0:888f200c7271 867 DigitalIn switch_ip1(p7);
Pushyanth_D 0:888f200c7271 868 DigitalIn switch_ip2(p8);
Pushyanth_D 0:888f200c7271 869 char switch_word;//word we will send
Pushyanth_D 0:888f200c7271 870 char recd_val;//value return from slave
Pushyanth_D 0:888f200c7271 871 int main() {
Pushyanth_D 0:888f200c7271 872 while(1) {
Pushyanth_D 0:888f200c7271 873 //Default settings for SPI Master chosen, no need for further configuration
Pushyanth_D 0:888f200c7271 874 //Set up word to be sent, by testing switch inputs
Pushyanth_D 0:888f200c7271 875 switch_word=0xa0;//set up a recognisable output pattern
Pushyanth_D 0:888f200c7271 876 if (switch_ip1==1)
Pushyanth_D 0:888f200c7271 877 switch_word=switch_word|0x01;//OR in lsb
Pushyanth_D 0:888f200c7271 878 if (switch_ip2==1)
Pushyanth_D 0:888f200c7271 879 switch_word=switch_word|0x02;//OR in next lsb
Pushyanth_D 0:888f200c7271 880 cs=0;//select slave
Pushyanth_D 0:888f200c7271 881 recd_val=ser_port.write(switch_word);//send switch_word and receive data
Pushyanth_D 0:888f200c7271 882 cs=1;
Pushyanth_D 0:888f200c7271 883 wait(0.01);
Pushyanth_D 0:888f200c7271 884 //set leds according to incoming word from slave
Pushyanth_D 0:888f200c7271 885 led1=0;//preset both to 0
Pushyanth_D 0:888f200c7271 886 led2=0;
Pushyanth_D 0:888f200c7271 887 recd_val=recd_val&0x03;//AND out unwanted bits
Pushyanth_D 0:888f200c7271 888 if(recd_val==1)
Pushyanth_D 0:888f200c7271 889 led1=1;
Pushyanth_D 0:888f200c7271 890 if(recd_val==2)
Pushyanth_D 0:888f200c7271 891 led2=1;
Pushyanth_D 0:888f200c7271 892 if(recd_val==3)
Pushyanth_D 0:888f200c7271 893 {
Pushyanth_D 0:888f200c7271 894 led1=1;
Pushyanth_D 0:888f200c7271 895 led2=1;
Pushyanth_D 0:888f200c7271 896 }
Pushyanth_D 0:888f200c7271 897 }
Pushyanth_D 0:888f200c7271 898 }
Pushyanth_D 0:888f200c7271 899
Pushyanth_D 0:888f200c7271 900
Pushyanth_D 0:888f200c7271 901 //SLAVE
Pushyanth_D 0:888f200c7271 902 #include "mbed.h"
Pushyanth_D 0:888f200c7271 903 SPISlave ser_port(p11,p12,p13,p14);//mosi,miso,sclk
Pushyanth_D 0:888f200c7271 904 DigitalOut led1(LED1);//led
Pushyanth_D 0:888f200c7271 905 DigitalOut led2(LED2);//led
Pushyanth_D 0:888f200c7271 906 DigitalIn switch_ip1(p5);
Pushyanth_D 0:888f200c7271 907 DigitalIn switch_ip2(p6);
Pushyanth_D 0:888f200c7271 908 char switch_word;//word we will send
Pushyanth_D 0:888f200c7271 909 char recd_val;//value return from slave
Pushyanth_D 0:888f200c7271 910 int main() {
Pushyanth_D 0:888f200c7271 911 //default formatting applied
Pushyanth_D 0:888f200c7271 912 while(1) {
Pushyanth_D 0:888f200c7271 913 //set up switch_word from switches that are pressed
Pushyanth_D 0:888f200c7271 914 switch_word=0xa0;//set up a recognizable outout pattern
Pushyanth_D 0:888f200c7271 915 if (switch_ip1==1)
Pushyanth_D 0:888f200c7271 916 switch_word=switch_word|0x01;
Pushyanth_D 0:888f200c7271 917 if (switch_ip2==1)
Pushyanth_D 0:888f200c7271 918 switch_word=switch_word|0x02;
Pushyanth_D 0:888f200c7271 919 if (ser_port.receive())
Pushyanth_D 0:888f200c7271 920 {//test if data transfer has occured
Pushyanth_D 0:888f200c7271 921 recd_val=ser_port.read();//Read byte from master
Pushyanth_D 0:888f200c7271 922 ser_port.reply(switch_word);//Make this the next reply
Pushyanth_D 0:888f200c7271 923 }
Pushyanth_D 0:888f200c7271 924 led1 = 0;
Pushyanth_D 0:888f200c7271 925 led2 = 0;
Pushyanth_D 0:888f200c7271 926 recd_val=recd_val&0x03;
Pushyanth_D 0:888f200c7271 927 if(recd_val==1)
Pushyanth_D 0:888f200c7271 928 led1=1;
Pushyanth_D 0:888f200c7271 929 if(recd_val==2)
Pushyanth_D 0:888f200c7271 930 led2=1;
Pushyanth_D 0:888f200c7271 931 if(recd_val==3)
Pushyanth_D 0:888f200c7271 932 {
Pushyanth_D 0:888f200c7271 933 led1=1;
Pushyanth_D 0:888f200c7271 934 led2=1;
Pushyanth_D 0:888f200c7271 935 }
Pushyanth_D 0:888f200c7271 936 }
Pushyanth_D 0:888f200c7271 937 }
Pushyanth_D 0:888f200c7271 938
Pushyanth_D 0:888f200c7271 939
Pushyanth_D 0:888f200c7271 940 //MASTER CODE:
Pushyanth_D 0:888f200c7271 941 #include "mbed.h"
Pushyanth_D 0:888f200c7271 942 SPI spi(p11,p12,p13,p14); // mosi, miso, sclk
Pushyanth_D 0:888f200c7271 943 DigitalOut cs(p8);
Pushyanth_D 0:888f200c7271 944 Serial pc(USBTX, USBRX); // tx, rx
Pushyanth_D 0:888f200c7271 945 char a;
Pushyanth_D 0:888f200c7271 946 int main()
Pushyanth_D 0:888f200c7271 947 {
Pushyanth_D 0:888f200c7271 948 while(1)
Pushyanth_D 0:888f200c7271 949 {
Pushyanth_D 0:888f200c7271 950 //pc.printf("Enter char: ");
Pushyanth_D 0:888f200c7271 951 a=pc.getc();
Pushyanth_D 0:888f200c7271 952 pc.printf("%c",a);
Pushyanth_D 0:888f200c7271 953 cs=0;
Pushyanth_D 0:888f200c7271 954 spi.write(a);
Pushyanth_D 0:888f200c7271 955 cs=1;
Pushyanth_D 0:888f200c7271 956 }
Pushyanth_D 0:888f200c7271 957 }
Pushyanth_D 0:888f200c7271 958
Pushyanth_D 0:888f200c7271 959 //SLAVE CODE:
Pushyanth_D 0:888f200c7271 960 #include "mbed.h"
Pushyanth_D 0:888f200c7271 961 SPISlave device(p11, p12, p13, p14);
Pushyanth_D 0:888f200c7271 962 Serial pc(USBTX, USBRX);
Pushyanth_D 0:888f200c7271 963 int main() {
Pushyanth_D 0:888f200c7271 964 while (1) {
Pushyanth_D 0:888f200c7271 965 if (device.receive()) {
Pushyanth_D 0:888f200c7271 966 char v = device.read();
Pushyanth_D 0:888f200c7271 967 pc.printf("%c",v);
Pushyanth_D 0:888f200c7271 968 if (v =='#')
Pushyanth_D 0:888f200c7271 969 pc.printf("/b");
Pushyanth_D 0:888f200c7271 970 }
Pushyanth_D 0:888f200c7271 971 }
Pushyanth_D 0:888f200c7271 972 ================================================================
Pushyanth_D 0:888f200c7271 973 TIMERS AND COUNTERS
Pushyanth_D 0:888f200c7271 974
Pushyanth_D 0:888f200c7271 975 //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.
Pushyanth_D 0:888f200c7271 976 // Create a square ware (400ms) output using scheduled programming and verify the timing accuracy with an oscilloscope.
Pushyanth_D 0:888f200c7271 977 // 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.
Pushyanth_D 0:888f200c7271 978 //Use the mbed interrupt In library to toggle an LED whenever a digital input goes high, implementing a debounce counter to avoid multiple interrupts.
Pushyanth_D 0:888f200c7271 979
Pushyanth_D 0:888f200c7271 980 #include "mbed.h"
Pushyanth_D 0:888f200c7271 981
Pushyanth_D 0:888f200c7271 982 Timer t;
Pushyanth_D 0:888f200c7271 983 Serial pc(USBTX,USBRX);
Pushyanth_D 0:888f200c7271 984
Pushyanth_D 0:888f200c7271 985 int main() {
Pushyanth_D 0:888f200c7271 986 t.start();
Pushyanth_D 0:888f200c7271 987 pc.printf("Hey");
Pushyanth_D 0:888f200c7271 988 t.stop();
Pushyanth_D 0:888f200c7271 989 pc.printf("The time taken was %f seconds\n", t.read());
Pushyanth_D 0:888f200c7271 990 }
Pushyanth_D 0:888f200c7271 991
Pushyanth_D 0:888f200c7271 992
Pushyanth_D 0:888f200c7271 993
Pushyanth_D 0:888f200c7271 994 #include "mbed.h"
Pushyanth_D 0:888f200c7271 995
Pushyanth_D 0:888f200c7271 996 Timer t;
Pushyanth_D 0:888f200c7271 997 DigitalOut sqr(p7);
Pushyanth_D 0:888f200c7271 998
Pushyanth_D 0:888f200c7271 999 int main() {
Pushyanth_D 0:888f200c7271 1000 while(1)
Pushyanth_D 0:888f200c7271 1001 {
Pushyanth_D 0:888f200c7271 1002 t.start();
Pushyanth_D 0:888f200c7271 1003 sqr=1;
Pushyanth_D 0:888f200c7271 1004 if (t.read_ms()==200)
Pushyanth_D 0:888f200c7271 1005 {
Pushyanth_D 0:888f200c7271 1006 t.stop();
Pushyanth_D 0:888f200c7271 1007 }
Pushyanth_D 0:888f200c7271 1008 t.start();
Pushyanth_D 0:888f200c7271 1009 sqr=0;
Pushyanth_D 0:888f200c7271 1010 if (t.read_ms()==200)
Pushyanth_D 0:888f200c7271 1011 {
Pushyanth_D 0:888f200c7271 1012 t.stop();
Pushyanth_D 0:888f200c7271 1013 }
Pushyanth_D 0:888f200c7271 1014 }
Pushyanth_D 0:888f200c7271 1015 }
Pushyanth_D 0:888f200c7271 1016
Pushyanth_D 0:888f200c7271 1017
Pushyanth_D 0:888f200c7271 1018 InterruptIn button(p5);
Pushyanth_D 0:888f200c7271 1019 DigitalOut led(LED1);
Pushyanth_D 0:888f200c7271 1020 DigitalOut flash(LED4);
Pushyanth_D 0:888f200c7271 1021
Pushyanth_D 0:888f200c7271 1022 void flip() {
Pushyanth_D 0:888f200c7271 1023 led = !led;
Pushyanth_D 0:888f200c7271 1024 }
Pushyanth_D 0:888f200c7271 1025
Pushyanth_D 0:888f200c7271 1026 int main() {
Pushyanth_D 0:888f200c7271 1027 button.rise(&flip);
Pushyanth_D 0:888f200c7271 1028 while(1) {
Pushyanth_D 0:888f200c7271 1029 flash = !flash;
Pushyanth_D 0:888f200c7271 1030 wait(0.25);
Pushyanth_D 0:888f200c7271 1031 }
Pushyanth_D 0:888f200c7271 1032 }
Pushyanth_D 0:888f200c7271 1033
Pushyanth_D 0:888f200c7271 1034 #include "mbed.h"
Pushyanth_D 0:888f200c7271 1035
Pushyanth_D 0:888f200c7271 1036 Timer debounce;
Pushyanth_D 0:888f200c7271 1037 InterruptIn button(p18);
Pushyanth_D 0:888f200c7271 1038 DigitalOut led1(p5);
Pushyanth_D 0:888f200c7271 1039
Pushyanth_D 0:888f200c7271 1040 void toggle(void);
Pushyanth_D 0:888f200c7271 1041 int main(){
Pushyanth_D 0:888f200c7271 1042 debounce.start();
Pushyanth_D 0:888f200c7271 1043 button.rise(&toggle);
Pushyanth_D 0:888f200c7271 1044 }
Pushyanth_D 0:888f200c7271 1045 void toggle(){
Pushyanth_D 0:888f200c7271 1046 if(debounce.read_ms()>200)
Pushyanth_D 0:888f200c7271 1047 led1=!led1;
Pushyanth_D 0:888f200c7271 1048 debounce.reset();
Pushyanth_D 0:888f200c7271 1049 }
Pushyanth_D 0:888f200c7271 1050
Pushyanth_D 0:888f200c7271 1051
Pushyanth_D 0:888f200c7271 1052
Pushyanth_D 0:888f200c7271 1053
Pushyanth_D 0:888f200c7271 1054