SSD541_HW12.2

Dependencies:   SLCD TSI mbed

Committer:
eseifert
Date:
Sun Nov 06 16:54:17 2016 +0000
Revision:
0:fb6ea61c315b
Child:
1:d82569d51964
finished;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eseifert 0:fb6ea61c315b 1 #include "mbed.h"
eseifert 0:fb6ea61c315b 2 #include "TSISensor.h"
eseifert 0:fb6ea61c315b 3 #include "SLCD.h"
eseifert 0:fb6ea61c315b 4 #define TSILIMIT 0.99
eseifert 0:fb6ea61c315b 5 #define LCDCHARLEN 10
eseifert 0:fb6ea61c315b 6 #define DATAINTERVAL 0.5
eseifert 0:fb6ea61c315b 7 #define PROGNAME "kl46z_recursive_count\n\r"
eseifert 0:fb6ea61c315b 8 #define BUTTONTIME 0.2
eseifert 0:fb6ea61c315b 9 #define LCDTITLE "rCNT"
eseifert 0:fb6ea61c315b 10 #define TITLEWAIT 2.0
eseifert 0:fb6ea61c315b 11 #define NUMBUTS 2
eseifert 0:fb6ea61c315b 12 #define LBUT PTC12 // port addresses for buttons
eseifert 0:fb6ea61c315b 13 #define RBUT PTC3
eseifert 0:fb6ea61c315b 14
eseifert 0:fb6ea61c315b 15 DigitalOut gpo(D0);
eseifert 0:fb6ea61c315b 16 DigitalOut led0(LED_RED);
eseifert 0:fb6ea61c315b 17 SLCD slcd;
eseifert 0:fb6ea61c315b 18 Serial pc(USBTX, USBRX);
eseifert 0:fb6ea61c315b 19 PwmOut gled(LED_GREEN);
eseifert 0:fb6ea61c315b 20 PwmOut rled(LED_RED);
eseifert 0:fb6ea61c315b 21 TSISensor tsi;
eseifert 0:fb6ea61c315b 22 Timer ButtonTimer;
eseifert 0:fb6ea61c315b 23 DigitalIn buttons[NUMBUTS] = {LBUT, RBUT};
eseifert 0:fb6ea61c315b 24
eseifert 0:fb6ea61c315b 25 char lcdData[LCDCHARLEN];
eseifert 0:fb6ea61c315b 26 float tsidata = 0.0;
eseifert 0:fb6ea61c315b 27 int count_num = 0;
eseifert 0:fb6ea61c315b 28 int count_min = 0;
eseifert 0:fb6ea61c315b 29
eseifert 0:fb6ea61c315b 30 void LCDMess(char *lMess){
eseifert 0:fb6ea61c315b 31 slcd.Home();
eseifert 0:fb6ea61c315b 32 slcd.clear();
eseifert 0:fb6ea61c315b 33 slcd.printf(lMess);
eseifert 0:fb6ea61c315b 34 }
eseifert 0:fb6ea61c315b 35
eseifert 0:fb6ea61c315b 36 void initialize_global_vars()
eseifert 0:fb6ea61c315b 37 {
eseifert 0:fb6ea61c315b 38 pc.printf(PROGNAME);
eseifert 0:fb6ea61c315b 39 // set up DAQ timer
eseifert 0:fb6ea61c315b 40 // set up DAQ timers
eseifert 0:fb6ea61c315b 41 ButtonTimer.start();
eseifert 0:fb6ea61c315b 42 ButtonTimer.reset();
eseifert 0:fb6ea61c315b 43 }
eseifert 0:fb6ea61c315b 44
eseifert 0:fb6ea61c315b 45 void showTitle()
eseifert 0:fb6ea61c315b 46 {
eseifert 0:fb6ea61c315b 47 LCDMess(LCDTITLE);
eseifert 0:fb6ea61c315b 48 wait(TITLEWAIT);
eseifert 0:fb6ea61c315b 49 return;
eseifert 0:fb6ea61c315b 50 }
eseifert 0:fb6ea61c315b 51
eseifert 0:fb6ea61c315b 52 void count_backwards(int cnt, int min){
eseifert 0:fb6ea61c315b 53 gled = !gled;
eseifert 0:fb6ea61c315b 54 pc.printf("\tT-%d\n\r", cnt);
eseifert 0:fb6ea61c315b 55 sprintf (lcdData,"tz%d",cnt);
eseifert 0:fb6ea61c315b 56 LCDMess(lcdData);
eseifert 0:fb6ea61c315b 57 wait(DATAINTERVAL);
eseifert 0:fb6ea61c315b 58 // tail recursion to correct the direction of counting
eseifert 0:fb6ea61c315b 59 if(cnt > min){
eseifert 0:fb6ea61c315b 60 count_backwards(cnt-1,min);
eseifert 0:fb6ea61c315b 61 }
eseifert 0:fb6ea61c315b 62 }
eseifert 0:fb6ea61c315b 63
eseifert 0:fb6ea61c315b 64 int main()
eseifert 0:fb6ea61c315b 65 {
eseifert 0:fb6ea61c315b 66 initialize_global_vars();
eseifert 0:fb6ea61c315b 67 showTitle();
eseifert 0:fb6ea61c315b 68 pc.printf(PROGNAME);
eseifert 0:fb6ea61c315b 69 while (true) {
eseifert 0:fb6ea61c315b 70 rled = !rled;
eseifert 0:fb6ea61c315b 71 tsidata = tsi.readPercentage();
eseifert 0:fb6ea61c315b 72 count_num = floor(tsidata*51);
eseifert 0:fb6ea61c315b 73 if(count_num > 2){
eseifert 0:fb6ea61c315b 74 pc.printf("\n Count down from: %d\n\r", count_num);
eseifert 0:fb6ea61c315b 75 sprintf (lcdData,"%d",count_num);
eseifert 0:fb6ea61c315b 76 LCDMess(lcdData);
eseifert 0:fb6ea61c315b 77 }
eseifert 0:fb6ea61c315b 78 wait(DATAINTERVAL);
eseifert 0:fb6ea61c315b 79 if (ButtonTimer > BUTTONTIME) {
eseifert 0:fb6ea61c315b 80 if(!buttons[0]){
eseifert 0:fb6ea61c315b 81 count_backwards(count_num, count_min);
eseifert 0:fb6ea61c315b 82 showTitle();
eseifert 0:fb6ea61c315b 83 }
eseifert 0:fb6ea61c315b 84 if(!buttons[1]){
eseifert 0:fb6ea61c315b 85 count_num = 0;
eseifert 0:fb6ea61c315b 86 showTitle();
eseifert 0:fb6ea61c315b 87 }
eseifert 0:fb6ea61c315b 88 }
eseifert 0:fb6ea61c315b 89 ButtonTimer.reset();
eseifert 0:fb6ea61c315b 90 }
eseifert 0:fb6ea61c315b 91 }