skeleton code project setup for mbed student thermostat lab - students must add code
Dependencies: PinDetect SDFileSystem TextLCD mbed
Fork of Two_Pushbutton_Debounce_Interrupt by
Revision 2:58d85409f7ff, committed 2013-01-31
- Comitter:
- 4180_1
- Date:
- Thu Jan 31 03:15:20 2013 +0000
- Parent:
- 1:768b8bd42e33
- Child:
- 3:346ef671ef28
- Commit message:
- ver 1.0
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Thu Jan 31 03:15:20 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Shiftbrite.h Thu Jan 31 03:15:20 2013 +0000 @@ -0,0 +1,26 @@ +#include "mbed.h" + +//Setup a new class for a Shiftbrite RGB LED module +class Shiftbrite +{ +public: + Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk); + void write(int red, int green, int blue); + +private: +//class sets up the pins + DigitalOut _pin_e; + DigitalOut _pin_l; + SPI _spi; +}; + +Shiftbrite::Shiftbrite(PinName pin_e, PinName pin_l, PinName pin_do, PinName pin_di, PinName pin_clk) + : _pin_e(pin_e), _pin_l(pin_l), _spi(pin_do, pin_di, pin_clk) +{ + // ADD CODE HERE +} + +void Shiftbrite::write(int red, int green, int blue) +{ + // ADD CODE HERE +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Thu Jan 31 03:15:20 2013 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TMP36.h Thu Jan 31 03:15:20 2013 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + +//Setup a new class for TMP36 sensor +class TMP36 +{ +public: + TMP36(PinName pin); + TMP36(); + operator float (); + float read(); +private: +//class sets up the AnalogIn pin + AnalogIn _pin; +}; + +TMP36::TMP36(PinName pin) : _pin(pin) +{ +// _pin(pin) means pass pin to the AnalogIn constructor +} + +float TMP36::read() +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} +//overload of float conversion (avoids needing to type .read() in equations) +TMP36::operator float () +{ +//convert sensor reading to temperature in degrees C + return ((_pin.read()*3.3)-0.500)*100.0; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Thu Jan 31 03:15:20 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
--- a/main.cpp Tue Jan 29 02:34:03 2013 +0000 +++ b/main.cpp Thu Jan 31 03:15:20 2013 +0000 @@ -1,57 +1,137 @@ +// skeleton code for ECE 2036 thermostat lab +// code must be added by students #include "mbed.h" +#include "TMP36.h" +#include "SDFileSystem.h" +#include "TextLCD.h" #include "PinDetect.h" -// must import Cookbook PinDetct library into project -// URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw +#include "Speaker.h" +// must add your new class code to the project file Shiftbrite.h +#include "Shiftbrite.h" + +// use class to setup temperature sensor pins +TMP36 myTMP36(p15); //Analog in -DigitalOut myled(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut myled4(LED4); +// use class to setup microSD card filesystem +SDFileSystem sd(p5, p6, p7, p8, "sd"); + +// use class to setup the LCD +TextLCD myLCD(p22, p23, p24, p25, p26, p27); // rs, e, d4-d7 + +// use class to setup pushbuttons pins +PinDetect pb1(p28); +PinDetect pb2(p29); +PinDetect pb3(p30); + +// use class to setup speaker pin +Speaker mySpeaker(p21); //PWM out -PinDetect pb1(p8); -PinDetect pb2(p7); -// SPST Pushbutton debounced count demo using interrupts and callback -// no external PullUp resistor needed -// Pushbutton from P8 to GND. -// Second Pushbutton from P7 to GND. -// A pb hit generates an interrupt and activates the callback function -// after the switch is debounced +// use class to setup Shiftbrite pins +Shiftbrite myShiftbrite(p9, p10, p11, p12, p13);// ei li di n/c ci + +// use class to setup Mbed's four on-board LEDs +DigitalOut myLED1(LED1); +DigitalOut myLED2(LED2); +DigitalOut myLED3(LED3); +DigitalOut myLED4(LED4); + +// heat or code mode jumper - removed when pushbuttons added +DigitalIn jumper_wire(p14); -// Global count variable -int volatile count=0; +//also setting any unused analog input pins to digital outputs reduces A/D noise a bit +//see http://mbed.org/users/chris/notebook/Getting-best-ADC-performance/ +DigitalOut P16(p16); +DigitalOut P17(p17); +DigitalOut P18(p18); +DigitalOut P19(p19); +DigitalOut P20(p20); + + + + +// Global variables used in callbacks and main program +// C variables in interrupt routines should use volatile keyword +int volatile heat_setting=78; // heat to temp +int volatile cool_setting=68; // cool to temp +bool volatile mode=false; // heat or cool mpde // Callback routine is interrupt activated by a debounced pb1 hit -void pb1_hit_callback (void) { - count++; - myled4 = count & 0x01; - myled3 = (count & 0x02)>>1; - myled2 = (count & 0x04)>>2; +void pb1_hit_callback (void) +{ +// ADD CODE HERE } // Callback routine is interrupt activated by a debounced pb2 hit -void pb2_hit_callback (void) { - count--; - myled4 = count & 0x01; - myled3 = (count & 0x02)>>1; - myled2 = (count & 0x04)>>2; +void pb2_hit_callback (void) +{ +// ADD CODE HERE +} +// Callback routine is interrupt activated by a debounced pb3 hit +void pb3_hit_callback (void) +{ +// ADD CODE HERE } -int main() { + - // Use internal pullups for pushbutton - pb1.mode(PullUp); +int main() +{ + float Current_temp=0.0; + + // Use internal pullups for the three pushbuttons + pb1.mode(PullUp); pb2.mode(PullUp); + pb3.mode(PullUp); // Delay for initial pullup to take effect wait(.01); // Setup Interrupt callback functions for a pb hit pb1.attach_deasserted(&pb1_hit_callback); pb2.attach_deasserted(&pb2_hit_callback); + pb3.attach_deasserted(&pb2_hit_callback); // Start sampling pb inputs using interrupts pb1.setSampleFrequency(); pb2.setSampleFrequency(); - //Blink myled in main routine forever while responding to pb changes - // via interrupts that activate the callback counter function + pb2.setSampleFrequency(); + // pushbuttons now setup and running + + + // start I/O examples - DELETE THIS IN YOUR CODE..BUT WILL USE THESE I/O IDEAS ELSEWHERE + // since all this compiles - the needed *.h files for these are in the project + // + Current_temp = myTMP36; + printf("Hello PC World\n\r"); // need terminal application running on PC to see this + myLCD.printf("Hello LCD World"); // LCD + mySpeaker.PlayNote(500.0, 1.0, 1.0); // Speaker buzz + myShiftbrite.write( 0, 50 ,0); // Green RGB LED + // SD card write file example - prints error message on PC when running until SD card hooked up + FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); //open SD + if(fp == NULL) { + error("Could not open file for write\n"); + } + fprintf(fp, "Hello SD Card World!"); // write SD + fclose(fp); // close SD card + // + // end I/O examples + + + + + // State machine code below will need changes while (1) { - myled = !myled; - wait(.5); + { + enum Statetype { Heat_off = 0, Heat_on }; + Statetype state = Heat_off; + while(1) { + switch (state) { + case Heat_off: + myLED2 = 0; + state = Heat_on; + break; + case Heat_on: + myLED2 = 1; + state = Heat_off; + break; + } + wait(0.33); + } + } } - } \ No newline at end of file
--- a/mbed.bld Tue Jan 29 02:34:03 2013 +0000 +++ b/mbed.bld Thu Jan 31 03:15:20 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912 +http://mbed.org/users/mbed_official/code/mbed/builds/0954ebd79f59 \ No newline at end of file