example skeleton code for student lab project

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed

Fork of mymbedthermostat by jim hamblen

Committer:
4180_1
Date:
Thu Jan 23 16:47:05 2014 +0000
Revision:
4:9a4d22a279b3
Parent:
3:346ef671ef28
ver 1.0

Who changed what in which revision?

UserRevisionLine numberNew 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 4:9a4d22a279b3 6 #include "uLCD_4DGL.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 4:9a4d22a279b3 18 // use class to setup the Color LCD
4180_1 4:9a4d22a279b3 19 uLCD_4DGL uLCD(p28, p27, p29); // create a global uLCD object
4180_1 2:58d85409f7ff 20
4180_1 2:58d85409f7ff 21 // use class to setup pushbuttons pins
4180_1 4:9a4d22a279b3 22 PinDetect pb1(p23);
4180_1 4:9a4d22a279b3 23 PinDetect pb2(p24);
4180_1 4:9a4d22a279b3 24 PinDetect pb3(p25);
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 4:9a4d22a279b3 38
4180_1 0:cc87c48aa43c 39
4180_1 2:58d85409f7ff 40 //also setting any unused analog input pins to digital outputs reduces A/D noise a bit
4180_1 2:58d85409f7ff 41 //see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/
4180_1 2:58d85409f7ff 42 DigitalOut P16(p16);
4180_1 2:58d85409f7ff 43 DigitalOut P17(p17);
4180_1 2:58d85409f7ff 44 DigitalOut P18(p18);
4180_1 2:58d85409f7ff 45 DigitalOut P19(p19);
4180_1 2:58d85409f7ff 46 DigitalOut P20(p20);
4180_1 2:58d85409f7ff 47
4180_1 2:58d85409f7ff 48
4180_1 2:58d85409f7ff 49
4180_1 2:58d85409f7ff 50
4180_1 2:58d85409f7ff 51 // Global variables used in callbacks and main program
4180_1 2:58d85409f7ff 52 // C variables in interrupt routines should use volatile keyword
4180_1 2:58d85409f7ff 53 int volatile heat_setting=78; // heat to temp
4180_1 2:58d85409f7ff 54 int volatile cool_setting=68; // cool to temp
4180_1 4:9a4d22a279b3 55 bool volatile mode=false; // heat or cool mode
4180_1 0:cc87c48aa43c 56
4180_1 1:768b8bd42e33 57 // Callback routine is interrupt activated by a debounced pb1 hit
4180_1 2:58d85409f7ff 58 void pb1_hit_callback (void)
4180_1 2:58d85409f7ff 59 {
4180_1 2:58d85409f7ff 60 // ADD CODE HERE
4180_1 0:cc87c48aa43c 61 }
4180_1 1:768b8bd42e33 62 // Callback routine is interrupt activated by a debounced pb2 hit
4180_1 2:58d85409f7ff 63 void pb2_hit_callback (void)
4180_1 2:58d85409f7ff 64 {
4180_1 2:58d85409f7ff 65 // ADD CODE HERE
4180_1 2:58d85409f7ff 66 }
4180_1 2:58d85409f7ff 67 // Callback routine is interrupt activated by a debounced pb3 hit
4180_1 2:58d85409f7ff 68 void pb3_hit_callback (void)
4180_1 2:58d85409f7ff 69 {
4180_1 2:58d85409f7ff 70 // ADD CODE HERE
4180_1 1:768b8bd42e33 71 }
4180_1 2:58d85409f7ff 72
4180_1 0:cc87c48aa43c 73
4180_1 2:58d85409f7ff 74 int main()
4180_1 2:58d85409f7ff 75 {
4180_1 2:58d85409f7ff 76 float Current_temp=0.0;
4180_1 2:58d85409f7ff 77
4180_1 2:58d85409f7ff 78 // Use internal pullups for the three pushbuttons
4180_1 2:58d85409f7ff 79 pb1.mode(PullUp);
4180_1 1:768b8bd42e33 80 pb2.mode(PullUp);
4180_1 2:58d85409f7ff 81 pb3.mode(PullUp);
4180_1 0:cc87c48aa43c 82 // Delay for initial pullup to take effect
4180_1 0:cc87c48aa43c 83 wait(.01);
4180_1 1:768b8bd42e33 84 // Setup Interrupt callback functions for a pb hit
4180_1 1:768b8bd42e33 85 pb1.attach_deasserted(&pb1_hit_callback);
4180_1 1:768b8bd42e33 86 pb2.attach_deasserted(&pb2_hit_callback);
4180_1 3:346ef671ef28 87 pb3.attach_deasserted(&pb3_hit_callback);
4180_1 1:768b8bd42e33 88 // Start sampling pb inputs using interrupts
4180_1 1:768b8bd42e33 89 pb1.setSampleFrequency();
4180_1 1:768b8bd42e33 90 pb2.setSampleFrequency();
4180_1 3:346ef671ef28 91 pb3.setSampleFrequency();
4180_1 2:58d85409f7ff 92 // pushbuttons now setup and running
4180_1 2:58d85409f7ff 93
4180_1 2:58d85409f7ff 94
4180_1 2:58d85409f7ff 95 // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE
4180_1 2:58d85409f7ff 96 // since all this compiles - the needed *.h files for these are in the project
4180_1 2:58d85409f7ff 97 //
4180_1 4:9a4d22a279b3 98 Current_temp = myTMP36; //Read temp sensor
4180_1 4:9a4d22a279b3 99 printf("Hello PC World\n\r"); // need terminal application running on PC to see this output
4180_1 4:9a4d22a279b3 100 uLCD.printf("\n\rHello LCD World\n\r"); // LCD
4180_1 2:58d85409f7ff 101 mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz
4180_1 2:58d85409f7ff 102 myShiftbrite.write( 0, 50 ,0); // Green RGB LED
4180_1 2:58d85409f7ff 103 // SD card write file example - prints error message on PC when running until SD card hooked up
4180_1 4:9a4d22a279b3 104 // Delete to avoid run time error
4180_1 3:346ef671ef28 105 mkdir("/sd/mydir", 0777); // set up directory and permissions
4180_1 2:58d85409f7ff 106 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD
4180_1 2:58d85409f7ff 107 if(fp == NULL) {
4180_1 2:58d85409f7ff 108 error("Could not open file for write\n");
4180_1 2:58d85409f7ff 109 }
4180_1 2:58d85409f7ff 110 fprintf(fp, "Hello SD Card World!"); // write SD
4180_1 2:58d85409f7ff 111 fclose(fp); // close SD card
4180_1 2:58d85409f7ff 112 //
4180_1 2:58d85409f7ff 113 // end I/O examples
4180_1 2:58d85409f7ff 114
4180_1 2:58d85409f7ff 115
4180_1 2:58d85409f7ff 116
4180_1 2:58d85409f7ff 117
4180_1 3:346ef671ef28 118 // State machine code below will need changes and additions
4180_1 0:cc87c48aa43c 119 while (1) {
4180_1 2:58d85409f7ff 120 {
4180_1 2:58d85409f7ff 121 enum Statetype { Heat_off = 0, Heat_on };
4180_1 2:58d85409f7ff 122 Statetype state = Heat_off;
4180_1 2:58d85409f7ff 123 while(1) {
4180_1 2:58d85409f7ff 124 switch (state) {
4180_1 2:58d85409f7ff 125 case Heat_off:
4180_1 3:346ef671ef28 126 myLED4 = 0;
4180_1 2:58d85409f7ff 127 state = Heat_on;
4180_1 2:58d85409f7ff 128 break;
4180_1 2:58d85409f7ff 129 case Heat_on:
4180_1 3:346ef671ef28 130 myLED4 = 1;
4180_1 2:58d85409f7ff 131 state = Heat_off;
4180_1 2:58d85409f7ff 132 break;
4180_1 2:58d85409f7ff 133 }
4180_1 2:58d85409f7ff 134 wait(0.33);
4180_1 3:346ef671ef28 135 // heartbeat LED - common debug tool
4180_1 3:346ef671ef28 136 // blinks as long as code is running and not locked up
4180_1 3:346ef671ef28 137 myLED1=!myLED1;
4180_1 2:58d85409f7ff 138 }
4180_1 2:58d85409f7ff 139 }
4180_1 0:cc87c48aa43c 140 }
4180_1 0:cc87c48aa43c 141 }