Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of blink_kl46z_button_LCD by
main.cpp@4:c55200a56444, 2016-09-12 (annotated)
- Committer:
- annalou
- Date:
- Mon Sep 12 02:10:41 2016 +0000
- Revision:
- 4:c55200a56444
- Parent:
- 3:fc189dd7ac64
hw4.2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| scohennm | 0:e23fffd4b9a7 | 1 | #include "mbed.h" |
| scohennm | 2:b49e5adf84df | 2 | #include "SLCD.h" |
| scohennm | 2:b49e5adf84df | 3 | |
| scohennm | 0:e23fffd4b9a7 | 4 | #define LEDON false |
| scohennm | 0:e23fffd4b9a7 | 5 | #define LEDOFF true |
| scohennm | 1:2688f68df85d | 6 | #define NUMBUTS 2 |
| scohennm | 3:fc189dd7ac64 | 7 | #define LBUT PTC12 // port addresses for buttons |
| scohennm | 1:2688f68df85d | 8 | #define RBUT PTC3 |
| annalou | 4:c55200a56444 | 9 | #define BLINKTIME 0.5 // in seconds |
| scohennm | 3:fc189dd7ac64 | 10 | #define BUTTONTIME 0.2 |
| annalou | 4:c55200a56444 | 11 | #define LCDCHARLEN 40 |
| scohennm | 2:b49e5adf84df | 12 | #define NUMMESS 2 |
| scohennm | 2:b49e5adf84df | 13 | #define LRED "RED" |
| scohennm | 2:b49e5adf84df | 14 | #define LGREEN "GREN" |
| annalou | 4:c55200a56444 | 15 | #define PRED "RED BUTTON PUSHED\r\n" |
| annalou | 4:c55200a56444 | 16 | #define PGREEN "GREEN BUTTON PUSHED\r\n" |
| scohennm | 3:fc189dd7ac64 | 17 | #define PROGNAME "blink_kl46z_buttton LCD v2\r\n" |
| scohennm | 0:e23fffd4b9a7 | 18 | |
| scohennm | 0:e23fffd4b9a7 | 19 | // slightly more interesting blinky 140814 sc |
| scohennm | 2:b49e5adf84df | 20 | SLCD slcd; //define LCD display |
| scohennm | 0:e23fffd4b9a7 | 21 | |
| scohennm | 3:fc189dd7ac64 | 22 | // Timer to elliminate wait() function |
| scohennm | 3:fc189dd7ac64 | 23 | Timer LEDTimer; // for blinking LEDs |
| scohennm | 3:fc189dd7ac64 | 24 | Timer ButtonTimer; // for reading button states |
| scohennm | 1:2688f68df85d | 25 | |
| scohennm | 3:fc189dd7ac64 | 26 | bool ledState = LEDON; |
| scohennm | 3:fc189dd7ac64 | 27 | |
| scohennm | 1:2688f68df85d | 28 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; |
| scohennm | 1:2688f68df85d | 29 | DigitalOut LEDs[NUMBUTS] = {LED_GREEN, LED_RED}; |
| scohennm | 1:2688f68df85d | 30 | Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons |
| scohennm | 0:e23fffd4b9a7 | 31 | |
| scohennm | 3:fc189dd7ac64 | 32 | void allLEDsOff(){ |
| scohennm | 3:fc189dd7ac64 | 33 | int i; |
| scohennm | 3:fc189dd7ac64 | 34 | for (i=0; i<NUMBUTS; i++){ |
| scohennm | 3:fc189dd7ac64 | 35 | LEDs[i] = LEDOFF; |
| scohennm | 3:fc189dd7ac64 | 36 | } |
| scohennm | 3:fc189dd7ac64 | 37 | } |
| scohennm | 3:fc189dd7ac64 | 38 | |
| scohennm | 2:b49e5adf84df | 39 | void LCDMess(char *lMess){ |
| scohennm | 2:b49e5adf84df | 40 | slcd.Home(); |
| scohennm | 2:b49e5adf84df | 41 | slcd.clear(); |
| scohennm | 2:b49e5adf84df | 42 | slcd.printf(lMess); |
| scohennm | 2:b49e5adf84df | 43 | } |
| scohennm | 2:b49e5adf84df | 44 | |
| scohennm | 3:fc189dd7ac64 | 45 | void initialize_global_vars(){ |
| annalou | 4:c55200a56444 | 46 | //pc.printf(PROGNAME); |
| scohennm | 3:fc189dd7ac64 | 47 | // set up DAQ timers |
| scohennm | 3:fc189dd7ac64 | 48 | ButtonTimer.start(); |
| scohennm | 3:fc189dd7ac64 | 49 | ButtonTimer.reset(); |
| scohennm | 3:fc189dd7ac64 | 50 | LEDTimer.start(); |
| scohennm | 3:fc189dd7ac64 | 51 | LEDTimer.reset(); |
| scohennm | 3:fc189dd7ac64 | 52 | allLEDsOff(); |
| scohennm | 3:fc189dd7ac64 | 53 | } |
| scohennm | 1:2688f68df85d | 54 | // -------------------------------- |
| scohennm | 3:fc189dd7ac64 | 55 | int main() { |
| scohennm | 1:2688f68df85d | 56 | int i; |
| annalou | 4:c55200a56444 | 57 | int currentLED = 1; |
| scohennm | 3:fc189dd7ac64 | 58 | char rMess[NUMMESS][LCDCHARLEN]={LGREEN, LRED}; // for LCD |
| scohennm | 3:fc189dd7ac64 | 59 | char pMess[NUMMESS][LCDCHARLEN]={PRED, PGREEN}; // for pc serial port |
| scohennm | 3:fc189dd7ac64 | 60 | |
| scohennm | 3:fc189dd7ac64 | 61 | initialize_global_vars(); //keep things organized |
| scohennm | 3:fc189dd7ac64 | 62 | LEDs[currentLED] = LEDON; |
| scohennm | 2:b49e5adf84df | 63 | LCDMess(rMess[currentLED]); |
| scohennm | 2:b49e5adf84df | 64 | pc.printf(pMess[currentLED]); |
| scohennm | 3:fc189dd7ac64 | 65 | // End of setup |
| scohennm | 0:e23fffd4b9a7 | 66 | while(true) { |
| annalou | 4:c55200a56444 | 67 | if (ButtonTimer <= BUTTONTIME){ |
| scohennm | 3:fc189dd7ac64 | 68 | for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1 |
| scohennm | 3:fc189dd7ac64 | 69 | if(!buttons[i]) { |
| scohennm | 3:fc189dd7ac64 | 70 | allLEDsOff(); |
| scohennm | 3:fc189dd7ac64 | 71 | LCDMess(rMess[i]); |
| scohennm | 3:fc189dd7ac64 | 72 | pc.printf(pMess[i]); |
| scohennm | 3:fc189dd7ac64 | 73 | currentLED = i; |
| scohennm | 3:fc189dd7ac64 | 74 | } // if ! buttons |
| scohennm | 3:fc189dd7ac64 | 75 | }// for loop to look at buttons |
| scohennm | 3:fc189dd7ac64 | 76 | ButtonTimer.reset(); |
| scohennm | 1:2688f68df85d | 77 | } |
| scohennm | 3:fc189dd7ac64 | 78 | if(LEDTimer.read() > BLINKTIME){ |
| scohennm | 3:fc189dd7ac64 | 79 | LEDTimer.reset(); |
| scohennm | 3:fc189dd7ac64 | 80 | ledState = !ledState; // Flip the general state |
| scohennm | 3:fc189dd7ac64 | 81 | LEDs[currentLED] = ledState; |
| scohennm | 3:fc189dd7ac64 | 82 | } |
| scohennm | 3:fc189dd7ac64 | 83 | // Do other things here between times of reading and flashing |
| scohennm | 0:e23fffd4b9a7 | 84 | } |
| scohennm | 0:e23fffd4b9a7 | 85 | } |
