Project

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

Committer:
ericspatterson
Date:
Wed Nov 19 15:19:54 2014 +0000
Revision:
4:85eb29963721
Parent:
3:0f0e79eff4a7
updated controls...still needs more user control

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 2:c94244d1cab4 6 #include <mpr121.h>
ericspatterson 2:c94244d1cab4 7
ericspatterson 2:c94244d1cab4 8 DigitalOut led1(LED1);
ericspatterson 2:c94244d1cab4 9 DigitalOut led2(LED2);
ericspatterson 2:c94244d1cab4 10 DigitalOut led3(LED3);
ericspatterson 4:85eb29963721 11 DigitalOut led4(LED4);
ericspatterson 4:85eb29963721 12 DigitalOut motor(p22); // motor control
ericspatterson 0:4fbaafa6574c 13
ericspatterson 0:4fbaafa6574c 14 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
ericspatterson 0:4fbaafa6574c 15 SPI thermoSPI(p5,p6,p7); // setup SPI interface
ericspatterson 0:4fbaafa6574c 16 max31855 max1(thermoSPI, p20); //setup max31855 interface
ericspatterson 0:4fbaafa6574c 17 Mutex lcd_mutex; // mutex to make the lcd lib thread safe
ericspatterson 0:4fbaafa6574c 18 Semaphore four_slots(1); //Activate semaphore
ericspatterson 1:26267401354a 19 DigitalOut toggle(p30); // toggle for Nichrome wire SSR
ericspatterson 1:26267401354a 20 DigitalIn up(p19); // User pushbutton up controller
ericspatterson 1:26267401354a 21 DigitalIn down(p18); // User pushbutton down button controller
ericspatterson 0:4fbaafa6574c 22
ericspatterson 4:85eb29963721 23
ericspatterson 2:c94244d1cab4 24 int settemp = 85; // initial temperature set
ericspatterson 0:4fbaafa6574c 25 float ftemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 26 float ctemperature = 0; // float variable for temperature
ericspatterson 0:4fbaafa6574c 27
ericspatterson 2:c94244d1cab4 28 int key_code=0;
ericspatterson 2:c94244d1cab4 29
ericspatterson 4:85eb29963721 30
ericspatterson 2:c94244d1cab4 31 // Create the interrupt receiver object on pin 26
ericspatterson 2:c94244d1cab4 32 InterruptIn interrupt(p26);
ericspatterson 2:c94244d1cab4 33
ericspatterson 2:c94244d1cab4 34 // Setup the i2c bus on pins 28 and 27
ericspatterson 2:c94244d1cab4 35 I2C i2c(p28, p27);
ericspatterson 2:c94244d1cab4 36
ericspatterson 2:c94244d1cab4 37 // Setup the Mpr121:
ericspatterson 2:c94244d1cab4 38 // constructor(i2c object, i2c address of the mpr121)
ericspatterson 2:c94244d1cab4 39 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
ericspatterson 2:c94244d1cab4 40
ericspatterson 1:26267401354a 41 // Thread 1: print the elapsed time on line 1 of the uLCD (i.e., HH:MM:SS)
ericspatterson 1:26267401354a 42 void elapsedtime(void const *args) { //elapsed timer for when program starts
ericspatterson 1:26267401354a 43 int s = 00; //seconds
ericspatterson 1:26267401354a 44 int m = 00; //minutes
ericspatterson 1:26267401354a 45 int h = 00; //hours
ericspatterson 0:4fbaafa6574c 46
ericspatterson 0:4fbaafa6574c 47 while(true){
ericspatterson 1:26267401354a 48 s++;
ericspatterson 0:4fbaafa6574c 49 if(s>59){
ericspatterson 0:4fbaafa6574c 50 s=00;
ericspatterson 0:4fbaafa6574c 51 m++;
ericspatterson 0:4fbaafa6574c 52 }
ericspatterson 0:4fbaafa6574c 53 if(m>59){
ericspatterson 0:4fbaafa6574c 54 m=00;
ericspatterson 0:4fbaafa6574c 55 h++;
ericspatterson 0:4fbaafa6574c 56 }
ericspatterson 1:26267401354a 57 four_slots.wait(); // lock screen resource
ericspatterson 0:4fbaafa6574c 58 //lcd_mutex.lock();
ericspatterson 1:26267401354a 59 uLCD.color(0xFFFF00); //set text color
ericspatterson 1:26267401354a 60 uLCD.locate(0,0); //col,row location of text
ericspatterson 1:26267401354a 61 wait(0.1);
ericspatterson 1:26267401354a 62 uLCD.printf("ET: %2d:%2d:%2d", h,m,s); // display time
ericspatterson 0:4fbaafa6574c 63 //lcd_mutex.unlock();
ericspatterson 1:26267401354a 64 four_slots.release(); //unlock screen resource
ericspatterson 0:4fbaafa6574c 65 Thread::wait(1000); // update once per secon
ericspatterson 0:4fbaafa6574c 66 }
ericspatterson 0:4fbaafa6574c 67 }
ericspatterson 0:4fbaafa6574c 68
ericspatterson 0:4fbaafa6574c 69
ericspatterson 1:26267401354a 70 // Thread 2: print the temperature from the thermocouple - MAX31855 device
ericspatterson 0:4fbaafa6574c 71 void thermoread(void const *args) { //line 2
ericspatterson 0:4fbaafa6574c 72 while(true){
ericspatterson 0:4fbaafa6574c 73 if (max1.ready()==1){
ericspatterson 0:4fbaafa6574c 74 ctemperature = max1.read_temp(); //Get the reading
ericspatterson 0:4fbaafa6574c 75 ftemperature = (ctemperature)*(9.0/5.0)+32.0;
ericspatterson 0:4fbaafa6574c 76 four_slots.wait();
ericspatterson 0:4fbaafa6574c 77 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 78 uLCD.locate(0,2); //col,row
ericspatterson 0:4fbaafa6574c 79 wait(0.1);
ericspatterson 0:4fbaafa6574c 80 uLCD.printf("Current Temp. F");
ericspatterson 0:4fbaafa6574c 81 uLCD.locate(0,3); //col,row
ericspatterson 0:4fbaafa6574c 82 uLCD.printf("%4.2f", ftemperature);
ericspatterson 0:4fbaafa6574c 83 four_slots.release();
ericspatterson 0:4fbaafa6574c 84 }
ericspatterson 0:4fbaafa6574c 85 }
ericspatterson 0:4fbaafa6574c 86 }
ericspatterson 0:4fbaafa6574c 87
ericspatterson 2:c94244d1cab4 88
ericspatterson 2:c94244d1cab4 89 void fallInterrupt() {
ericspatterson 2:c94244d1cab4 90 int i=0;
ericspatterson 2:c94244d1cab4 91 int value=mpr121.read(0x00);
ericspatterson 2:c94244d1cab4 92 value +=mpr121.read(0x01)<<8;
ericspatterson 2:c94244d1cab4 93 // LED demo mod
ericspatterson 2:c94244d1cab4 94 i=0;
ericspatterson 2:c94244d1cab4 95 // puts key number out to LEDs for demo
ericspatterson 2:c94244d1cab4 96 for (i=0; i<12; i++) {
ericspatterson 2:c94244d1cab4 97 if (((value>>i)&0x01)==1) key_code=i+1;
ericspatterson 2:c94244d1cab4 98 }
ericspatterson 2:c94244d1cab4 99 }
ericspatterson 2:c94244d1cab4 100
ericspatterson 2:c94244d1cab4 101
ericspatterson 2:c94244d1cab4 102 int main() {
ericspatterson 2:c94244d1cab4 103 max1.initialise(); //initialize thermocouple IC
ericspatterson 2:c94244d1cab4 104 uLCD.baudrate(3000000); //set LCD baudrate
ericspatterson 2:c94244d1cab4 105
ericspatterson 2:c94244d1cab4 106 interrupt.fall(&fallInterrupt);
ericspatterson 2:c94244d1cab4 107 interrupt.mode(PullUp);
ericspatterson 2:c94244d1cab4 108
ericspatterson 2:c94244d1cab4 109 Thread t1(elapsedtime); //run elapsed time counter
ericspatterson 2:c94244d1cab4 110 Thread t2(thermoread); //read and display temperature from thermocouple
ericspatterson 2:c94244d1cab4 111
ericspatterson 2:c94244d1cab4 112 while(1){ //hystersis program
ericspatterson 2:c94244d1cab4 113 key_code = 0;
ericspatterson 2:c94244d1cab4 114 four_slots.wait();
ericspatterson 2:c94244d1cab4 115 uLCD.color(0xFFFF00);
ericspatterson 2:c94244d1cab4 116 uLCD.locate(0,10); //col,row
ericspatterson 2:c94244d1cab4 117 //wait(0.1);
ericspatterson 2:c94244d1cab4 118 uLCD.printf("Test.");
ericspatterson 2:c94244d1cab4 119 uLCD.locate(0,11); //col,row
ericspatterson 2:c94244d1cab4 120 uLCD.printf("%2d", key_code);
ericspatterson 2:c94244d1cab4 121 four_slots.release();
ericspatterson 2:c94244d1cab4 122
ericspatterson 4:85eb29963721 123 if(key_code == 2){
ericspatterson 0:4fbaafa6574c 124 settemp += 1;
ericspatterson 0:4fbaafa6574c 125 four_slots.wait();
ericspatterson 0:4fbaafa6574c 126 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 127 uLCD.locate(0,5); //col,row
ericspatterson 0:4fbaafa6574c 128 uLCD.printf("Set Temp. F");
ericspatterson 0:4fbaafa6574c 129 uLCD.locate(0,6); //col,row
ericspatterson 0:4fbaafa6574c 130 uLCD.printf("%d",settemp);
ericspatterson 4:85eb29963721 131 four_slots.release();
ericspatterson 4:85eb29963721 132 wait(2);
ericspatterson 0:4fbaafa6574c 133 }
ericspatterson 4:85eb29963721 134 if(key_code== 1){
ericspatterson 0:4fbaafa6574c 135 settemp -= 1;
ericspatterson 0:4fbaafa6574c 136 four_slots.wait();
ericspatterson 0:4fbaafa6574c 137 uLCD.color(0xFFFF00);
ericspatterson 0:4fbaafa6574c 138 uLCD.locate(0,5); //col,row
ericspatterson 0:4fbaafa6574c 139 uLCD.printf("Set Temp. F");
ericspatterson 0:4fbaafa6574c 140 uLCD.locate(0,6); //col,row
ericspatterson 0:4fbaafa6574c 141 uLCD.printf("%d",settemp);
ericspatterson 0:4fbaafa6574c 142 four_slots.release();
ericspatterson 4:85eb29963721 143 }
ericspatterson 4:85eb29963721 144
ericspatterson 4:85eb29963721 145 if(key_code== 9){
ericspatterson 4:85eb29963721 146 motor = 0;
ericspatterson 4:85eb29963721 147 settemp -= 1;
ericspatterson 4:85eb29963721 148 four_slots.wait();
ericspatterson 4:85eb29963721 149 uLCD.color(0xFFFF00);
ericspatterson 4:85eb29963721 150 uLCD.locate(0,13); //col,row
ericspatterson 4:85eb29963721 151 uLCD.printf("Motor is OFF");
ericspatterson 4:85eb29963721 152 four_slots.release();
ericspatterson 4:85eb29963721 153 }
ericspatterson 4:85eb29963721 154 if(key_code== 10){
ericspatterson 4:85eb29963721 155 motor = 1;
ericspatterson 4:85eb29963721 156 settemp -= 1;
ericspatterson 4:85eb29963721 157 four_slots.wait();
ericspatterson 4:85eb29963721 158 uLCD.color(0xFFFF00);
ericspatterson 4:85eb29963721 159 uLCD.locate(0,13); //col,row
ericspatterson 4:85eb29963721 160 uLCD.printf("Motor is ON ");
ericspatterson 4:85eb29963721 161 four_slots.release();
ericspatterson 0:4fbaafa6574c 162 }
ericspatterson 2:c94244d1cab4 163
ericspatterson 2:c94244d1cab4 164 if(ftemperature < settemp-1){ //condition for 1 degree under
ericspatterson 2:c94244d1cab4 165 toggle=1; //turn on ssr for nichrome wire
ericspatterson 2:c94244d1cab4 166 }
ericspatterson 2:c94244d1cab4 167 if(ftemperature > settemp+1){ //condition for 1 degree over
ericspatterson 2:c94244d1cab4 168 toggle=0; //turn off ssr for nichrome wire
ericspatterson 2:c94244d1cab4 169 }
ericspatterson 2:c94244d1cab4 170 // wait(.1); //check every 1 second
ericspatterson 0:4fbaafa6574c 171 }
ericspatterson 0:4fbaafa6574c 172 }