mbed_binary_counter

Dependencies:   mbed

Committer:
foivosHrist
Date:
Thu Oct 09 20:30:07 2014 +0000
Revision:
1:4e311ab3af04
Parent:
0:858e3371c27e
mbed binary counter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
foivosHrist 1:4e311ab3af04 1 // for LPC1768
foivosHrist 1:4e311ab3af04 2 //binary counter with 4 onboard leds. counts +1 with every button press
foivosHrist 1:4e311ab3af04 3
foivosHrist 0:858e3371c27e 4 #include "mbed.h"
foivosHrist 0:858e3371c27e 5 #define PRESSED 0
foivosHrist 0:858e3371c27e 6 #define UNPRESSED 1
foivosHrist 1:4e311ab3af04 7 #define BUTTONPIN p8
foivosHrist 0:858e3371c27e 8
foivosHrist 1:4e311ab3af04 9 int getNext(); //get next num for counter. ( counts 0 to 15) after 15 there is a rollback.
foivosHrist 1:4e311ab3af04 10 void setLeds(int); //sets the leds to show in binary the argument
foivosHrist 1:4e311ab3af04 11 Serial pc(USBTX, USBRX);//initialize serial connection
foivosHrist 1:4e311ab3af04 12
foivosHrist 1:4e311ab3af04 13 DigitalIn button(BUTTONPIN); //button on pin8
foivosHrist 1:4e311ab3af04 14 DigitalOut leds[4]={ DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) };
foivosHrist 0:858e3371c27e 15
foivosHrist 0:858e3371c27e 16 //-----------------------------------------------
foivosHrist 0:858e3371c27e 17 int main() {
foivosHrist 0:858e3371c27e 18
foivosHrist 1:4e311ab3af04 19 button.mode(PullUp); //set button internal pullup
foivosHrist 0:858e3371c27e 20
foivosHrist 1:4e311ab3af04 21 bool previousState=button;
foivosHrist 0:858e3371c27e 22 bool currentState;
foivosHrist 0:858e3371c27e 23
foivosHrist 1:4e311ab3af04 24 while(1) {//polling
foivosHrist 0:858e3371c27e 25
foivosHrist 1:4e311ab3af04 26 currentState=button;
foivosHrist 1:4e311ab3af04 27 if(currentState==PRESSED && previousState==UNPRESSED){ //on button push
foivosHrist 1:4e311ab3af04 28 setLeds(getNext());
foivosHrist 1:4e311ab3af04 29
foivosHrist 0:858e3371c27e 30
foivosHrist 0:858e3371c27e 31 }
foivosHrist 0:858e3371c27e 32 previousState=currentState;
foivosHrist 1:4e311ab3af04 33 wait(0.1); //small delay for debounce
foivosHrist 1:4e311ab3af04 34
foivosHrist 0:858e3371c27e 35 }
foivosHrist 0:858e3371c27e 36 }
foivosHrist 1:4e311ab3af04 37 //------------------------------------------
foivosHrist 1:4e311ab3af04 38 void setLeds(int num){
foivosHrist 1:4e311ab3af04 39
foivosHrist 1:4e311ab3af04 40 int eights =num/8; //how many of 8's the number has...
foivosHrist 1:4e311ab3af04 41 int fours =(num - eights*8) /4 ;
foivosHrist 1:4e311ab3af04 42 int twos =(num - eights*8 -fours*4)/2;
foivosHrist 1:4e311ab3af04 43 int ones =num%2;
foivosHrist 1:4e311ab3af04 44
foivosHrist 1:4e311ab3af04 45 leds[0]=eights;
foivosHrist 1:4e311ab3af04 46 leds[1]=fours;
foivosHrist 1:4e311ab3af04 47 leds[2]=twos;
foivosHrist 1:4e311ab3af04 48 leds[3]=ones;
foivosHrist 1:4e311ab3af04 49 pc.printf("dec=%d bin=%d%d%d%d \n",num,eights,fours,twos,ones);
foivosHrist 1:4e311ab3af04 50
foivosHrist 1:4e311ab3af04 51 }
foivosHrist 0:858e3371c27e 52 //-----------------------------------------------
foivosHrist 0:858e3371c27e 53 int getNext(){
foivosHrist 0:858e3371c27e 54 static int i=0;
foivosHrist 0:858e3371c27e 55 if(i==15)i=0;
foivosHrist 0:858e3371c27e 56 else i++;
foivosHrist 0:858e3371c27e 57
foivosHrist 0:858e3371c27e 58 return i;
foivosHrist 0:858e3371c27e 59
foivosHrist 0:858e3371c27e 60 }