Roman Valencia / Mbed 2 deprecated inputs

Dependencies:   Hotboards_switches mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* Library:     Hotboards_switches.h
00003  * Project:     inputs
00004  * File:        main.cpp
00005  * Author:      Diego Perez
00006  * Modified by: Roman Valencia 
00007  * Contact:     http://www.hotboards.org/
00008  *
00009  * Read the value from 4 sw's (dip switch) and also read the value from a single sw.
00010  */
00011  
00012 #include "mbed.h"
00013 #include "Hotboards_switches.h"
00014 
00015 // Creates two sw objects, this interrupt will give us a LOW(0) value when close
00016 // because our dip switch works with pull-ups.
00017 Hotboards_switches dip1( PA_9 , PA_8 , PB_10 , PB_4 );
00018 Hotboards_switches sw1( PA_6 );
00019 // If your dip switch will gave you a HIGH(1) value when close, then we need to create
00020 // the sw object with an extra parameter: 
00021 // Hotboards_switches dip1( PA_9 , PA_8 , PB_10 , PB_4 , 1 );
00022 // Hotboards_swtiches sw1( PA_6 , 1 );
00023 // In any case the function will return a HIGH(1) value any time the sw is closed
00024 
00025 //For this example we will use the USB serial port, here we initialize it
00026 Serial pc(USBTX,USBRX);
00027 
00028 int i, j;
00029 uint8_t binary, decimal;
00030 
00031 int main()
00032 {
00033     while(1)
00034     {
00035         //Asks for the state of the sw1
00036         if( sw1.read( ) )
00037         {
00038             pc.printf( "sw1     = close (on)\r" );
00039         }
00040         else
00041         {
00042             pc.printf( "sw1     = open (off)\r" );
00043         }
00044         //Asks for the state of an specific sw from the dip switch dip1
00045         if( dip1.read( 2 ) )
00046         {
00047             pc.printf( "dip sw2 = close (on)\r" );
00048         }
00049         else
00050         {
00051             pc.printf( "dip sw2 = open (off)\r" );
00052         }
00053         
00054         // Reads the dip switch value and puts it in binary
00055         binary = dip1.read();
00056         // Inverts the read value to make it coincide with the state of each
00057         // switch in binary
00058         binary = ~binary;
00059         // Applies a mask(00001111) to extract the value of the dip switch
00060         decimal = binary&0x0F;
00061         // Sends throught USB serial the value in decimal
00062         pc.printf( "dec     = " );
00063         pc.printf( "%d\r" , decimal );
00064         // Sends through USB serial the value in binary
00065         pc.printf( "bin     = " );
00066         for(i=0;i<4;i++)            //Extracts and shows the binary value of each sw
00067         { 
00068             j = binary&0x08;        //Applies a mask(00001000) to extract the value of the bit in turn
00069             j>>=3;                  //Moves this bit 3 positions to the right
00070             pc.printf("%d",j);      //Sends through USB serial the value of the extracted bit
00071             binary <<=1;            //Moves the position 1 bit to the left to extract the next bit
00072         }
00073         pc.printf( "\n\r" );
00074         wait( 2 );
00075     }
00076 }