reading a complete dip switch

Dependencies:   Hotboards_switches mbed

Files at this revision

API Documentation at this revision

Comitter:
RomanValenciaP
Date:
Wed Mar 02 20:32:39 2016 +0000
Child:
1:2ed56eefabf0
Commit message:
first release - recquires approval

Changed in this revision

Hotboards_switches.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Hotboards_switches.lib	Wed Mar 02 20:32:39 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/Hotboards/code/Hotboards_switches/#dfb1302f847d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Mar 02 20:32:39 2016 +0000
@@ -0,0 +1,47 @@
+
+/*
+ * Read an entire dip switch composed by 8 interrupts
+ */
+
+#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)
+    {
+        // 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( 1 );
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Mar 02 20:32:39 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/87f2f5183dfb
\ No newline at end of file