reading a complete dip switch when it changes its value
Dependencies: Hotboards_switches mbed
main.cpp
- Committer:
- RomanValenciaP
- Date:
- 2016-03-02
- Revision:
- 0:d5cdb8374e51
- Child:
- 1:11a5f3209b40
File content as of revision 0:d5cdb8374e51:
/*
* Read an entire dip switch composed by 8 interrupts, only when it changes its value
*/
#include "mbed.h"
#include "Hotboards_switches.h"
// Creates a single sw object composed by 8 interrupts. theses interrupts will give us a
// LOW(0) value when close because our dip switch works with pull-ups.
Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 );
// If your dip switch will gave you a HIGH(1) value when close, then we need to create the sw
// object with an extra parameter:
// Hotboards_switches dip_sw( PA_6 , PA_7 , PB_6 , PC_7 , PA_9 , PA_8 , PB_10 , PB_4 , 1 );
// In any case the functions will return a HIGH(1) value any time the sw is closed
// For this example we will use the USB serial port, here we initialize it
Serial pc(USBTX,USBRX);
int i, j;
uint8_t value;
int main()
{
while(1)
{
if( dip_sw.hasItChange( ) )
{
// Reads the dip switch and puts it in value
value = dip_sw.read();
// Inverts the read value to make it coincide with the state of each
// switch in binary
value = ~value;
// Sends throught USB serial the value in decimal
pc.printf( "dec = " );
pc.printf( "%d\r" , value );
// Sends through USB serial the value in binary
pc.printf( "bin = " );
for(i=0;i<8;i++) //Extracts and shows the binary value of each sw
{
j = value&0x80; //Applies a mask(10000000) to extract the value of the bit in turn
j>>=7; //Moves this bit 7 positions to the right
pc.printf("%d",j); //Sends through USB serial the value of the extracted bit
value <<=1; //Moves the position 1 bit to the left to extract the next bit
}
pc.printf( "\n\r" );
wait_ms( 250 );
}
}
}
Roman Valencia