Dependencies: C12832 LM75B mbed
Fork of co838_driving_test_2 by
Revision 6:8510744cd8b8, committed 2016-02-26
- Comitter:
- co838_tsc23
- Date:
- Fri Feb 26 21:11:02 2016 +0000
- Parent:
- 5:34755d01f74a
- Commit message:
- Assessment 3
Changed in this revision
assessment2.cpp | Show annotated file Show diff for this revision Revisions of this file |
dtest2.cpp | Show diff for this revision Revisions of this file |
diff -r 34755d01f74a -r 8510744cd8b8 assessment2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/assessment2.cpp Fri Feb 26 21:11:02 2016 +0000 @@ -0,0 +1,106 @@ + + +#include "mbed.h" // stock MBED API +#include "C12832.h" // for the LCD +#include "LM75B.h" // for the temperature sensor + +// various globals + + +Serial host (USBTX, USBRX); // to-host UART via OpenSDAv2 + +C12832 shld_lcd (D11, D13, D12, D7, D10); // LCD on the shield (128x32) + +LM75B lm_temp (D14, D15); // temperature sensor + + +InterruptIn sw2_int (PTC6); // interrupts for the two on-board switches +InterruptIn sw3_int (PTA4); + +static volatile int sw2_trig; // switches triggered? +static volatile int sw3_trig; + + +//various interrupt handlers + +void sw2_interrupt (void) // trig 2 interrupt handler +{ + sw2_trig = 1; +} + + +void sw3_interrupt (void) //trig 3 interrupt handler +{ + sw3_trig = 1; +} + +/* + *The main method displays the temperature in F and C, the different temperature metrics can be accessed by using the two + triggers on the device SW2 and SW3. + */ +int main (void) +{ + + host.baud (38400); + + // Initialsising variable for the triggers + sw2_trig = 0; + sw3_trig = 0; + + // Initialising a variable for the different modes (F or C) + int mode = 0; + + sw2_int.mode (PullUp); + sw2_int.fall (&sw2_interrupt); + + sw3_int.mode (PullUp); + sw3_int.fall (&sw3_interrupt); + + shld_lcd.cls (); + shld_lcd.locate (1, 1); + + //Prints this to the lcd + shld_lcd.printf ("The current temperature is:"); + + + + // this loop will run forever + for (;;) { + wait (0.7); // approx 50 cycles/sec + + // every 32 cycles, or thereabouts (will be truncated) + float t = lm_temp.read (); + + //switch the temp to C + if (sw2_trig) { + mode = 0; + sw2_trig = 0; + } + + //switch the temp to F + if (sw3_trig) { + mode = 1; + sw3_trig = 0; + } + + // display the temp to the serial HOST and LCD if the temp is in C + if(mode == 0){ + + host.printf ("Temperature: %.2f celsuis \r\n", t); + shld_lcd.locate (1, 10); + shld_lcd.printf ("%.2f C", t); + + } + // display the temp to the serial HOST and LCD if the temp is in F + else if (mode == 1){ + + float tf = (t * 9.0/5.0 + 32); + host.printf ("Temperature: %.2f fehrenheit \r\n", tf); + shld_lcd.locate (1, 10); + shld_lcd.printf ("%.2f F", tf); + + + } + + } +}
diff -r 34755d01f74a -r 8510744cd8b8 dtest2.cpp --- a/dtest2.cpp Wed Jan 27 01:44:35 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,167 +0,0 @@ -/* - * dtest2.cpp -- driving test for CO657 (interacting with the shield) - * Copyright (C) 2015 Fred Barnes, University of Kent <frmb@kent.ac.uk> - * GPL >= 2.0 - */ - -#include "mbed.h" /* stock MBED API */ -#include "C12832.h" /* for the LCD */ -#include "LM75B.h" /* for the temperature sensor */ - -/* assorted globals */ - -DigitalOut r_led (LED1); /* connections for RGB LED */ -DigitalOut g_led (LED2); -DigitalOut b_led (LED3); - -DigitalOut xr_led (PTA2); -DigitalOut xg_led (PTC4); -DigitalOut xb_led (PTA0); - -Serial host (USBTX, USBRX); /* to-host UART via OpenSDAv2 */ - -InterruptIn sw2_int (PTC6); /* interrupts for the two on-board switches */ -InterruptIn sw3_int (PTA4); - -Ticker rgb_tick; /* timer for driving the LED */ - -C12832 shld_lcd (D11, D13, D12, D7, D10); /* LCD on the shield (128x32) */ - -LM75B lm_temp (D14, D15); /* temperature sensor */ - -static volatile uint16_t counters[3]; /* red, green, blue */ -static volatile uint8_t bright[3]; - -static volatile int sw2_trig; /* switches triggered? */ -static volatile int sw3_trig; - - -/* - * turns offset [0-311] into brightness level [0-255-255-0]. - */ -uint8_t brilvl (int offset) -{ - while (offset > 311) { - offset -= 312; - } - if (offset < 52) { - return (uint8_t)(offset * 5); - } else if (offset < 156) { - return 255; - } else if (offset < 208) { - return (uint8_t)((207 - offset) * 5); - } - return 0; -} - - -/* - * resets the various counters to their initial state - */ -void reset_counters (void) -{ - for (int i=0; i<3; i++) { - counters[i] = 0; - bright[i] = brilvl (i * 104); - } -} - - -/* - * interrupt handlers - */ -void rgb_tick_interrupt (void) -{ - for (int i=0; i<3; i++) { - counters[i] += (uint16_t)bright[i]; - } - - r_led = ((counters[0] >> 8) & 1) ^ 1; - g_led = ((counters[1] >> 8) & 1) ^ 1; - b_led = ((counters[2] >> 8) & 1) ^ 1; - - xr_led = ((counters[0] >> 8) & 1); - xg_led = ((counters[1] >> 8) & 1); - xb_led = ((counters[2] >> 8) & 1); - - for (int i=0; i<3; i++) { - counters[i] &= 0xff; - } -} - - -void sw2_interrupt (void) -{ - sw2_trig = 1; -} - - -void sw3_interrupt (void) -{ - sw3_trig = 1; -} - - - - -/* - * start here - */ -int main (void) -{ - int ctr; - - host.baud (38400); - host.printf ("Hello, FRDM-K64F driving-test world!\r\n"); - - sw2_trig = 0; - sw3_trig = 0; - - reset_counters (); - - shld_lcd.cls (); - shld_lcd.locate (1, 1); - shld_lcd.printf ("Hello, CO657!"); - - rgb_tick.attach (&rgb_tick_interrupt, 0.001); - ctr = 0; - - xr_led = 0; - - sw2_int.mode (PullUp); - sw2_int.fall (&sw2_interrupt); - - sw3_int.mode (PullUp); - sw3_int.fall (&sw3_interrupt); - - /* and then forever.. */ - for (;;) { - wait (0.02); /* approx 50 cycles/sec */ - - if ((ctr & 0x1f) == 0) { - /* every 32 cycles, or thereabouts (will be truncated) */ - float t = lm_temp.read (); - - shld_lcd.locate (1, 10); - shld_lcd.printf ("Temp: %.2f", t); - } - - if (sw2_trig) { - ctr += 104; - sw2_trig = 0; - } - if (sw3_trig) { - ctr += 208; - sw3_trig = 0; - } - - ctr++; - if (ctr > 311) { - ctr -= 312; - } - for (int i=0; i<3; i++) { - bright[i] = brilvl (ctr + (i * 104)); - } - } -} -