HW_5.2

Dependencies:   SLCD mbed

Fork of lightsense_kl46z_basic by Stanley Cohen

Committer:
scohennm
Date:
Fri Sep 05 02:26:47 2014 +0000
Revision:
1:51f8c2b04ce2
Parent:
0:e23fffd4b9a7
Child:
2:c016448d89b2
Use of KL46z PWM LCD and LED's  for NMHU 140904

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 0:e23fffd4b9a7 1 #include "mbed.h"
scohennm 1:51f8c2b04ce2 2 #include "SLCD.h"
scohennm 1:51f8c2b04ce2 3
scohennm 1:51f8c2b04ce2 4 // An example of C++ abuse 140904 sc
scohennm 1:51f8c2b04ce2 5 //#define PRINTDEBUG
scohennm 1:51f8c2b04ce2 6 #define PROGNAME "blink_kl46z_states v1\n\r"
scohennm 0:e23fffd4b9a7 7 #define LEDON false
scohennm 0:e23fffd4b9a7 8 #define LEDOFF true
scohennm 1:51f8c2b04ce2 9 #define PWMDWELL 50 // milliseconds
scohennm 1:51f8c2b04ce2 10 #define DFDELTA 0.01
scohennm 1:51f8c2b04ce2 11 #define PWMTIME 1 // ms (kHz
scohennm 1:51f8c2b04ce2 12 #define LCDLEN 10
scohennm 1:51f8c2b04ce2 13 #define RMPUP true
scohennm 1:51f8c2b04ce2 14 #define RMPDWN false
scohennm 1:51f8c2b04ce2 15 #define NUMSTATES 2
scohennm 0:e23fffd4b9a7 16
scohennm 1:51f8c2b04ce2 17 float dutyFactor = 0.0;
scohennm 1:51f8c2b04ce2 18 PwmOut greenColor(LED_GREEN);
scohennm 1:51f8c2b04ce2 19 PwmOut redColor(LED_RED);
scohennm 1:51f8c2b04ce2 20 SLCD slcd; //define LCD display
scohennm 1:51f8c2b04ce2 21 float rampDirection[NUMSTATES] = {-DFDELTA, DFDELTA};
scohennm 1:51f8c2b04ce2 22
scohennm 1:51f8c2b04ce2 23 Serial pc(USBTX, USBRX);
scohennm 1:51f8c2b04ce2 24
scohennm 1:51f8c2b04ce2 25 void LCDMess(char *lMess){
scohennm 1:51f8c2b04ce2 26 slcd.Home();
scohennm 1:51f8c2b04ce2 27 slcd.clear();
scohennm 1:51f8c2b04ce2 28 slcd.printf(lMess);
scohennm 1:51f8c2b04ce2 29 }
scohennm 0:e23fffd4b9a7 30
scohennm 0:e23fffd4b9a7 31 int main() {
scohennm 1:51f8c2b04ce2 32 char lcdData[LCDLEN];
scohennm 1:51f8c2b04ce2 33 greenColor.period_ms(PWMTIME); // set the frequency of the pulse train
scohennm 1:51f8c2b04ce2 34 redColor.period_ms(PWMTIME);
scohennm 1:51f8c2b04ce2 35 int rampstate = RMPUP;
scohennm 1:51f8c2b04ce2 36 int numSteps;
scohennm 1:51f8c2b04ce2 37 float workingDelta;
scohennm 1:51f8c2b04ce2 38 int i=0;
scohennm 1:51f8c2b04ce2 39
scohennm 1:51f8c2b04ce2 40 pc.printf(PROGNAME);
scohennm 1:51f8c2b04ce2 41
scohennm 1:51f8c2b04ce2 42 workingDelta = rampDirection[rampstate];
scohennm 1:51f8c2b04ce2 43 numSteps = (int)(1.0/workingDelta);
scohennm 0:e23fffd4b9a7 44 while(true) {
scohennm 1:51f8c2b04ce2 45 i++;
scohennm 1:51f8c2b04ce2 46 redColor.write(dutyFactor);
scohennm 1:51f8c2b04ce2 47 greenColor.write(1.0 - dutyFactor);
scohennm 1:51f8c2b04ce2 48 dutyFactor += workingDelta;
scohennm 1:51f8c2b04ce2 49 sprintf (lcdData,"%4.3f",dutyFactor);
scohennm 1:51f8c2b04ce2 50 LCDMess(lcdData);
scohennm 1:51f8c2b04ce2 51 #ifdef PRINTDEBUG
scohennm 1:51f8c2b04ce2 52 pc.printf("i= %d dutyfactor = %5.4f workingDelta %5.4f \n\r", i, dutyFactor, workingDelta);
scohennm 1:51f8c2b04ce2 53 #endif
scohennm 1:51f8c2b04ce2 54 if (!(i % numSteps)){
scohennm 1:51f8c2b04ce2 55 i=0;
scohennm 1:51f8c2b04ce2 56 rampstate = !rampstate;
scohennm 1:51f8c2b04ce2 57 workingDelta = rampDirection[rampstate];
scohennm 1:51f8c2b04ce2 58 }
scohennm 1:51f8c2b04ce2 59 wait_ms(PWMDWELL);
scohennm 0:e23fffd4b9a7 60 }
scohennm 0:e23fffd4b9a7 61 }