STM32F103C8T6_WIFI_Heating_system

Dependencies:   mbed mbed-STM32F103C8T6 eeprom_flash Watchdog PinDetect DS1820

  1. Bluepill STM32F103C8T6 Heating system
    1. _This project is core part of bigger heating system project!_

Features - Reading temperature from four DS18B20 sensors - Making a decision about switching on/off heater and pomp - Executing simple user commands from UART - Storing state parameters to program memory (EEPROM emulation)

Committer:
andrewklmn
Date:
Fri Sep 21 19:23:45 2018 +0000
Revision:
41:978c2d85a0e7
Parent:
40:6168609574d0
Child:
42:03cbc2a6f63b
123

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewklmn 38:a0753c2a4497 1 #include "temp_controller.h"
andrewklmn 38:a0753c2a4497 2
andrewklmn 38:a0753c2a4497 3 extern Serial pc;
andrewklmn 38:a0753c2a4497 4 extern Watchdog wd;
andrewklmn 38:a0753c2a4497 5 extern DigitalOut myled;
andrewklmn 39:aa5f95061409 6 extern DigitalOut pomp_OFF;
andrewklmn 39:aa5f95061409 7 extern DigitalOut heater_OFF;
andrewklmn 39:aa5f95061409 8
andrewklmn 38:a0753c2a4497 9 extern unsigned int eeprom_config_value;
andrewklmn 38:a0753c2a4497 10
andrewklmn 38:a0753c2a4497 11 #define MIN_MEBEL_TEMP 15
andrewklmn 38:a0753c2a4497 12 #define MIN_LITOS_TEMP 10
andrewklmn 38:a0753c2a4497 13 #define MIN_BACK_WATER_TEMP 10
andrewklmn 38:a0753c2a4497 14 #define MAX_HOT_WATER_TEMP 35
andrewklmn 38:a0753c2a4497 15
andrewklmn 38:a0753c2a4497 16 //unsigned int epprom_config_value = 0x0F0A0A23;
andrewklmn 38:a0753c2a4497 17 /*
andrewklmn 38:a0753c2a4497 18 epprom_config_value:
andrewklmn 38:a0753c2a4497 19 1 unsigned char - Min mebel temp (default +15)
andrewklmn 38:a0753c2a4497 20 2 unsigned char Min litos temp (default +10)
andrewklmn 38:a0753c2a4497 21 3 unsigned char Min back water temp (default +10)
andrewklmn 38:a0753c2a4497 22 4 unsigned char Max hot water temp (default +35)
andrewklmn 38:a0753c2a4497 23 */
andrewklmn 38:a0753c2a4497 24
andrewklmn 38:a0753c2a4497 25 float temp[5] = {
andrewklmn 39:aa5f95061409 26 // initial temperature is maximum
andrewklmn 39:aa5f95061409 27 85, // OUTDOOR
andrewklmn 39:aa5f95061409 28 85, // LITOS
andrewklmn 39:aa5f95061409 29 85, // MEBEL
andrewklmn 39:aa5f95061409 30 85, // HOT WATER
andrewklmn 39:aa5f95061409 31 85 // BACK WATER
andrewklmn 39:aa5f95061409 32 };
andrewklmn 39:aa5f95061409 33
andrewklmn 39:aa5f95061409 34 float simulated_temp[5] = {
andrewklmn 39:aa5f95061409 35 // initial temperature for simulator
andrewklmn 39:aa5f95061409 36 7, // OUTDOOR
andrewklmn 39:aa5f95061409 37 10, // LITOS
andrewklmn 39:aa5f95061409 38 14, // MEBEL
andrewklmn 39:aa5f95061409 39 33, // HOT WATER
andrewklmn 39:aa5f95061409 40 18 // BACK WATER
andrewklmn 38:a0753c2a4497 41 };
andrewklmn 38:a0753c2a4497 42
andrewklmn 38:a0753c2a4497 43 int temp_error[5] = {
andrewklmn 39:aa5f95061409 44 1,1,1,1,1 // initial state is no sensors
andrewklmn 39:aa5f95061409 45 // 0 - sensor present
andrewklmn 39:aa5f95061409 46 // 1 - no sensor
andrewklmn 39:aa5f95061409 47 // 2 - sensor CRC error
andrewklmn 39:aa5f95061409 48 };
andrewklmn 38:a0753c2a4497 49
andrewklmn 38:a0753c2a4497 50 string labels[5] = {
andrewklmn 38:a0753c2a4497 51 "OUTDOOR",
andrewklmn 38:a0753c2a4497 52 "LITOS",
andrewklmn 38:a0753c2a4497 53 "MEBEL",
andrewklmn 38:a0753c2a4497 54 "HOT WATER",
andrewklmn 38:a0753c2a4497 55 "BACK WATER"
andrewklmn 38:a0753c2a4497 56 };
andrewklmn 38:a0753c2a4497 57
andrewklmn 38:a0753c2a4497 58 DS1820 ds1820[5] = {
andrewklmn 39:aa5f95061409 59 DS1820(PA_9), // substitute PA_9 with actual mbed pin name connected to the OUTDOOR
andrewklmn 39:aa5f95061409 60 DS1820(PA_8), // substitute PA_8 with actual mbed pin name connected to the INDOOR LITOS
andrewklmn 39:aa5f95061409 61 DS1820(PA_7), // substitute PA_7 with actual mbed pin name connected to the INDOOR MEBEL
andrewklmn 39:aa5f95061409 62 DS1820(PA_6), // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 39:aa5f95061409 63 DS1820(PA_5) // substitute PA_6 with actual mbed pin name connected to the HOT WATER
andrewklmn 39:aa5f95061409 64 };
andrewklmn 38:a0753c2a4497 65
andrewklmn 39:aa5f95061409 66 unsigned char working_mode = 1; // default mode after powerup is ECO
andrewklmn 39:aa5f95061409 67 // 0 - OFF - heating off, pomp and heater are OFF
andrewklmn 39:aa5f95061409 68 // 1 - ECO - eco heating
andrewklmn 39:aa5f95061409 69 // 2 - STANDART - standart heating
andrewklmn 39:aa5f95061409 70 // 3 - SIMULATOR - simulator mode - work on simulated_temp
andrewklmn 39:aa5f95061409 71 // 4 - POMP - pomp is ON, heater is OFF
andrewklmn 39:aa5f95061409 72 // 5 - FULL - pomp and heater are ON
andrewklmn 38:a0753c2a4497 73
andrewklmn 38:a0753c2a4497 74 void start_temp(){
andrewklmn 38:a0753c2a4497 75
andrewklmn 38:a0753c2a4497 76 __disable_irq();
andrewklmn 38:a0753c2a4497 77
andrewklmn 38:a0753c2a4497 78 for ( int j=0; j < 5; j++ ) {
andrewklmn 38:a0753c2a4497 79 if(ds1820[j].begin()) {
andrewklmn 38:a0753c2a4497 80 ds1820[j].startConversion();
andrewklmn 39:aa5f95061409 81 //pc.printf("%s sensor present!\r\n", labels[j].c_str());
andrewklmn 39:aa5f95061409 82 };// else {
andrewklmn 39:aa5f95061409 83 //pc.printf("No %s sensor found!\r\n", labels[j].c_str());
andrewklmn 39:aa5f95061409 84 //};
andrewklmn 38:a0753c2a4497 85 };
andrewklmn 38:a0753c2a4497 86 __enable_irq();
andrewklmn 38:a0753c2a4497 87
andrewklmn 38:a0753c2a4497 88 };
andrewklmn 38:a0753c2a4497 89
andrewklmn 38:a0753c2a4497 90 void check_temp(){
andrewklmn 38:a0753c2a4497 91
andrewklmn 38:a0753c2a4497 92 myled = 0; // turn the LED on
andrewklmn 38:a0753c2a4497 93
andrewklmn 38:a0753c2a4497 94 // kick the watchdog
andrewklmn 38:a0753c2a4497 95 wd.Service();
andrewklmn 38:a0753c2a4497 96
andrewklmn 38:a0753c2a4497 97 __disable_irq();
andrewklmn 38:a0753c2a4497 98
andrewklmn 38:a0753c2a4497 99 for ( int j=0; j < 5; j++ ) {
andrewklmn 38:a0753c2a4497 100
andrewklmn 38:a0753c2a4497 101 temp_error[j] = ds1820[j].read(temp[j]); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 41:978c2d85a0e7 102 ds1820[j].startConversion(); // start temperature conversion from analog to digital before next reading
andrewklmn 39:aa5f95061409 103 };
andrewklmn 38:a0753c2a4497 104
andrewklmn 38:a0753c2a4497 105
andrewklmn 38:a0753c2a4497 106 // save new config if it was changed
andrewklmn 38:a0753c2a4497 107 if (readEEPROMWord(0)!= eeprom_config_value) {
andrewklmn 39:aa5f95061409 108 save_new_config();
andrewklmn 38:a0753c2a4497 109 };
andrewklmn 38:a0753c2a4497 110
andrewklmn 38:a0753c2a4497 111
andrewklmn 38:a0753c2a4497 112
andrewklmn 38:a0753c2a4497 113 __enable_irq();
andrewklmn 38:a0753c2a4497 114
andrewklmn 38:a0753c2a4497 115 myled = 1; // turn the LED off
andrewklmn 38:a0753c2a4497 116 };
andrewklmn 38:a0753c2a4497 117
andrewklmn 39:aa5f95061409 118
andrewklmn 39:aa5f95061409 119 void process_temp() {
andrewklmn 39:aa5f95061409 120 switch(working_mode) {
andrewklmn 39:aa5f95061409 121 case 0:
andrewklmn 39:aa5f95061409 122 // 0 - OFF - heating off, pomp and heater are OFF
andrewklmn 39:aa5f95061409 123 pomp_OFF = 1;
andrewklmn 39:aa5f95061409 124 heater_OFF = 1;
andrewklmn 39:aa5f95061409 125 break;
andrewklmn 39:aa5f95061409 126 case 1:
andrewklmn 39:aa5f95061409 127 // 1 - ECO - eco heating
andrewklmn 39:aa5f95061409 128 pc.printf("State %d|%d|%d|%d|%d\r\n", temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4] );
andrewklmn 39:aa5f95061409 129 pc.printf("Temp %3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4] );
andrewklmn 39:aa5f95061409 130 pc.printf("RAM_config=%X, FLASH_config=%X\r\n", eeprom_config_value ,readEEPROMWord(0));
andrewklmn 39:aa5f95061409 131 pc.printf("=======================================");
andrewklmn 39:aa5f95061409 132 pomp_OFF = !pomp_OFF;
andrewklmn 39:aa5f95061409 133 heater_OFF = !heater_OFF;
andrewklmn 39:aa5f95061409 134 break;
andrewklmn 39:aa5f95061409 135 case 2:
andrewklmn 39:aa5f95061409 136 // 2 - STANDART - standart heating
andrewklmn 39:aa5f95061409 137 pomp_OFF = 1;
andrewklmn 39:aa5f95061409 138 heater_OFF = 1;
andrewklmn 39:aa5f95061409 139 break;
andrewklmn 39:aa5f95061409 140 case 3:
andrewklmn 39:aa5f95061409 141 // 3 - SIMULATOR - simulator mode - work on simulated_temp
andrewklmn 39:aa5f95061409 142 pomp_OFF = 1;
andrewklmn 39:aa5f95061409 143 heater_OFF = 1;
andrewklmn 39:aa5f95061409 144 break;
andrewklmn 39:aa5f95061409 145 case 4:
andrewklmn 39:aa5f95061409 146 // 4 - POMP - pomp is ON, heater is OFF
andrewklmn 40:6168609574d0 147 pomp_OFF = 0;
andrewklmn 39:aa5f95061409 148 heater_OFF = 1;
andrewklmn 39:aa5f95061409 149 break;
andrewklmn 39:aa5f95061409 150 case 5:
andrewklmn 39:aa5f95061409 151 // 5 - FULL - pomp and heater are ON
andrewklmn 41:978c2d85a0e7 152 pomp_OFF = 0;
andrewklmn 41:978c2d85a0e7 153 heater_OFF = 0;
andrewklmn 39:aa5f95061409 154 break;
andrewklmn 39:aa5f95061409 155 default:
andrewklmn 39:aa5f95061409 156 pomp_OFF = 1;
andrewklmn 39:aa5f95061409 157 heater_OFF = 1;
andrewklmn 39:aa5f95061409 158 };
andrewklmn 39:aa5f95061409 159
andrewklmn 39:aa5f95061409 160 };
andrewklmn 39:aa5f95061409 161
andrewklmn 39:aa5f95061409 162
andrewklmn 39:aa5f95061409 163 void save_new_config() {
andrewklmn 39:aa5f95061409 164
andrewklmn 39:aa5f95061409 165 enableEEPROMWriting();
andrewklmn 39:aa5f95061409 166 writeEEPROMWord(0,eeprom_config_value);
andrewklmn 39:aa5f95061409 167 disableEEPROMWriting();
andrewklmn 39:aa5f95061409 168
andrewklmn 39:aa5f95061409 169 };
andrewklmn 39:aa5f95061409 170
andrewklmn 39:aa5f95061409 171
andrewklmn 38:a0753c2a4497 172 unsigned int get_temp_config_value(){
andrewklmn 38:a0753c2a4497 173
andrewklmn 38:a0753c2a4497 174 unsigned int v = readEEPROMWord(0);
andrewklmn 38:a0753c2a4497 175 if ( v == 0xFFFFFFFF ) {
andrewklmn 38:a0753c2a4497 176 // if eeprom config value is not initialized
andrewklmn 38:a0753c2a4497 177 // set default value 0x0F0A0A23;
andrewklmn 38:a0753c2a4497 178 v = MIN_MEBEL_TEMP;
andrewklmn 38:a0753c2a4497 179 v = ( v << 8 ) | MIN_LITOS_TEMP;
andrewklmn 38:a0753c2a4497 180 v = ( v << 8 ) | MIN_BACK_WATER_TEMP;
andrewklmn 38:a0753c2a4497 181 v = ( v << 8 ) | MAX_HOT_WATER_TEMP;
andrewklmn 38:a0753c2a4497 182 };
andrewklmn 38:a0753c2a4497 183 return v;
andrewklmn 38:a0753c2a4497 184 };