![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
thermostat
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed
Fork of mythermostat by
main.cpp@2:58d85409f7ff, 2013-01-31 (annotated)
- Committer:
- 4180_1
- Date:
- Thu Jan 31 03:15:20 2013 +0000
- Revision:
- 2:58d85409f7ff
- Parent:
- 1:768b8bd42e33
- Child:
- 3:346ef671ef28
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 2:58d85409f7ff | 1 | // skeleton code for ECE 2036 thermostat lab |
4180_1 | 2:58d85409f7ff | 2 | // code must be added by students |
4180_1 | 0:cc87c48aa43c | 3 | #include "mbed.h" |
4180_1 | 2:58d85409f7ff | 4 | #include "TMP36.h" |
4180_1 | 2:58d85409f7ff | 5 | #include "SDFileSystem.h" |
4180_1 | 2:58d85409f7ff | 6 | #include "TextLCD.h" |
4180_1 | 0:cc87c48aa43c | 7 | #include "PinDetect.h" |
4180_1 | 2:58d85409f7ff | 8 | #include "Speaker.h" |
4180_1 | 2:58d85409f7ff | 9 | // must add your new class code to the project file Shiftbrite.h |
4180_1 | 2:58d85409f7ff | 10 | #include "Shiftbrite.h" |
4180_1 | 2:58d85409f7ff | 11 | |
4180_1 | 2:58d85409f7ff | 12 | // use class to setup temperature sensor pins |
4180_1 | 2:58d85409f7ff | 13 | TMP36 myTMP36(p15); //Analog in |
4180_1 | 0:cc87c48aa43c | 14 | |
4180_1 | 2:58d85409f7ff | 15 | // use class to setup microSD card filesystem |
4180_1 | 2:58d85409f7ff | 16 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
4180_1 | 2:58d85409f7ff | 17 | |
4180_1 | 2:58d85409f7ff | 18 | // use class to setup the LCD |
4180_1 | 2:58d85409f7ff | 19 | TextLCD myLCD(p22, p23, p24, p25, p26, p27); // rs, e, d4-d7 |
4180_1 | 2:58d85409f7ff | 20 | |
4180_1 | 2:58d85409f7ff | 21 | // use class to setup pushbuttons pins |
4180_1 | 2:58d85409f7ff | 22 | PinDetect pb1(p28); |
4180_1 | 2:58d85409f7ff | 23 | PinDetect pb2(p29); |
4180_1 | 2:58d85409f7ff | 24 | PinDetect pb3(p30); |
4180_1 | 2:58d85409f7ff | 25 | |
4180_1 | 2:58d85409f7ff | 26 | // use class to setup speaker pin |
4180_1 | 2:58d85409f7ff | 27 | Speaker mySpeaker(p21); //PWM out |
4180_1 | 0:cc87c48aa43c | 28 | |
4180_1 | 2:58d85409f7ff | 29 | // use class to setup Shiftbrite pins |
4180_1 | 2:58d85409f7ff | 30 | Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci |
4180_1 | 2:58d85409f7ff | 31 | |
4180_1 | 2:58d85409f7ff | 32 | // use class to setup Mbed's four on-board LEDs |
4180_1 | 2:58d85409f7ff | 33 | DigitalOut myLED1(LED1); |
4180_1 | 2:58d85409f7ff | 34 | DigitalOut myLED2(LED2); |
4180_1 | 2:58d85409f7ff | 35 | DigitalOut myLED3(LED3); |
4180_1 | 2:58d85409f7ff | 36 | DigitalOut myLED4(LED4); |
4180_1 | 2:58d85409f7ff | 37 | |
4180_1 | 2:58d85409f7ff | 38 | // heat or code mode jumper - removed when pushbuttons added |
4180_1 | 2:58d85409f7ff | 39 | DigitalIn jumper_wire(p14); |
4180_1 | 0:cc87c48aa43c | 40 | |
4180_1 | 2:58d85409f7ff | 41 | //also setting any unused analog input pins to digital outputs reduces A/D noise a bit |
4180_1 | 2:58d85409f7ff | 42 | //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/ |
4180_1 | 2:58d85409f7ff | 43 | DigitalOut P16(p16); |
4180_1 | 2:58d85409f7ff | 44 | DigitalOut P17(p17); |
4180_1 | 2:58d85409f7ff | 45 | DigitalOut P18(p18); |
4180_1 | 2:58d85409f7ff | 46 | DigitalOut P19(p19); |
4180_1 | 2:58d85409f7ff | 47 | DigitalOut P20(p20); |
4180_1 | 2:58d85409f7ff | 48 | |
4180_1 | 2:58d85409f7ff | 49 | |
4180_1 | 2:58d85409f7ff | 50 | |
4180_1 | 2:58d85409f7ff | 51 | |
4180_1 | 2:58d85409f7ff | 52 | // Global variables used in callbacks and main program |
4180_1 | 2:58d85409f7ff | 53 | // C variables in interrupt routines should use volatile keyword |
4180_1 | 2:58d85409f7ff | 54 | int volatile heat_setting=78; // heat to temp |
4180_1 | 2:58d85409f7ff | 55 | int volatile cool_setting=68; // cool to temp |
4180_1 | 2:58d85409f7ff | 56 | bool volatile mode=false; // heat or cool mpde |
4180_1 | 0:cc87c48aa43c | 57 | |
4180_1 | 1:768b8bd42e33 | 58 | // Callback routine is interrupt activated by a debounced pb1 hit |
4180_1 | 2:58d85409f7ff | 59 | void pb1_hit_callback (void) |
4180_1 | 2:58d85409f7ff | 60 | { |
4180_1 | 2:58d85409f7ff | 61 | // ADD CODE HERE |
4180_1 | 0:cc87c48aa43c | 62 | } |
4180_1 | 1:768b8bd42e33 | 63 | // Callback routine is interrupt activated by a debounced pb2 hit |
4180_1 | 2:58d85409f7ff | 64 | void pb2_hit_callback (void) |
4180_1 | 2:58d85409f7ff | 65 | { |
4180_1 | 2:58d85409f7ff | 66 | // ADD CODE HERE |
4180_1 | 2:58d85409f7ff | 67 | } |
4180_1 | 2:58d85409f7ff | 68 | // Callback routine is interrupt activated by a debounced pb3 hit |
4180_1 | 2:58d85409f7ff | 69 | void pb3_hit_callback (void) |
4180_1 | 2:58d85409f7ff | 70 | { |
4180_1 | 2:58d85409f7ff | 71 | // ADD CODE HERE |
4180_1 | 1:768b8bd42e33 | 72 | } |
4180_1 | 2:58d85409f7ff | 73 | |
4180_1 | 0:cc87c48aa43c | 74 | |
4180_1 | 2:58d85409f7ff | 75 | int main() |
4180_1 | 2:58d85409f7ff | 76 | { |
4180_1 | 2:58d85409f7ff | 77 | float Current_temp=0.0; |
4180_1 | 2:58d85409f7ff | 78 | |
4180_1 | 2:58d85409f7ff | 79 | // Use internal pullups for the three pushbuttons |
4180_1 | 2:58d85409f7ff | 80 | pb1.mode(PullUp); |
4180_1 | 1:768b8bd42e33 | 81 | pb2.mode(PullUp); |
4180_1 | 2:58d85409f7ff | 82 | pb3.mode(PullUp); |
4180_1 | 0:cc87c48aa43c | 83 | // Delay for initial pullup to take effect |
4180_1 | 0:cc87c48aa43c | 84 | wait(.01); |
4180_1 | 1:768b8bd42e33 | 85 | // Setup Interrupt callback functions for a pb hit |
4180_1 | 1:768b8bd42e33 | 86 | pb1.attach_deasserted(&pb1_hit_callback); |
4180_1 | 1:768b8bd42e33 | 87 | pb2.attach_deasserted(&pb2_hit_callback); |
4180_1 | 2:58d85409f7ff | 88 | pb3.attach_deasserted(&pb2_hit_callback); |
4180_1 | 1:768b8bd42e33 | 89 | // Start sampling pb inputs using interrupts |
4180_1 | 1:768b8bd42e33 | 90 | pb1.setSampleFrequency(); |
4180_1 | 1:768b8bd42e33 | 91 | pb2.setSampleFrequency(); |
4180_1 | 2:58d85409f7ff | 92 | pb2.setSampleFrequency(); |
4180_1 | 2:58d85409f7ff | 93 | // pushbuttons now setup and running |
4180_1 | 2:58d85409f7ff | 94 | |
4180_1 | 2:58d85409f7ff | 95 | |
4180_1 | 2:58d85409f7ff | 96 | // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE |
4180_1 | 2:58d85409f7ff | 97 | // since all this compiles - the needed *.h files for these are in the project |
4180_1 | 2:58d85409f7ff | 98 | // |
4180_1 | 2:58d85409f7ff | 99 | Current_temp = myTMP36; |
4180_1 | 2:58d85409f7ff | 100 | printf("Hello PC World\n\r"); // need terminal application running on PC to see this |
4180_1 | 2:58d85409f7ff | 101 | myLCD.printf("Hello LCD World"); // LCD |
4180_1 | 2:58d85409f7ff | 102 | mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz |
4180_1 | 2:58d85409f7ff | 103 | myShiftbrite.write( 0, 50 ,0); // Green RGB LED |
4180_1 | 2:58d85409f7ff | 104 | // SD card write file example - prints error message on PC when running until SD card hooked up |
4180_1 | 2:58d85409f7ff | 105 | FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD |
4180_1 | 2:58d85409f7ff | 106 | if(fp == NULL) { |
4180_1 | 2:58d85409f7ff | 107 | error("Could not open file for write\n"); |
4180_1 | 2:58d85409f7ff | 108 | } |
4180_1 | 2:58d85409f7ff | 109 | fprintf(fp, "Hello SD Card World!"); // write SD |
4180_1 | 2:58d85409f7ff | 110 | fclose(fp); // close SD card |
4180_1 | 2:58d85409f7ff | 111 | // |
4180_1 | 2:58d85409f7ff | 112 | // end I/O examples |
4180_1 | 2:58d85409f7ff | 113 | |
4180_1 | 2:58d85409f7ff | 114 | |
4180_1 | 2:58d85409f7ff | 115 | |
4180_1 | 2:58d85409f7ff | 116 | |
4180_1 | 2:58d85409f7ff | 117 | // State machine code below will need changes |
4180_1 | 0:cc87c48aa43c | 118 | while (1) { |
4180_1 | 2:58d85409f7ff | 119 | { |
4180_1 | 2:58d85409f7ff | 120 | enum Statetype { Heat_off = 0, Heat_on }; |
4180_1 | 2:58d85409f7ff | 121 | Statetype state = Heat_off; |
4180_1 | 2:58d85409f7ff | 122 | while(1) { |
4180_1 | 2:58d85409f7ff | 123 | switch (state) { |
4180_1 | 2:58d85409f7ff | 124 | case Heat_off: |
4180_1 | 2:58d85409f7ff | 125 | myLED2 = 0; |
4180_1 | 2:58d85409f7ff | 126 | state = Heat_on; |
4180_1 | 2:58d85409f7ff | 127 | break; |
4180_1 | 2:58d85409f7ff | 128 | case Heat_on: |
4180_1 | 2:58d85409f7ff | 129 | myLED2 = 1; |
4180_1 | 2:58d85409f7ff | 130 | state = Heat_off; |
4180_1 | 2:58d85409f7ff | 131 | break; |
4180_1 | 2:58d85409f7ff | 132 | } |
4180_1 | 2:58d85409f7ff | 133 | wait(0.33); |
4180_1 | 2:58d85409f7ff | 134 | } |
4180_1 | 2:58d85409f7ff | 135 | } |
4180_1 | 0:cc87c48aa43c | 136 | } |
4180_1 | 0:cc87c48aa43c | 137 | } |