STM32F103C8T6_WIFI_Heating_system
Dependencies: mbed mbed-STM32F103C8T6 eeprom_flash Watchdog PinDetect DS1820
- Bluepill STM32F103C8T6 Heating system
- _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)
main.cpp@51:881a30217532, 2018-09-24 (annotated)
- Committer:
- andrewklmn
- Date:
- Mon Sep 24 14:57:38 2018 +0000
- Revision:
- 51:881a30217532
- Parent:
- 50:94730b99ea41
- Child:
- 53:b9620bb0608e
uart 19200
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 5:3c3ef17a17a6 | 1 | #include "stm32f103c8t6.h" |
hudakz | 0:ab218237069e | 2 | #include "mbed.h" |
andrewklmn | 27:007e17df5ba0 | 3 | #include "mbed_events.h" |
andrewklmn | 17:718e41e971f8 | 4 | #include "DS1820.h" |
andrewklmn | 26:750f21025bb9 | 5 | #include "PinDetect.h" |
andrewklmn | 31:0a516ceaec4d | 6 | #include "Watchdog.h" |
andrewklmn | 35:74a546766adb | 7 | #include "eeprom_flash.h" |
andrewklmn | 19:74f78a777ffa | 8 | #include <string> |
andrewklmn | 38:a0753c2a4497 | 9 | #include "temp_controller.h" |
andrewklmn | 12:187e02ab447f | 10 | |
andrewklmn | 27:007e17df5ba0 | 11 | // Create a queue that can hold a maximum of 32 events |
andrewklmn | 27:007e17df5ba0 | 12 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
andrewklmn | 27:007e17df5ba0 | 13 | // Create a thread that'll run the event queue's dispatch function |
andrewklmn | 27:007e17df5ba0 | 14 | //Thread t; |
andrewklmn | 27:007e17df5ba0 | 15 | |
andrewklmn | 32:aebbfe391bbc | 16 | Watchdog wd; |
andrewklmn | 38:a0753c2a4497 | 17 | unsigned int eeprom_config_value; |
andrewklmn | 41:978c2d85a0e7 | 18 | extern char working_mode; |
andrewklmn | 42:03cbc2a6f63b | 19 | extern int temp_error[]; |
andrewklmn | 42:03cbc2a6f63b | 20 | extern float temp[]; |
andrewklmn | 43:cc47a2e6ae07 | 21 | extern float simulated_temp[]; |
andrewklmn | 27:007e17df5ba0 | 22 | |
andrewklmn | 39:aa5f95061409 | 23 | Serial pc(PA_2, PA_3); // (TX, RX) Pin definition |
andrewklmn | 12:187e02ab447f | 24 | char a[128] = ""; // RX command buffer |
andrewklmn | 12:187e02ab447f | 25 | char i = 0; // RX char pointer |
andrewklmn | 14:d954558361b9 | 26 | static char recieved = 0; |
andrewklmn | 12:187e02ab447f | 27 | |
andrewklmn | 38:a0753c2a4497 | 28 | |
andrewklmn | 23:33d6689834c6 | 29 | DigitalOut myled(LED1); |
andrewklmn | 17:718e41e971f8 | 30 | |
andrewklmn | 34:09d8edcce624 | 31 | DigitalOut pomp_OFF(PA_12); // pomp off |
andrewklmn | 34:09d8edcce624 | 32 | DigitalOut heater_OFF(PA_15); // heater off |
andrewklmn | 34:09d8edcce624 | 33 | |
andrewklmn | 12:187e02ab447f | 34 | // This function is called when a character goes into the RX buffer. |
andrewklmn | 12:187e02ab447f | 35 | void rxCallback() { |
andrewklmn | 12:187e02ab447f | 36 | char c; |
andrewklmn | 14:d954558361b9 | 37 | c = pc.getc(); |
andrewklmn | 12:187e02ab447f | 38 | if (recieved == 0){ // skip if command was already received |
andrewklmn | 12:187e02ab447f | 39 | if ( c == 0x0d || c == 0x0a ) { |
andrewklmn | 29:5940a80717e4 | 40 | while ( pc.readable() ) c = pc.getc(); |
andrewklmn | 12:187e02ab447f | 41 | recieved = 1; |
andrewklmn | 12:187e02ab447f | 42 | } else { |
andrewklmn | 43:cc47a2e6ae07 | 43 | if (i==127){ // max length of command from SERIAL |
andrewklmn | 12:187e02ab447f | 44 | a[0] = c; |
andrewklmn | 12:187e02ab447f | 45 | a[1] = '\0'; |
andrewklmn | 12:187e02ab447f | 46 | i = 0; |
andrewklmn | 12:187e02ab447f | 47 | } else { |
andrewklmn | 12:187e02ab447f | 48 | a[i] = c; |
andrewklmn | 12:187e02ab447f | 49 | i++; |
andrewklmn | 12:187e02ab447f | 50 | a[i] = '\0'; |
andrewklmn | 12:187e02ab447f | 51 | }; |
andrewklmn | 14:d954558361b9 | 52 | }; |
andrewklmn | 12:187e02ab447f | 53 | }; |
andrewklmn | 12:187e02ab447f | 54 | }; |
andrewklmn | 12:187e02ab447f | 55 | |
andrewklmn | 48:4ec055c066b4 | 56 | void blink_myled(char amount) { |
andrewklmn | 48:4ec055c066b4 | 57 | while (amount > 0) { |
andrewklmn | 48:4ec055c066b4 | 58 | myled = 0; |
andrewklmn | 48:4ec055c066b4 | 59 | wait(0.1); |
andrewklmn | 48:4ec055c066b4 | 60 | myled = 1; |
andrewklmn | 48:4ec055c066b4 | 61 | wait(0.2); |
andrewklmn | 48:4ec055c066b4 | 62 | amount--; |
andrewklmn | 48:4ec055c066b4 | 63 | }; |
andrewklmn | 48:4ec055c066b4 | 64 | }; |
andrewklmn | 23:33d6689834c6 | 65 | |
andrewklmn | 23:33d6689834c6 | 66 | void pb_hit_interrupt (void) { |
andrewklmn | 38:a0753c2a4497 | 67 | |
andrewklmn | 37:c9b96afec535 | 68 | //writeEEPROMWord(0, 666 ); |
andrewklmn | 38:a0753c2a4497 | 69 | //epprom_config_value++; |
andrewklmn | 41:978c2d85a0e7 | 70 | working_mode ++; |
andrewklmn | 48:4ec055c066b4 | 71 | blink_myled(working_mode); |
andrewklmn | 46:0d60fbcfb245 | 72 | if (working_mode == 7) working_mode=0; |
andrewklmn | 41:978c2d85a0e7 | 73 | |
andrewklmn | 48:4ec055c066b4 | 74 | //pc.printf("working_mode %d \r\n", working_mode); |
andrewklmn | 41:978c2d85a0e7 | 75 | //pc.printf("Button pressed \r\n"); |
andrewklmn | 23:33d6689834c6 | 76 | }; |
andrewklmn | 25:49e5d653d789 | 77 | |
andrewklmn | 25:49e5d653d789 | 78 | void pb_out_interrupt (void) { |
andrewklmn | 45:abc682827659 | 79 | //queue.call_in(2000, printf, "called in 2 seconds\n"); |
andrewklmn | 45:abc682827659 | 80 | //pc.printf("Button unpressed\r\n"); |
andrewklmn | 25:49e5d653d789 | 81 | }; |
andrewklmn | 27:007e17df5ba0 | 82 | |
andrewklmn | 38:a0753c2a4497 | 83 | |
andrewklmn | 27:007e17df5ba0 | 84 | void at_command(){ |
andrewklmn | 44:e6efa8be1f9e | 85 | int x; |
andrewklmn | 44:e6efa8be1f9e | 86 | int ii; |
andrewklmn | 44:e6efa8be1f9e | 87 | int jj; |
andrewklmn | 44:e6efa8be1f9e | 88 | char num[10]; |
andrewklmn | 44:e6efa8be1f9e | 89 | |
andrewklmn | 44:e6efa8be1f9e | 90 | |
andrewklmn | 27:007e17df5ba0 | 91 | if (recieved == 1) { |
andrewklmn | 27:007e17df5ba0 | 92 | |
andrewklmn | 32:aebbfe391bbc | 93 | __disable_irq(); |
andrewklmn | 44:e6efa8be1f9e | 94 | |
andrewklmn | 44:e6efa8be1f9e | 95 | if (strcmp(a,"AT get all")==0) { |
andrewklmn | 44:e6efa8be1f9e | 96 | // config_ram|config_rom|mode|status0|status1|status2|status3|status4|temp0|temp1|temp2|temp3|temp4 |
andrewklmn | 44:e6efa8be1f9e | 97 | pc.printf("%u|%u|", eeprom_config_value, readEEPROMWord(0)); |
andrewklmn | 47:4a955f381a1e | 98 | if (working_mode==3 || working_mode==4) { |
andrewklmn | 44:e6efa8be1f9e | 99 | pc.printf("%d|%d|%d|%d|%d|%d|", working_mode, 0, 0, 0, 0, 0); |
andrewklmn | 50:94730b99ea41 | 100 | pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f|%d|%d\r\n", |
andrewklmn | 50:94730b99ea41 | 101 | simulated_temp[0], |
andrewklmn | 50:94730b99ea41 | 102 | simulated_temp[1], |
andrewklmn | 50:94730b99ea41 | 103 | simulated_temp[2], |
andrewklmn | 50:94730b99ea41 | 104 | simulated_temp[3], |
andrewklmn | 50:94730b99ea41 | 105 | simulated_temp[4], |
andrewklmn | 50:94730b99ea41 | 106 | (uint8_t)pomp_OFF, |
andrewklmn | 50:94730b99ea41 | 107 | (uint8_t)heater_OFF |
andrewklmn | 50:94730b99ea41 | 108 | ); |
andrewklmn | 44:e6efa8be1f9e | 109 | } else { |
andrewklmn | 44:e6efa8be1f9e | 110 | pc.printf("%d|%d|%d|%d|%d|%d|", working_mode, temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4]); |
andrewklmn | 50:94730b99ea41 | 111 | pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f|%d|%d\r\n", temp[0], temp[1], temp[2], temp[3], temp[4], (uint8_t)pomp_OFF , (uint8_t)heater_OFF); |
andrewklmn | 44:e6efa8be1f9e | 112 | }; |
andrewklmn | 44:e6efa8be1f9e | 113 | } else if (strcmp(a,"AT")==0) { |
andrewklmn | 43:cc47a2e6ae07 | 114 | pc.printf("OK\r\n"); |
andrewklmn | 44:e6efa8be1f9e | 115 | } else if (strcmp(a,"AT get config")==0) { |
andrewklmn | 43:cc47a2e6ae07 | 116 | pc.printf("%u|%u\r\n", eeprom_config_value, readEEPROMWord(0)); |
andrewklmn | 42:03cbc2a6f63b | 117 | } else if (strcmp(a,"AT get status")==0) { |
andrewklmn | 50:94730b99ea41 | 118 | pc.printf("%d|%d|%d|%d|%d|%d|%d\r\n", temp_error[0], temp_error[1], temp_error[2], temp_error[3], temp_error[4], (uint8_t)pomp_OFF , (uint8_t)heater_OFF); |
andrewklmn | 42:03cbc2a6f63b | 119 | } else if (strcmp(a,"AT get temp")==0) { |
andrewklmn | 42:03cbc2a6f63b | 120 | pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", temp[0], temp[1], temp[2], temp[3], temp[4]); |
andrewklmn | 44:e6efa8be1f9e | 121 | } else if (strcmp(a,"AT get sim temp")==0) { |
andrewklmn | 43:cc47a2e6ae07 | 122 | pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", simulated_temp[0], simulated_temp[1], simulated_temp[2], simulated_temp[3], simulated_temp[4]); |
andrewklmn | 42:03cbc2a6f63b | 123 | } else if (strcmp(a,"AT get mode")==0) { |
andrewklmn | 42:03cbc2a6f63b | 124 | pc.printf("%d\r\n", working_mode); |
andrewklmn | 44:e6efa8be1f9e | 125 | } else if (strncmp(a,"AT set mode ",12) == 0) { |
andrewklmn | 44:e6efa8be1f9e | 126 | x= (int)a[12] - 48; |
andrewklmn | 50:94730b99ea41 | 127 | working_mode = ( x >= 0 && x <=6 ) ? x : working_mode; |
andrewklmn | 44:e6efa8be1f9e | 128 | pc.printf("%d\r\n", working_mode); |
andrewklmn | 44:e6efa8be1f9e | 129 | } else if (strncmp(a,"AT set sim temp ", 16) == 0) { |
andrewklmn | 44:e6efa8be1f9e | 130 | // from a[16] "t0|t1|t2|t3|t4\0" |
andrewklmn | 44:e6efa8be1f9e | 131 | ii = 16; |
andrewklmn | 44:e6efa8be1f9e | 132 | for (int j=0; j<6; j++) { |
andrewklmn | 44:e6efa8be1f9e | 133 | jj = 0; |
andrewklmn | 44:e6efa8be1f9e | 134 | while ( a[ii]!='|' && a[ii]!='\0') { |
andrewklmn | 44:e6efa8be1f9e | 135 | num[jj] = a[ii]; |
andrewklmn | 44:e6efa8be1f9e | 136 | ii++; |
andrewklmn | 44:e6efa8be1f9e | 137 | jj++; |
andrewklmn | 44:e6efa8be1f9e | 138 | }; |
andrewklmn | 44:e6efa8be1f9e | 139 | ii++; |
andrewklmn | 44:e6efa8be1f9e | 140 | num[jj] = '\0'; |
andrewklmn | 44:e6efa8be1f9e | 141 | simulated_temp[j] = (float)atol(num); |
andrewklmn | 44:e6efa8be1f9e | 142 | }; |
andrewklmn | 44:e6efa8be1f9e | 143 | pc.printf("%3.1f|%3.1f|%3.1f|%3.1f|%3.1f\r\n", |
andrewklmn | 44:e6efa8be1f9e | 144 | simulated_temp[0], |
andrewklmn | 44:e6efa8be1f9e | 145 | simulated_temp[1], |
andrewklmn | 44:e6efa8be1f9e | 146 | simulated_temp[2], |
andrewklmn | 44:e6efa8be1f9e | 147 | simulated_temp[3], |
andrewklmn | 44:e6efa8be1f9e | 148 | simulated_temp[4] |
andrewklmn | 44:e6efa8be1f9e | 149 | ); |
andrewklmn | 42:03cbc2a6f63b | 150 | } else { |
andrewklmn | 42:03cbc2a6f63b | 151 | pc.printf("Bad request\r\n"); |
andrewklmn | 42:03cbc2a6f63b | 152 | }; |
andrewklmn | 42:03cbc2a6f63b | 153 | |
andrewklmn | 32:aebbfe391bbc | 154 | __enable_irq(); |
andrewklmn | 32:aebbfe391bbc | 155 | |
andrewklmn | 27:007e17df5ba0 | 156 | // ready for new command |
andrewklmn | 27:007e17df5ba0 | 157 | recieved = 0; |
andrewklmn | 27:007e17df5ba0 | 158 | a[0] = '\0'; |
andrewklmn | 27:007e17df5ba0 | 159 | i = 0; |
andrewklmn | 27:007e17df5ba0 | 160 | }; |
andrewklmn | 27:007e17df5ba0 | 161 | }; |
andrewklmn | 39:aa5f95061409 | 162 | |
hudakz | 10:4b88be251088 | 163 | |
hudakz | 0:ab218237069e | 164 | int main() { |
andrewklmn | 12:187e02ab447f | 165 | |
hudakz | 10:4b88be251088 | 166 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
andrewklmn | 37:c9b96afec535 | 167 | |
andrewklmn | 48:4ec055c066b4 | 168 | heater_OFF = 1; |
andrewklmn | 48:4ec055c066b4 | 169 | pomp_OFF = 1; |
andrewklmn | 48:4ec055c066b4 | 170 | |
andrewklmn | 38:a0753c2a4497 | 171 | eeprom_config_value = get_temp_config_value(); |
andrewklmn | 50:94730b99ea41 | 172 | working_mode = (char)get_mode_config_value(); |
andrewklmn | 38:a0753c2a4497 | 173 | |
andrewklmn | 37:c9b96afec535 | 174 | wd.Configure(10.0); |
andrewklmn | 32:aebbfe391bbc | 175 | |
andrewklmn | 26:750f21025bb9 | 176 | PinDetect pb(PA_11); |
andrewklmn | 23:33d6689834c6 | 177 | pb.mode(PullUp); |
andrewklmn | 26:750f21025bb9 | 178 | |
andrewklmn | 26:750f21025bb9 | 179 | // Delay for initial pullup to take effect |
andrewklmn | 26:750f21025bb9 | 180 | wait(.005); |
andrewklmn | 26:750f21025bb9 | 181 | |
andrewklmn | 26:750f21025bb9 | 182 | pb.attach_deasserted(&pb_hit_interrupt); |
andrewklmn | 26:750f21025bb9 | 183 | pb.attach_asserted(&pb_out_interrupt); |
andrewklmn | 26:750f21025bb9 | 184 | //pb.rise(&pb_out_interrupt); |
andrewklmn | 26:750f21025bb9 | 185 | pb.setSampleFrequency(); |
andrewklmn | 22:32beca745706 | 186 | |
andrewklmn | 12:187e02ab447f | 187 | pc.attach(&rxCallback, Serial::RxIrq); |
andrewklmn | 51:881a30217532 | 188 | pc.baud(19200); |
andrewklmn | 39:aa5f95061409 | 189 | pc.printf("\r\nNaumovich 2.0\r\n"); |
andrewklmn | 48:4ec055c066b4 | 190 | |
andrewklmn | 48:4ec055c066b4 | 191 | blink_myled(3); |
andrewklmn | 48:4ec055c066b4 | 192 | |
andrewklmn | 48:4ec055c066b4 | 193 | |
andrewklmn | 17:718e41e971f8 | 194 | |
andrewklmn | 27:007e17df5ba0 | 195 | queue.call( start_temp ); |
andrewklmn | 27:007e17df5ba0 | 196 | queue.call_every(100, at_command); |
andrewklmn | 37:c9b96afec535 | 197 | queue.call_every(2000, check_temp); |
andrewklmn | 41:978c2d85a0e7 | 198 | queue.call_every(500, process_temp); |
andrewklmn | 27:007e17df5ba0 | 199 | |
andrewklmn | 27:007e17df5ba0 | 200 | // Start queue |
andrewklmn | 27:007e17df5ba0 | 201 | queue.dispatch(); |
andrewklmn | 22:32beca745706 | 202 | }; |
hudakz | 8:f1432e9af6c8 | 203 |