This a basic program that uses digital input-output functions. It counts how many times the button is pressed and displays the resulting value as 4-bit binary on LPC1768 leds.

Dependencies:   mbed

Revision:
0:4b9e06ca482c
Child:
1:55585733d77e
diff -r 000000000000 -r 4b9e06ca482c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 09 09:31:28 2015 +0000
@@ -0,0 +1,55 @@
+/*   Digital input output example on LPC1768 
+*
+*    @author: Baser Kandehir 
+*    @date: July 9, 2015
+*    @license: Use this code however you'd like
+*   
+*    @description of the program: 
+*
+*    This a basic program that uses digital input-output functions.
+*    It counts how many times the button is pressed and displays the 
+*    resulting value as 4-bit binary on LPC1768 leds. 
+*     
+*    @connections: Pull up button with 10k resistor is connected to pin 18.   
+*     
+*/
+
+#include "mbed.h"
+
+/* Activate on board LEDs */
+DigitalOut LPC_led1(LED1);
+DigitalOut LPC_led2(LED2);
+DigitalOut LPC_led3(LED3);
+DigitalOut LPC_led4(LED4);
+
+DigitalIn button(p18);  // button (pull-up) is connected to P18
+
+/* Function prototype */
+void ledCounter(int value);
+
+int count=0;
+
+int main()
+{
+    while(1)
+    {
+       if(button.read()==0)           // if the button is pressed
+       {   
+           while(button.read()==0);   // wait until release
+           wait_ms(20);               // button debounce
+           count++;                   // count up
+           count%=16;                 // count (mod 16)
+       }
+       ledCounter(count);             // display the count 
+    }
+}
+
+// This function can display value(mod 16) as binary on LPC1768
+// For example =  12 = (1010) led2 and led4 will be ON, led1 and led3 will be OFF.
+void ledCounter(int value)
+{
+    LPC_led1=value%2;
+    LPC_led2=(value>>1)%2;
+    LPC_led3=(value>>2)%2;
+    LPC_led4=(value>>3)%2;
+}