Project

Dependencies:   4DGL-uLCD-SE MAX31855 mbed-rtos mbed

Fork of Coffee_Roaster by Eric Patterson

Committer:
ericspatterson
Date:
Wed Nov 12 19:17:19 2014 +0000
Revision:
1:26267401354a
Parent:
0:4fbaafa6574c
Child:
2:c94244d1cab4
added more code comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ericspatterson 0:4fbaafa6574c 1 #include "mbed.h"
ericspatterson 0:4fbaafa6574c 2 #include "uLCD_4DGL.h"
ericspatterson 0:4fbaafa6574c 3 #include "rtos.h"
ericspatterson 0:4fbaafa6574c 4 #include "stdio.h"
ericspatterson 0:4fbaafa6574c 5 #include "max31855.h"
ericspatterson 0:4fbaafa6574c 6
ericspatterson 0:4fbaafa6574c 7 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
ericspatterson 0:4fbaafa6574c 8 SPI thermoSPI(p5,p6,p7); // setup SPI interface
ericspatterson 0:4fbaafa6574c 9 max31855 max1(thermoSPI, p20); //setup max31855 interface
ericspatterson 0:4fbaafa6574c 10 Mutex lcd_mutex; // mutex to make the lcd lib thread safe
ericspatterson 0:4fbaafa6574c 11 Semaphore four_slots(1); //Activate semaphore
ericspatterson 1:26267401354a 12 DigitalOut toggle(p30); // toggle for Nichrome wire SSR
ericspatterson 1:26267401354a 13 DigitalIn up(p19); // User pushbutton up controller
ericspatterson 1:26267401354a 14 DigitalIn down(p18); // User pushbutton down button controller
ericspatterson 0:4fbaafa6574c 15
ericspatterson 1:26267401354a 16 int settemp = 75; // initial temperature set
ericspatterson 0:4fbaafa6574c 17 float ftemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 18 float ctemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 19
ericspatterson 1:26267401354a 20 // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS)
ericspatterson 1:26267401354a 21 void elapsedtime(void const *args) { //elapsed timer for when program starts
ericspatterson 1:26267401354a 22 int s = 00; //seconds
ericspatterson 1:26267401354a 23 int m = 00; //minutes
ericspatterson 1:26267401354a 24 int h = 00; //hours
ericspatterson 0:4fbaafa6574c 25
ericspatterson 0:4fbaafa6574c 26 while(true){
ericspatterson 1:26267401354a 27 s++;
ericspatterson 0:4fbaafa6574c 28 if(s>59){
ericspatterson 0:4fbaafa6574c 29 s=00;
ericspatterson 0:4fbaafa6574c 30 m++;
ericspatterson 0:4fbaafa6574c 31 }
ericspatterson 0:4fbaafa6574c 32 if(m>59){
ericspatterson 0:4fbaafa6574c 33 m=00;
ericspatterson 0:4fbaafa6574c 34 h++;
ericspatterson 0:4fbaafa6574c 35 }
ericspatterson 1:26267401354a 36 four_slots.wait(); // lock screen resource
ericspatterson 0:4fbaafa6574c 37 //lcd_mutex.lock();
ericspatterson 1:26267401354a 38 uLCD.color(0xFFFF00); //set text color
ericspatterson 1:26267401354a 39 uLCD.locate(0,0); //col,row location of text
ericspatterson 1:26267401354a 40 wait(0.1);
ericspatterson 1:26267401354a 41 uLCD.printf("ET: %2d:%2d:%2d", h,m,s); // display time
ericspatterson 0:4fbaafa6574c 42 //lcd_mutex.unlock();
ericspatterson 1:26267401354a 43 four_slots.release(); //unlock screen resource
ericspatterson 0:4fbaafa6574c 44 Thread::wait(1000); // update once per secon
ericspatterson 0:4fbaafa6574c 45 }
ericspatterson 0:4fbaafa6574c 46 }
ericspatterson 0:4fbaafa6574c 47
ericspatterson 0:4fbaafa6574c 48
ericspatterson 1:26267401354a 49 // Thread 2: print the temperature from the thermocouple - MAX31855 device
ericspatterson 0:4fbaafa6574c 50 void thermoread(void const *args) { //line 2
ericspatterson 0:4fbaafa6574c 51 while(true){
ericspatterson 0:4fbaafa6574c 52 if (max1.ready()==1){
ericspatterson 0:4fbaafa6574c 53 ctemperature = max1.read_temp(); //Get the reading
ericspatterson 0:4fbaafa6574c 54 ftemperature = (ctemperature)*(9.0/5.0)+32.0;
ericspatterson 0:4fbaafa6574c 55 four_slots.wait();
ericspatterson 0:4fbaafa6574c 56 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 57 uLCD.locate(0,2); //col,row
ericspatterson 0:4fbaafa6574c 58 wait(0.1);
ericspatterson 0:4fbaafa6574c 59 uLCD.printf("Current Temp. F");
ericspatterson 0:4fbaafa6574c 60 uLCD.locate(0,3); //col,row
ericspatterson 0:4fbaafa6574c 61 uLCD.printf("%4.2f", ftemperature);
ericspatterson 0:4fbaafa6574c 62 four_slots.release();
ericspatterson 0:4fbaafa6574c 63 }
ericspatterson 0:4fbaafa6574c 64 }
ericspatterson 0:4fbaafa6574c 65 }
ericspatterson 0:4fbaafa6574c 66
ericspatterson 1:26267401354a 67 // Thread 3: read in user input from buttons..
ericspatterson 1:26267401354a 68 void thermoset (void const *args) { //
ericspatterson 0:4fbaafa6574c 69 while(true){
ericspatterson 0:4fbaafa6574c 70 if (up==1){
ericspatterson 0:4fbaafa6574c 71 settemp += 1;
ericspatterson 0:4fbaafa6574c 72 four_slots.wait();
ericspatterson 0:4fbaafa6574c 73 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 74 uLCD.locate(0,5); //col,row
ericspatterson 0:4fbaafa6574c 75 uLCD.printf("Set Temp. F");
ericspatterson 0:4fbaafa6574c 76 uLCD.locate(0,6); //col,row
ericspatterson 0:4fbaafa6574c 77 uLCD.printf("%d",settemp);
ericspatterson 0:4fbaafa6574c 78 four_slots.release();
ericspatterson 0:4fbaafa6574c 79 wait(.5);
ericspatterson 0:4fbaafa6574c 80 }
ericspatterson 0:4fbaafa6574c 81 if (down==1){
ericspatterson 0:4fbaafa6574c 82 settemp -= 1;
ericspatterson 0:4fbaafa6574c 83 four_slots.wait();
ericspatterson 0:4fbaafa6574c 84 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 85 uLCD.locate(0,5); //col,row
ericspatterson 0:4fbaafa6574c 86 uLCD.printf("Set Temp. F");
ericspatterson 0:4fbaafa6574c 87 uLCD.locate(0,6); //col,row
ericspatterson 0:4fbaafa6574c 88 uLCD.printf("%d",settemp);
ericspatterson 0:4fbaafa6574c 89 four_slots.release();
ericspatterson 0:4fbaafa6574c 90 wait(.5);
ericspatterson 0:4fbaafa6574c 91 }
ericspatterson 0:4fbaafa6574c 92 }
ericspatterson 0:4fbaafa6574c 93 }
ericspatterson 0:4fbaafa6574c 94
ericspatterson 0:4fbaafa6574c 95
ericspatterson 0:4fbaafa6574c 96 int main() {
ericspatterson 0:4fbaafa6574c 97 max1.initialise(); //initialize thermocouple IC
ericspatterson 0:4fbaafa6574c 98 uLCD.baudrate(3000000); //set LCD baudrate
ericspatterson 0:4fbaafa6574c 99 Thread t1(elapsedtime); //run elapsed time counter
ericspatterson 0:4fbaafa6574c 100 Thread t2(thermoread); //read and display temperature from thermocouple
ericspatterson 1:26267401354a 101 Thread t3(thermoset); //runs thread that checks to see if user has changed the temperature setpoint
ericspatterson 0:4fbaafa6574c 102
ericspatterson 1:26267401354a 103 while(1){ //hystersis program
ericspatterson 1:26267401354a 104 if(ftemperature < settemp-1){ //condition for 1 degree under
ericspatterson 1:26267401354a 105 toggle=1; //turn on ssr for nichrome wire
ericspatterson 0:4fbaafa6574c 106 }
ericspatterson 1:26267401354a 107 if(ftemperature > settemp+1){ //condition for 1 degree over
ericspatterson 1:26267401354a 108 toggle=0; //turn off ssr for nichrome wire
ericspatterson 0:4fbaafa6574c 109 }
ericspatterson 1:26267401354a 110 wait(1); //check every 1 second
ericspatterson 0:4fbaafa6574c 111 }
ericspatterson 1:26267401354a 112 }