Pushyanth Damarapati / Mbed 2 deprecated mbed

Dependencies:   mbed

main.cpp

Committer:
Pushyanth_D
Date:
2018-10-28
Revision:
0:888f200c7271

File content as of revision 0:888f200c7271:

BLUETOOTH HC 05

 
#include "mbed.h"
Serial pc(USBTX, USBRX);
Serial blue(p9, p10);
DigitalOut myled(LED1);
int main() 
{
    blue.baud(115200);
    pc.baud(115200);
    pc.printf("Bluetooth Start\r\n");
    // echo back characters and toggle the LED
    while (1) 
    {
        if (blue.readable()) 
        {
            pc.putc(blue.getc());
            myled = !myled;
        }
    }
}




 #include "mbed.h"
Serial pc(USBTX, USBRX);
Serial blue(p9, p10);  
AnalogIn temp(p5);
int main() 
{
    blue.baud(9600);
    pc.baud(9600); 
    while (1) 
    { float t=temp*3.3*100;
        if (pc.readable()) 
        {   blue.printf("The temperature is ");
            blue.putc(t);
        } 
    }
}

===================================
BUSOUT FUNCTION

/// To write C++ Source Code to understand the digital port access 
/Program mbed Board to light two-two LED’s at a time using BusOut function.
//Display hexadecimal Coding pattern from 0 to 15 by blinking the on-board LED’s using BusOut function.
//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
#include "mbed.h"
BusOut leds1(LED1,LED2);
BusOut leds2(LED3,LED4);
int main() {
    while(1) {
        leds1 = 0xFF;
        wait(0.2);
    leds1 = 0;
        wait(0.2);
        leds2 = 0xFF;
        wait(0.2);
    leds2 = 0;
        wait(0.2)
    }
}




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




#include "mbed.h"
BusIn switches1(p22,p23);
BusIn switches2(p24,p25);
BusOut leds(LED1,LED2,LED3,LED4);
              int main() {
    while(1) {
        if(switches1==0x03)
        leds=0x55;
        else if(switches2==0x03)
        leds=0xAA;
            }
}



#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);
       }
    }
=================================================

SERVO MOTOR

///To control a server motor using PWM.
#include "mbed.h"
PwmOut ser(p6);
AnalogIn pot(p5);
int main()
{
float i;
ser.period(20);
while(1)
{
if(pot==0)
{
for(i=0;i<1;i=i+0.25)
ser.pulsewidth_ms(1.25*i);
}
else
{
for(i=1;i>0;i=i-0.25)
ser.pulsewidth_ms(1.25*i);
}

}



// Light tracking devices are very important for the capture of solar energy. 
//Often, they operate in 3 dimensions, and tilt a solar panel so that it is facing the sun as accurately as possible.
// 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 
//connected to the LDRs using the circuit in the figure to two ADC inputs. 
//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

#include "mbed.h"
AnalogIn ldr1(p7);
AnalogIn ldr2(p6);
PwmOut servo(p20);

int main() {
    
    servo.period_ms(20);
    float i = 1.25;
    while(1) {
        //servo.pulsewidth(1.25);
        while(ldr1!=ldr2) {
            if(ldr1<ldr2)
                servo.pulsewidth(i+0.25);
            else
                servo.pulsewidth(i-0.25);
                
        }
    }
}
===================================================================
I2C
MASTER AND SLAVE

//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:
//Master
#include <mbed.h>
Serial pc(USBTX, USBRX);
I2CSlave slave(p28, p27);
int main()
{
    char buf[20];
    char msg[] = "Slave!";
    slave.address(0xA0);
    while (1) {
        int i = slave.receive();
        switch (i) {
            case I2CSlave::ReadAddressed:
                slave.write(msg, strlen(msg) + 1); // Includes null char
                break;
            case I2CSlave::WriteGeneral:
                slave.read(buf, 20);
                pc.printf("Read : %s\n", buf);
                break;
            case I2CSlave::WriteAddressed:
                slave.read(buf, 20);
                pc.printf("Read : %s\n", buf);
                break;
        }
        for(int i = 0; i< 20; i++)
            buf[i] = 0;
    }



//Slave
#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);
}




///I2C Master, transfers switch state to second mbed acting as slave, and displays state of slave’s switches on its leds.
Master:
#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) {
            led1=1;
            led2=1;
        }
    }
}



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

#include "mbed.h"
I2CSlave 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) {
            led1=1;
            led2=1;
        }
    }
}
===============================================================
LCD INTERFACING

// LCD.cpp file 
#include "LCD.h" 
DigitalOut RS(p19);
DigitalOut E(p20); 
BusOut data(p21, p22, p23, p24);
void toggle_enable(void){
E=1; 
wait(0.001);
 E=0; 
wait(0.001);
}//initialise LCD function 
void LCD_init(void)
{
wait(0.02); 
RS=0; 
E=0;//function node 
data=0x2; 
toggle_enable(); //display mode 
data=0x0; 
toggle_enable(); 
data=0xF; 
toggle_enable(); //clear display 
data=0x0; 
toggle_enable() ; 
data=0x1; 
toggle_enable(); 
}
void display_to_LCD(char value) //display function 
{ 
RS=1;
data=value>>4;
toggle_enable(); 
data=value&0x0F; 
toggle_enable();
}



//LCD.h FIle
#ifndef LCD_H
#define LCD_H
#include "mbed.h"


void display_to_LCD(char value);
void toggle_enable(void);
void LCD_init(void);

#endif




#include "mbed.h"
#include "LCD.h"
//DigitalOut vo(p18);

int main()
{
//vo = 0.5;
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’
}



//Write a C++ program by using TextLCD library to display a message “Hello World”.
CODE:
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p19, p20, p21, p22, p23, p24); //rs,e,d0,d1,d2,d3
int main()
{
lcd.printf("Hello World!");
}

//Display a continuous count variable o the LCD display 2nd row 5th digit.
 CODE:
#include "mbed.h"
#include "TextLCD.h"
TextLCD lcd(p19, p20, p21, p22, p23, p24);
//rs,e,d0,d1,d2,d3
int t=0;
int main()
{
    while(t<=50)
    {    lcd.locate(4,2);

        t=t+1;
        lcd.printf("%d",t);
    wait(0.5);
    }
}
=======================================================================
LED BLINKING EXP1

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}



//4 LEDS BLINKING
#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);
    }
}


/////EXTERNAL LEDS BLINKING
#include "mbed.h"
DigitalOut myled(p23);
int main() {
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
}
}


///SWITCHING
#include "mbed.h"
DigitalOut myled(LED1);
DigitalIn Switch(p23);
int main() {
    while(1) {
            if(Switch==1)
                myled = 1;
                else
                myled=0;
            }
}
=================================================================
POT_LM35 SENSORS

//.attach a potentiometer to mbedpin 20. 
//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.
//.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 
// 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


#include "mbed.h"
Serial pc(USBTX ,USBRX);
AnalogIn A(p20);
 float Ain=A;
int main()
{
    while(1)
{
  pc.printf("%f the value is ",Ain*3.3);
  wait(0.5);
        }
        }



#include "mbed.h"
AnalogIn A(p20);
BusOut leds (LED1,LED2,LED3,LED4);
int main()
{
    while(1)
{
  float Ain=A;
  wait(0.5);
   if(Ain<=0.2)
   leds=0;
   else if(Ain>0.2 & Ain<=0.4)
   leds=0x08;
   else if(Ain>0.4 & Ain<=0.6)
   leds=0x0C;
   else if(Ain>0.6 & Ain<=0.8)
   leds=0x0E;
   else if(Ain>0.8 & Ain<=1.0)
   leds=0x0F;
       }
        }



#include "mbed.h"
Serial pc(USBTX ,USBRX);
AnalogIn A(p20);
int main()
{
    while(1)
{
     float Ain=A;
      pc.printf("%f the value is ",(Ain*3.3)*100);
         wait(0.2);
        }
        }
=====================================================================
RTOS
//Creating and Controlling a Simple thread.
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread;
void led2_thread() {
    while (true) {
        led2 = !led2;
        wait(0.5);
    }
}
int main() {
  
 
thread.start(led2_thread);
    
while (true) {
        led1 = !led1;
        wait(0.5);
    }
}


//Create two threads (blinking two sets of LEDS) with different priorities and display the priorities on PC.
#include "mbed.h"
#include "rtos.h"
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
Serial pc(USBTX, USBRX); // tx, rx
Thread *(test);
void led2_thread(void const *args) {
    while (true) {
        led2 = !led2;  
                 
        Thread::wait(1000);       
    }
    }
    void led4_thread1(void const *args) {
    while (true) {
        led4 = !led4; 
                  
        Thread::wait(1000);       
    }
}
int main() {
    char ch;
    Thread thread(led2_thread);
    test = &thread;
    test->set_priority(osPriorityHigh);
    ch=test->get_priority();
    pc.printf("%i\n\r",ch);
    
    Thread thread1(led4_thread1);
    test = &thread;
    test->set_priority(osPriorityLow);
    pc.printf("%i\n\r",test->get_priority());
        
    while (true) {
        led1 = !led1;
        led3 = !led3;
        Thread::wait(500);        
    }

    
}


/// Write a program to use MUTEX to protect printf().
#include "mbed.h"
#include "rtos.h"
Mutex stdio_mutex;
Thread t2;
Thread t3;
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); wait(1);
        notify((const char*)args, 1); wait(1);
    }
}

int main() {
    t2.start(callback(test_thread, (void *)"Th 2"));
    t3.start(callback(test_thread, (void *)"Th 3"));
    test_thread((void *)"Th 1");
}
======================================================
PWM
// Create a PWM signal which will generate a 100 Hz pulse with 50% duty cycle.
//CODE:
#include "mbed.h"

PwmOut led(p5);

int main() {
    //specify time period first, then everything else
    led.period(0.5);
    led.write(0.5);
    while(1);
}
              }


//Change the duty cycle to some different values, say 0.2 (20%) and 0.8 (80%) and check the correct display.
//80 % duty cycle
#include "mbed.h"
 
PwmOut led(p5);
 
int main() {
    
    led.period(0.1);  
    led.pulsewidth(0.8); 
    while(1);          
}



//20 % duty cycle
#include "mbed.h"
 
PwmOut led(p5);
 
int main() {
    
    led.period(0.1);  
    led.pulsewidth(0.2); 
    while(1);          
}


//3.    Controlling LED brightness with PWM.
#include "mbed.h"

PwmOut led(p5);
float brightness=0.0;

int main() {
        while(1)
        {
            while(brightness<1)
            {
                brightness+=0.1;
                led=brightness;
                wait(0.2);
            }
            while (brightness>0)
            {
                brightness-=0.1;
                led=brightness;
                wait(0.2);
            }
        }   
}


//. 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.
#include "mbed.h"
Serial pc(USBTX, USBRX);
PwmOut led(p5);
float brightness=0.0;

int main() {
        while(1)
        {
            if(pc.getc()=='u')
            {
                brightness+=0.2;
                led=brightness;
            }
            else if(pc.getc()=='d')
            {
                brightness-=0.2;
                led=brightness;
            }
        }   
}
===========================================================
SERIAL PROGRAMMING
//1.Write a serial program to dispay a message to pc.
//. 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.
//. 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’. 

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

int main()
{
    pc.printf("sale please");
    while(1)
    {
        pc.putc(pc.getc());
        // pc.printf("sale please");
        }
        
        }


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

int main()
{
    pc.printf("sale please");
    while(1)
    {
        pc.putc(pc.getc());
        // pc.printf("sale please");
        }
        
        }




#include "mbed.h"
Serial pc(USBTX,USBRX);
DigitalOut led(LED1);
DigitalIn sw(p5);
int main()
{
    while(1)
    {
        if(sw==1)
        {
        led=1;
        wait(0.5);
        led=0;
        
    pc.printf("Hello");
    }
    else{
        led=0;
        
    pc.printf("sorry");
    }
   
        }
        
    }


 #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;
    }
    }
}
=================================================================
SERVO PROGRAM
#include "mbed.h"
#include "Servo.h"

Servo myservo(p21);
Serial pc(USBTX, USBRX);

int main() {
    printf("Servo Calibration Controls:\n");
    printf("1,2,3 - Position Servo (full left, middle, full right)\n");
    printf("4,5 - Decrease or Increase range\n");

    float range = 0.0005;
    float position = 0.5;
    
    while(1) {                   
        switch(pc.getc()) {
            case '1': position = 0.0; break;
            case '2': position = 0.5; break;
            case '3': position = 1.0; break;
            case '4': range += 0.0001; break; 
            case '5': range -= 0.0001; break; 
        }
        printf("position = %.1f, range = +/-%0.4f\n", position, range);
        myservo.calibrate(range, 45.0); 
        myservo = position;
    }
}
=========================================================================
SPI
//Set the mbed board as a master and exchange data with a slave, sending its own switch position and display that of slave.
//Set the mbed board as a slave and exchange data with a master, sending its own switch position and display that of master.
//Display the text typed into the terminal application of the slave terminal. Use # key to clear the screen of the text u have written

//MASTER
#include "mbed.h"
SPI ser_port(p11,p12,p13);//mosi,miso,sclk
DigitalOut led1(LED1);//led
DigitalOut led2(LED2);//led
DigitalOut cs(p14);//this acts as "slave select"
DigitalIn switch_ip1(p7);
DigitalIn  switch_ip2(p8);
char switch_word;//word we will send
char recd_val;//value return from slave
int main() {
    while(1) {
        //Default settings for SPI Master chosen, no need for further configuration
        //Set up word to be sent, by testing switch inputs
        switch_word=0xa0;//set up a recognisable output pattern
        if (switch_ip1==1)
            switch_word=switch_word|0x01;//OR in lsb
        if (switch_ip2==1)
            switch_word=switch_word|0x02;//OR in next lsb
        cs=0;//select slave
        recd_val=ser_port.write(switch_word);//send switch_word and receive data
        cs=1;
        wait(0.01);
        //set leds according to incoming word from slave
        led1=0;//preset both to 0
        led2=0;
        recd_val=recd_val&0x03;//AND out unwanted bits
        if(recd_val==1)
            led1=1;
        if(recd_val==2)
            led2=1;
        if(recd_val==3)
        {
            led1=1;
            led2=1;
            }
    }
}


//SLAVE
#include "mbed.h"
SPISlave ser_port(p11,p12,p13,p14);//mosi,miso,sclk
DigitalOut led1(LED1);//led
DigitalOut led2(LED2);//led
DigitalIn switch_ip1(p5);
DigitalIn  switch_ip2(p6);
char switch_word;//word we will send
char recd_val;//value return from slave
int main() {
    //default formatting applied
    while(1) {
        //set up switch_word from switches that are pressed
        switch_word=0xa0;//set up a recognizable outout pattern
        if (switch_ip1==1)
            switch_word=switch_word|0x01;
        if (switch_ip2==1)
            switch_word=switch_word|0x02;
        if (ser_port.receive())
        {//test if data transfer has occured
            recd_val=ser_port.read();//Read byte from master
            ser_port.reply(switch_word);//Make this the next reply
        }
        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;
        }
    }
}


//MASTER CODE:
#include "mbed.h"
SPI spi(p11,p12,p13,p14); // mosi, miso, sclk
DigitalOut cs(p8);
Serial pc(USBTX, USBRX); // tx, rx
char a;
int main()
{
    while(1)
    {   
        //pc.printf("Enter char: ");
        a=pc.getc();
        pc.printf("%c",a);
        cs=0;
        spi.write(a);
        cs=1;
    }
}

//SLAVE CODE:
  #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");
}
}
================================================================
TIMERS AND COUNTERS

//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.
// Create a square ware (400ms) output using scheduled programming and verify the timing accuracy with an oscilloscope. 
// 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.
//Use the mbed interrupt In library to toggle an LED whenever a digital input goes high, implementing a debounce counter to avoid multiple interrupts.

#include "mbed.h"
 
Timer t;
Serial pc(USBTX,USBRX);
 
int main() {
    t.start();
    pc.printf("Hey");
    t.stop();
    pc.printf("The time taken was %f seconds\n", t.read());
}



#include "mbed.h"
 
Timer t;
DigitalOut sqr(p7);
 
int main() {
    while(1)
    {
        t.start();
        sqr=1;
        if (t.read_ms()==200)
        {
            t.stop();
            }
        t.start();
        sqr=0;
        if (t.read_ms()==200)
        {
            t.stop();
            }
        }
}


InterruptIn button(p5);
DigitalOut led(LED1);
DigitalOut flash(LED4);
 
void flip() {
    led = !led;
}
 
int main() {
    button.rise(&flip);  
    while(1) {           
        flash = !flash;
        wait(0.25);
    }
}

#include "mbed.h"

Timer debounce;
InterruptIn button(p18);
DigitalOut led1(p5);
 
void toggle(void);
int main(){
    debounce.start();
    button.rise(&toggle);
    }
void toggle(){
    if(debounce.read_ms()>200)
    led1=!led1;
    debounce.reset();
    }