
IoT Assessment 3
Dependencies: C12832 LM75B mbed
Fork of co838_driving_test_2 by
assessment2.cpp@6:8510744cd8b8, 2016-02-26 (annotated)
- Committer:
- co838_tsc23
- Date:
- Fri Feb 26 21:11:02 2016 +0000
- Revision:
- 6:8510744cd8b8
Assessment 3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co838_tsc23 | 6:8510744cd8b8 | 1 | |
co838_tsc23 | 6:8510744cd8b8 | 2 | |
co838_tsc23 | 6:8510744cd8b8 | 3 | #include "mbed.h" // stock MBED API |
co838_tsc23 | 6:8510744cd8b8 | 4 | #include "C12832.h" // for the LCD |
co838_tsc23 | 6:8510744cd8b8 | 5 | #include "LM75B.h" // for the temperature sensor |
co838_tsc23 | 6:8510744cd8b8 | 6 | |
co838_tsc23 | 6:8510744cd8b8 | 7 | // various globals |
co838_tsc23 | 6:8510744cd8b8 | 8 | |
co838_tsc23 | 6:8510744cd8b8 | 9 | |
co838_tsc23 | 6:8510744cd8b8 | 10 | Serial host (USBTX, USBRX); // to-host UART via OpenSDAv2 |
co838_tsc23 | 6:8510744cd8b8 | 11 | |
co838_tsc23 | 6:8510744cd8b8 | 12 | C12832 shld_lcd (D11, D13, D12, D7, D10); // LCD on the shield (128x32) |
co838_tsc23 | 6:8510744cd8b8 | 13 | |
co838_tsc23 | 6:8510744cd8b8 | 14 | LM75B lm_temp (D14, D15); // temperature sensor |
co838_tsc23 | 6:8510744cd8b8 | 15 | |
co838_tsc23 | 6:8510744cd8b8 | 16 | |
co838_tsc23 | 6:8510744cd8b8 | 17 | InterruptIn sw2_int (PTC6); // interrupts for the two on-board switches |
co838_tsc23 | 6:8510744cd8b8 | 18 | InterruptIn sw3_int (PTA4); |
co838_tsc23 | 6:8510744cd8b8 | 19 | |
co838_tsc23 | 6:8510744cd8b8 | 20 | static volatile int sw2_trig; // switches triggered? |
co838_tsc23 | 6:8510744cd8b8 | 21 | static volatile int sw3_trig; |
co838_tsc23 | 6:8510744cd8b8 | 22 | |
co838_tsc23 | 6:8510744cd8b8 | 23 | |
co838_tsc23 | 6:8510744cd8b8 | 24 | //various interrupt handlers |
co838_tsc23 | 6:8510744cd8b8 | 25 | |
co838_tsc23 | 6:8510744cd8b8 | 26 | void sw2_interrupt (void) // trig 2 interrupt handler |
co838_tsc23 | 6:8510744cd8b8 | 27 | { |
co838_tsc23 | 6:8510744cd8b8 | 28 | sw2_trig = 1; |
co838_tsc23 | 6:8510744cd8b8 | 29 | } |
co838_tsc23 | 6:8510744cd8b8 | 30 | |
co838_tsc23 | 6:8510744cd8b8 | 31 | |
co838_tsc23 | 6:8510744cd8b8 | 32 | void sw3_interrupt (void) //trig 3 interrupt handler |
co838_tsc23 | 6:8510744cd8b8 | 33 | { |
co838_tsc23 | 6:8510744cd8b8 | 34 | sw3_trig = 1; |
co838_tsc23 | 6:8510744cd8b8 | 35 | } |
co838_tsc23 | 6:8510744cd8b8 | 36 | |
co838_tsc23 | 6:8510744cd8b8 | 37 | /* |
co838_tsc23 | 6:8510744cd8b8 | 38 | *The main method displays the temperature in F and C, the different temperature metrics can be accessed by using the two |
co838_tsc23 | 6:8510744cd8b8 | 39 | triggers on the device SW2 and SW3. |
co838_tsc23 | 6:8510744cd8b8 | 40 | */ |
co838_tsc23 | 6:8510744cd8b8 | 41 | int main (void) |
co838_tsc23 | 6:8510744cd8b8 | 42 | { |
co838_tsc23 | 6:8510744cd8b8 | 43 | |
co838_tsc23 | 6:8510744cd8b8 | 44 | host.baud (38400); |
co838_tsc23 | 6:8510744cd8b8 | 45 | |
co838_tsc23 | 6:8510744cd8b8 | 46 | // Initialsising variable for the triggers |
co838_tsc23 | 6:8510744cd8b8 | 47 | sw2_trig = 0; |
co838_tsc23 | 6:8510744cd8b8 | 48 | sw3_trig = 0; |
co838_tsc23 | 6:8510744cd8b8 | 49 | |
co838_tsc23 | 6:8510744cd8b8 | 50 | // Initialising a variable for the different modes (F or C) |
co838_tsc23 | 6:8510744cd8b8 | 51 | int mode = 0; |
co838_tsc23 | 6:8510744cd8b8 | 52 | |
co838_tsc23 | 6:8510744cd8b8 | 53 | sw2_int.mode (PullUp); |
co838_tsc23 | 6:8510744cd8b8 | 54 | sw2_int.fall (&sw2_interrupt); |
co838_tsc23 | 6:8510744cd8b8 | 55 | |
co838_tsc23 | 6:8510744cd8b8 | 56 | sw3_int.mode (PullUp); |
co838_tsc23 | 6:8510744cd8b8 | 57 | sw3_int.fall (&sw3_interrupt); |
co838_tsc23 | 6:8510744cd8b8 | 58 | |
co838_tsc23 | 6:8510744cd8b8 | 59 | shld_lcd.cls (); |
co838_tsc23 | 6:8510744cd8b8 | 60 | shld_lcd.locate (1, 1); |
co838_tsc23 | 6:8510744cd8b8 | 61 | |
co838_tsc23 | 6:8510744cd8b8 | 62 | //Prints this to the lcd |
co838_tsc23 | 6:8510744cd8b8 | 63 | shld_lcd.printf ("The current temperature is:"); |
co838_tsc23 | 6:8510744cd8b8 | 64 | |
co838_tsc23 | 6:8510744cd8b8 | 65 | |
co838_tsc23 | 6:8510744cd8b8 | 66 | |
co838_tsc23 | 6:8510744cd8b8 | 67 | // this loop will run forever |
co838_tsc23 | 6:8510744cd8b8 | 68 | for (;;) { |
co838_tsc23 | 6:8510744cd8b8 | 69 | wait (0.7); // approx 50 cycles/sec |
co838_tsc23 | 6:8510744cd8b8 | 70 | |
co838_tsc23 | 6:8510744cd8b8 | 71 | // every 32 cycles, or thereabouts (will be truncated) |
co838_tsc23 | 6:8510744cd8b8 | 72 | float t = lm_temp.read (); |
co838_tsc23 | 6:8510744cd8b8 | 73 | |
co838_tsc23 | 6:8510744cd8b8 | 74 | //switch the temp to C |
co838_tsc23 | 6:8510744cd8b8 | 75 | if (sw2_trig) { |
co838_tsc23 | 6:8510744cd8b8 | 76 | mode = 0; |
co838_tsc23 | 6:8510744cd8b8 | 77 | sw2_trig = 0; |
co838_tsc23 | 6:8510744cd8b8 | 78 | } |
co838_tsc23 | 6:8510744cd8b8 | 79 | |
co838_tsc23 | 6:8510744cd8b8 | 80 | //switch the temp to F |
co838_tsc23 | 6:8510744cd8b8 | 81 | if (sw3_trig) { |
co838_tsc23 | 6:8510744cd8b8 | 82 | mode = 1; |
co838_tsc23 | 6:8510744cd8b8 | 83 | sw3_trig = 0; |
co838_tsc23 | 6:8510744cd8b8 | 84 | } |
co838_tsc23 | 6:8510744cd8b8 | 85 | |
co838_tsc23 | 6:8510744cd8b8 | 86 | // display the temp to the serial HOST and LCD if the temp is in C |
co838_tsc23 | 6:8510744cd8b8 | 87 | if(mode == 0){ |
co838_tsc23 | 6:8510744cd8b8 | 88 | |
co838_tsc23 | 6:8510744cd8b8 | 89 | host.printf ("Temperature: %.2f celsuis \r\n", t); |
co838_tsc23 | 6:8510744cd8b8 | 90 | shld_lcd.locate (1, 10); |
co838_tsc23 | 6:8510744cd8b8 | 91 | shld_lcd.printf ("%.2f C", t); |
co838_tsc23 | 6:8510744cd8b8 | 92 | |
co838_tsc23 | 6:8510744cd8b8 | 93 | } |
co838_tsc23 | 6:8510744cd8b8 | 94 | // display the temp to the serial HOST and LCD if the temp is in F |
co838_tsc23 | 6:8510744cd8b8 | 95 | else if (mode == 1){ |
co838_tsc23 | 6:8510744cd8b8 | 96 | |
co838_tsc23 | 6:8510744cd8b8 | 97 | float tf = (t * 9.0/5.0 + 32); |
co838_tsc23 | 6:8510744cd8b8 | 98 | host.printf ("Temperature: %.2f fehrenheit \r\n", tf); |
co838_tsc23 | 6:8510744cd8b8 | 99 | shld_lcd.locate (1, 10); |
co838_tsc23 | 6:8510744cd8b8 | 100 | shld_lcd.printf ("%.2f F", tf); |
co838_tsc23 | 6:8510744cd8b8 | 101 | |
co838_tsc23 | 6:8510744cd8b8 | 102 | |
co838_tsc23 | 6:8510744cd8b8 | 103 | } |
co838_tsc23 | 6:8510744cd8b8 | 104 | |
co838_tsc23 | 6:8510744cd8b8 | 105 | } |
co838_tsc23 | 6:8510744cd8b8 | 106 | } |