Temperature Control: NUCLEO-F334R8 + DS18B20 + LCD1602 shield + RELAY and see the results on the PC and on the LCD1602. For more info see here: http://www.emcu.it/NUCLEOevaBoards/mBed/QSG-Mbed-Library.pdf

Dependencies:   DS1820 mbed TextLCD

Fork of F334andDS18B20 by Enrico Marinoni

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002 
00003 Temperature control based on:
00004     NUCLEO-F334R8 
00005     DS18B20 
00006     RELAY module 
00007     LCD1602 shield
00008 
00009 By:       www.emcu.it
00010 Date:     Dec.2015
00011 Version:  1.0
00012 Name:     F334andDS18B20andLDC1602
00013 NOTE:     For more info see here: http://www.emcu.it/NUCLEOevaBoards/mBed/QSG-Mbed-Library.pdf
00014 
00015 THE SOFTWARE AND HARDWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
00016 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00017 FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
00018 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
00019 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 
00022 UART Configuration (It is necessary for see the results, we suggest to use TeraTerm on PC)  
00023     Baud Rate:    9600 
00024     Data Bit:     8
00025     Parity:       NONE
00026     Stop Bit:     1
00027     Flow Control: NONE
00028     
00029 This SW is ready to use on the NUCLEO-F334R8.
00030 Connect to the NUCLEO-F334R8, the DS18B20 sensor (see the schematic below), the LCD1602 board and a RELAY board.
00031 The temperature sampling time is 200 msec, you change this parameter in the TempSamplingTime.
00032 
00033  DS18B20 front view
00034     __________
00035    |          |
00036    |    DS    |
00037    |   18B20  |   
00038    |          |
00039    |__________|
00040      |   |   |
00041      1   2   3
00042     GND  DQ VCC (5V)
00043      |   |   |______________ to VCC (5V on the LCD1602 or on the NUCLEO-F334R8)  
00044      |   |  _|_
00045      |   |  | |
00046      |   |  | | 4K7
00047      |   |  | |
00048      |   |  -|-
00049      |   |___|______________ to A1 (on the LCD1602 board or on the NUCLEO-F334R8) 
00050      |
00051      |
00052      |______________________ to GND (on the LCD1602 board or on the NUCLEO-F334R8)   
00053 
00054 This SW is just for only one DS18B20 + LCD1602 board
00055 This SW is a derivative of:: https://developer.mbed.org/users/Sissors/code/DS1820_HelloWorld/
00056 On the: https://developer.mbed.org/users/Sissors/code/DS1820_HelloWorld/ there is a multi sensor (DS18B20) example.
00057 
00058 */
00059 
00060 
00061 // Below there is the Data Pin where is connected the: 
00062 // pin.2 (DQ) of the DS18B20
00063 #define DATA_PIN  A1
00064 
00065 #include "mbed.h"
00066 #include "DS1820.h"
00067 #include "TextLCD.h"
00068 
00069 // Define the LED
00070 DigitalOut myled(LED1);
00071 DigitalOut RL1(A5);       // Relè n.1
00072 
00073 // Define the PC serial Port
00074 Serial pc(SERIAL_TX, SERIAL_RX);
00075 
00076 // Specify the pin of the NUCLEO-F334R8 where is connected 
00077 // the pin.2 (DQ) of the DS18B20.
00078 DS1820 probe(DATA_PIN);
00079 
00080 // SetUp GPIO for drive the LCD
00081 //  LCD (RS, E, D4, D5, D6, D7)
00082 TextLCD lcd(PA_9, PC_7, PB_5, PB_4, PB_10, PA_8); // LCD Shield for Arduino
00083 AnalogIn button(PA_0);  // board button
00084 PwmOut backlight(PB_6);
00085 
00086 // COSTANTs
00087 #define ON  1
00088 #define OFF 0
00089 #define tStep  0.1 // Step minimo di Up e Down x le impostazioni dei valori
00090 #define tUp    1
00091 #define tDown  2
00092 #define tRigth 3
00093 #define tLeft  4
00094 #define tNone  5
00095 #define NumConv 4
00096 #define MaxError 30
00097 #define TastoSU  15000
00098 #define TastoGIU 30900
00099 #define TastoDES DeltaTasti + 2
00100 #define TastoSIN 48000
00101 #define DeltaTasti 4000
00102 #define TempSamplingTime 200
00103 
00104 // VARIABLEs
00105 uint8_t n = 0;
00106 uint8_t FlashSeg = 0;
00107 uint8_t rele = OFF;
00108 float temperature = 0;
00109 float DeltaTemp = 0.5; 
00110 float tempON  = 20.00;
00111 float tempOFF = tempON + DeltaTemp;
00112 float temperaturePREVIOUS = 0;
00113 float temperatureMAX = 0;
00114 float temperatureMIN = 0;
00115 float MaxTempError = 0;
00116 unsigned long value = 0;
00117 uint8_t LastButtonPressed = tNone;
00118 
00119 // FUNCTIONs
00120 void CalcMed(void);
00121 void PrintTemperatures(void);
00122 uint8_t ReadButtonOnLCD1602(void);
00123 void SetUp(void);
00124 void ShowSetUp(void);
00125 void WaitingRightButton(void);
00126 
00127 
00128 int main() 
00129 {
00130     
00131     // SetUp the resolution of the DS18B20 to 12bit
00132     probe.setResolution(12);
00133     
00134     // StartUp Logo to the PC
00135     pc.printf("\r\n\r\nTemperature Control made using:\n\r");
00136     pc.printf("NUCLEO-F334R8 + DS18B20 (input on A1) + LCD1602 + RELAY (output on A5).\n\r");
00137     pc.printf("For more info look here:\n\r");
00138     pc.printf("   http://www.emcu.it/NUCLEOevaBoards/mBed/QSG-Mbed-Library.pdf\n\r");
00139     pc.printf("by: www.emcu.it\n\r\n\r");    
00140     
00141     // TurnOn the LCD backlight of the LCD1602 board
00142     backlight = ON;  // ON: 1, OFF: 0
00143     
00144     // StartUp Logo to the LCD1602 board
00145     lcd.cls();
00146     lcd.locate(0,0);
00147     lcd.printf("By: www.emcu.it");
00148     lcd.locate(0,1);
00149     lcd.printf("Ver.1.0");    
00150  
00151     // SetUp the temperatures after PowerOn
00152     probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
00153     temperature = probe.temperature('c');
00154     // Eliminate the wrong reading
00155     CalcMed();   
00156     temperaturePREVIOUS = temperature;
00157     temperatureMAX = temperature;
00158     temperatureMIN = 50.50;
00159     wait(1); 
00160         
00161     while(1) 
00162     {
00163         // READ BUTTON on LCD board +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00164         if (ReadButtonOnLCD1602() == tUp)
00165             backlight = ON;
00166         else if (ReadButtonOnLCD1602() == tDown)
00167             backlight = OFF;
00168         else if (ReadButtonOnLCD1602() == tRigth)
00169             ShowSetUp();            
00170         else if (ReadButtonOnLCD1602() == tLeft)
00171             SetUp();
00172         else;
00173         // END READ BUTTON on LCD board +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00174         
00175         // READ and DISPLAY the TEMPERATURE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00176         probe.convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready
00177         temperature = probe.temperature('c');
00178         wait_ms(TempSamplingTime);             
00179         // Eliminate the wrong reading
00180         CalcMed();
00181         
00182         // ON/OFF Relè
00183         if (temperature <= tempON)       // ON Relè
00184         {
00185             rele = ON;
00186             RL1 = 1;
00187         } 
00188         else if (temperature >= tempOFF) // OFF Relè
00189         {
00190             rele = OFF;
00191             RL1 = 0;
00192         } 
00193         
00194         // pc.printf("The Temperature is %2.3f Celsius/Centigradi\r\n", temperature);
00195         PrintTemperatures();
00196         
00197         // Display Status on the LCD1602 board
00198         lcd.cls();        
00199         lcd.locate(0,0);
00200         if (rele == ON)
00201             lcd.printf("T %2.3f RELEon ", temperature); // probe.temperature('c'));
00202         if (rele == OFF)
00203             lcd.printf("T %2.3f RELEoff", temperature); // probe.temperature('c'));            
00204         lcd.locate(0,1);
00205         lcd.printf("TM%2.3fTm%2.3f", temperatureMAX, temperatureMIN);   
00206         // END READ and DISPLAY the TEMPERATURE +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
00207 
00208         // START Flashing bar on the LCD ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++        
00209         FlashSeg++;
00210         if (FlashSeg == 1)
00211         {
00212             lcd.locate(8,0);
00213             lcd.printf(" ");
00214         }
00215         if (FlashSeg == 2)
00216         {
00217             lcd.locate(8,0);
00218             lcd.printf("*");
00219             FlashSeg = 0;
00220         }                
00221         // END START Flashing bar on the LCD ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00222                
00223     }
00224 }
00225 
00226 
00227 /*
00228 **** ShowSetUp
00229 This function use the global variable: 
00230     tempON
00231     tempOFF
00232 */
00233 void ShowSetUp(void)
00234 {
00235     // TurnOn the LCD backlight of the LCD1602 board
00236     backlight = ON;  // ON: 1, OFF: 0
00237         
00238     // Show the SetUp related to:
00239     // TempMin (tempON) & TempMax (tempOFF)
00240     lcd.cls();
00241     lcd.locate(0,0);
00242     lcd.printf("Tm%2.2f TM%2.2f ", tempON, tempOFF);
00243     WaitingRightButton();  
00244 
00245     lcd.cls();
00246     lcd.locate(0,0);
00247     lcd.printf("LEFT SetUp Menu ");
00248     WaitingRightButton(); 
00249     
00250     lcd.cls();
00251     lcd.locate(0,0);
00252     lcd.printf("By: www.emcu.it ");
00253     WaitingRightButton();      
00254 
00255     lcd.cls();
00256     lcd.locate(0,0);
00257     lcd.printf("By patient....  ", tempON, tempOFF);       
00258 }
00259 
00260 
00261 /*
00262 **** WaitingRightButton
00263 This function waiting the Release and Pressure of the Rirght Button
00264 */
00265 void WaitingRightButton(void)
00266 {
00267     // Awaiting the release of the Right Button
00268     lcd.locate(0,1);
00269     lcd.printf("Release RIGHT bu");    
00270     while (ReadButtonOnLCD1602() == tRigth);
00271     wait_ms(100);
00272     // Awaiting the pressure of the Rigth Button
00273     lcd.locate(0,1);    
00274     lcd.printf("Press RIGTH but.");   
00275     while (ReadButtonOnLCD1602() != tRigth);    
00276 }
00277 
00278 
00279 /*
00280 **** SetUp
00281 This function use the global variable: 
00282     tempON
00283     tempOFF
00284 This function use this definitions:     
00285     tUp 
00286     tDown  
00287     tRigth 
00288     tLeft    
00289 */
00290 void SetUp(void)
00291 { 
00292     // TurnOn the LCD backlight of the LCD1602 board
00293     backlight = ON;  // ON: 1, OFF: 0
00294     
00295     // Show the SetUp related to:
00296     // TempMin (tempON) & TempMax (tempOFF)
00297     lcd.cls();
00298     lcd.locate(0,0);
00299     lcd.printf("Tm%2.2f TM%2.2f ", tempON, tempOFF);
00300 
00301     // Awaiting the release of the Left Button
00302     lcd.locate(0,1);
00303     lcd.printf("Release LEFT but");    
00304     while (ReadButtonOnLCD1602() == tLeft);
00305     wait_ms(100);
00306     // Awaiting the pressure of the Left Button
00307     lcd.locate(0,1);    
00308     lcd.printf("Press LEFT butt.");
00309     // Awaiting the release of the Left Button    
00310     while (ReadButtonOnLCD1602() != tLeft);    
00311     
00312     // Menù for SetUp the TempMin (tempON)
00313     lcd.cls();
00314     lcd.locate(0,0);
00315     lcd.printf("TempMin %2.2f", tempON);
00316     lcd.locate(0,1);
00317     lcd.printf("UP DOWN RIGHT");
00318     // Awaiting the pressure of Right Button to finish this part
00319     while (ReadButtonOnLCD1602() != tRigth)
00320     { 
00321         // Increase value
00322         if (ReadButtonOnLCD1602() == tUp)
00323             {
00324             tempON = tempON + float(tStep);
00325             // Awaiting the release of the tUp Button
00326             while (ReadButtonOnLCD1602() == tUp);
00327             }
00328         // Decrease value
00329         if (ReadButtonOnLCD1602() == tDown)
00330             {
00331             tempON = tempON - float(tStep);
00332             // Awaiting the release of the tDown Button
00333             while (ReadButtonOnLCD1602() == tDown);
00334             }
00335         lcd.locate(0,0);
00336         lcd.printf("TempMin %2.2f", tempON);
00337     }
00338     lcd.locate(0,1);
00339     lcd.printf("Release RIGHT");
00340     // Awaiting the release of the tRight Button
00341     while (ReadButtonOnLCD1602() == tRigth);    
00342     wait_ms(100);
00343     
00344     // Menù for SetUp the TempMax (tempOFF)
00345     if (tempOFF <= tempON)
00346         tempOFF = tempON + float(tStep) + float(tStep);
00347     lcd.cls();
00348     lcd.locate(0,0);
00349     lcd.printf("TempMax %2.2f", tempOFF);
00350     lcd.locate(0,1);
00351     lcd.printf("UP DOWN LEFT");    
00352     // Awaiting the pressure of Left Button to finish this part
00353     while (ReadButtonOnLCD1602() != tLeft)
00354     { 
00355         // Increase value
00356         if (ReadButtonOnLCD1602() == tUp)
00357             {
00358             tempOFF = tempOFF + float(tStep);
00359             // Awaiting the release of the tUp Button
00360             while (ReadButtonOnLCD1602() == tUp);
00361             }
00362         // Decrease value
00363         if (ReadButtonOnLCD1602() == tDown)
00364             {
00365             tempOFF = tempOFF - float(tStep);
00366             // Awaiting the release of the tDown Button
00367             while (ReadButtonOnLCD1602() == tDown);
00368             }
00369         lcd.locate(0,0);
00370         lcd.printf("TempMax %2.2f", tempOFF);
00371     }        
00372     // Awaiting the release of the tLeft Button
00373     lcd.locate(0,1);    
00374     lcd.printf("Release LEFT but");
00375     while (ReadButtonOnLCD1602() == tLeft);
00376     
00377     lcd.cls();
00378     lcd.locate(0,0);
00379     lcd.printf("By patient....  ", tempON, tempOFF);
00380     
00381     return;
00382 }
00383 
00384 
00385 /*
00386 **** ReadButtonOnLCD1602
00387 This function Read the button on the LCD1620
00388 This function use the global variable: 
00389     value
00390     LastButtonPressed
00391 This function use this definitions:    
00392     TastoSU  
00393     TastoGIU 
00394     TastoDES 
00395     TastoSIN 
00396     DeltaTasti 
00397     tUp 
00398     tDown  
00399     tRigth 
00400     tLeft  
00401     tNone  
00402 */
00403 uint8_t ReadButtonOnLCD1602(void)
00404 { 
00405     value = button.read_u16();
00406     // pc.printf("Button value = %d\n\r",value); 
00407     
00408     if (value < (TastoSU + DeltaTasti) && value > (TastoSU - DeltaTasti))    // Button UP
00409     { 
00410         // pc.printf("UP\n\r");
00411         return tUp;
00412     }
00413     else if (value < (TastoGIU + DeltaTasti) && value > (TastoGIU - DeltaTasti))  // Button DOWN
00414     { 
00415         // pc.printf("DOWN\n\r");    
00416         return tDown;   
00417     }        
00418     else if (value < (TastoDES + DeltaTasti) && value <= (TastoDES - DeltaTasti))  // Button RIGHT
00419     { 
00420         // pc.printf("RIGHT\n\r");    
00421         return tRigth;          
00422     }
00423     else if (value < (TastoSIN + DeltaTasti) && value > (TastoSIN - DeltaTasti))  // Button LEFT 
00424     { 
00425         // pc.printf("LEFT\n\r");     
00426         return tLeft;   
00427     }  
00428     else           
00429         return tNone;     
00430 }
00431 
00432 
00433 /*
00434 This function is use for eliminate the wrong reading and 
00435 for setup the MAX and MIN temperature we have read.
00436 This function use the global variable:
00437     temperature
00438     MaxTempError
00439     temperaturePREVIOUS
00440     temperatureMAX
00441     temperatureMIN
00442 This function use this definition:
00443     MaxError
00444 */
00445 void CalcMed(void)
00446 {   
00447     // Remove a measure that exceed the +- MaxError
00448     MaxTempError = (temperature * MaxError) / 100;
00449     if (temperature > (temperature + MaxTempError))
00450         temperature = temperaturePREVIOUS;
00451     else if (temperature < (temperature - MaxTempError))
00452         temperature = temperaturePREVIOUS;    
00453     else
00454         temperaturePREVIOUS = temperature;
00455     
00456     // SetUp the Max and Min temperature measured
00457     if (temperature > temperatureMAX)
00458         temperatureMAX = temperature;
00459     if (temperature < temperatureMIN)
00460         temperatureMIN = temperature;
00461 }
00462 
00463 
00464 /*
00465 This function Display temperatures on the PC
00466 */
00467 void PrintTemperatures(void)
00468 {
00469     pc.printf("TEMPERATURE =====> %2.3f Centigrade/Centigradi\r\n", temperature); 
00470     // pc.printf("DeltaTemp = %2.3f\r\n", DeltaTemp); 
00471     pc.printf("tempON  = %2.3f\r\n", tempON); 
00472     pc.printf("tempOFF = %2.3f\r\n", tempOFF); 
00473     pc.printf("RELE' is = %d\r\n", rele);
00474     pc.printf("MaxTempError = %2.3f\r\n", MaxTempError);
00475     pc.printf("temperaturePREVIOUS = %2.3f\r\n", temperaturePREVIOUS); 
00476     pc.printf("temperatureMAX = %2.3f\r\n", temperatureMAX); 
00477     pc.printf("temperatureMIN = %2.3f\r\n\r\n", temperatureMIN); 
00478 }