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@41:978c2d85a0e7, 2018-09-21 (annotated)
- Committer:
- andrewklmn
- Date:
- Fri Sep 21 19:23:45 2018 +0000
- Revision:
- 41:978c2d85a0e7
- Parent:
- 39:aa5f95061409
- Child:
- 42:03cbc2a6f63b
123
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 | 27:007e17df5ba0 | 4 | #include <stdio.h> |
andrewklmn | 17:718e41e971f8 | 5 | #include "DS1820.h" |
andrewklmn | 26:750f21025bb9 | 6 | #include "PinDetect.h" |
andrewklmn | 31:0a516ceaec4d | 7 | #include "Watchdog.h" |
andrewklmn | 35:74a546766adb | 8 | #include "eeprom_flash.h" |
andrewklmn | 19:74f78a777ffa | 9 | #include <string> |
andrewklmn | 38:a0753c2a4497 | 10 | #include "temp_controller.h" |
andrewklmn | 12:187e02ab447f | 11 | |
andrewklmn | 27:007e17df5ba0 | 12 | // Create a queue that can hold a maximum of 32 events |
andrewklmn | 27:007e17df5ba0 | 13 | EventQueue queue(32 * EVENTS_EVENT_SIZE); |
andrewklmn | 27:007e17df5ba0 | 14 | // Create a thread that'll run the event queue's dispatch function |
andrewklmn | 27:007e17df5ba0 | 15 | //Thread t; |
andrewklmn | 27:007e17df5ba0 | 16 | |
andrewklmn | 32:aebbfe391bbc | 17 | Watchdog wd; |
andrewklmn | 38:a0753c2a4497 | 18 | unsigned int eeprom_config_value; |
andrewklmn | 41:978c2d85a0e7 | 19 | extern char working_mode; |
andrewklmn | 27:007e17df5ba0 | 20 | |
andrewklmn | 39:aa5f95061409 | 21 | Serial pc(PA_2, PA_3); // (TX, RX) Pin definition |
andrewklmn | 12:187e02ab447f | 22 | char a[128] = ""; // RX command buffer |
andrewklmn | 12:187e02ab447f | 23 | char i = 0; // RX char pointer |
andrewklmn | 14:d954558361b9 | 24 | static char recieved = 0; |
andrewklmn | 12:187e02ab447f | 25 | |
andrewklmn | 38:a0753c2a4497 | 26 | |
andrewklmn | 23:33d6689834c6 | 27 | DigitalOut myled(LED1); |
andrewklmn | 17:718e41e971f8 | 28 | |
andrewklmn | 34:09d8edcce624 | 29 | DigitalOut pomp_OFF(PA_12); // pomp off |
andrewklmn | 34:09d8edcce624 | 30 | DigitalOut heater_OFF(PA_15); // heater off |
andrewklmn | 34:09d8edcce624 | 31 | |
andrewklmn | 12:187e02ab447f | 32 | // This function is called when a character goes into the RX buffer. |
andrewklmn | 12:187e02ab447f | 33 | void rxCallback() { |
andrewklmn | 12:187e02ab447f | 34 | char c; |
andrewklmn | 14:d954558361b9 | 35 | c = pc.getc(); |
andrewklmn | 12:187e02ab447f | 36 | if (recieved == 0){ // skip if command was already received |
andrewklmn | 12:187e02ab447f | 37 | if ( c == 0x0d || c == 0x0a ) { |
andrewklmn | 29:5940a80717e4 | 38 | while ( pc.readable() ) c = pc.getc(); |
andrewklmn | 12:187e02ab447f | 39 | recieved = 1; |
andrewklmn | 12:187e02ab447f | 40 | } else { |
andrewklmn | 12:187e02ab447f | 41 | if (i==16){ // max length of command from SERIAL |
andrewklmn | 12:187e02ab447f | 42 | a[0] = c; |
andrewklmn | 12:187e02ab447f | 43 | a[1] = '\0'; |
andrewklmn | 12:187e02ab447f | 44 | i = 0; |
andrewklmn | 12:187e02ab447f | 45 | } else { |
andrewklmn | 12:187e02ab447f | 46 | a[i] = c; |
andrewklmn | 12:187e02ab447f | 47 | i++; |
andrewklmn | 12:187e02ab447f | 48 | a[i] = '\0'; |
andrewklmn | 12:187e02ab447f | 49 | }; |
andrewklmn | 14:d954558361b9 | 50 | }; |
andrewklmn | 12:187e02ab447f | 51 | }; |
andrewklmn | 12:187e02ab447f | 52 | }; |
andrewklmn | 12:187e02ab447f | 53 | |
andrewklmn | 23:33d6689834c6 | 54 | |
andrewklmn | 23:33d6689834c6 | 55 | void pb_hit_interrupt (void) { |
andrewklmn | 38:a0753c2a4497 | 56 | |
andrewklmn | 37:c9b96afec535 | 57 | //writeEEPROMWord(0, 666 ); |
andrewklmn | 38:a0753c2a4497 | 58 | //epprom_config_value++; |
andrewklmn | 41:978c2d85a0e7 | 59 | working_mode ++; |
andrewklmn | 41:978c2d85a0e7 | 60 | if (working_mode == 6) working_mode=0; |
andrewklmn | 41:978c2d85a0e7 | 61 | |
andrewklmn | 41:978c2d85a0e7 | 62 | pc.printf("working_mode %d \r\n", working_mode); |
andrewklmn | 41:978c2d85a0e7 | 63 | //pc.printf("Button pressed \r\n"); |
andrewklmn | 23:33d6689834c6 | 64 | }; |
andrewklmn | 25:49e5d653d789 | 65 | |
andrewklmn | 25:49e5d653d789 | 66 | void pb_out_interrupt (void) { |
andrewklmn | 27:007e17df5ba0 | 67 | pc.printf("Button unpressed\r\n"); |
andrewklmn | 25:49e5d653d789 | 68 | }; |
andrewklmn | 27:007e17df5ba0 | 69 | |
andrewklmn | 38:a0753c2a4497 | 70 | |
andrewklmn | 27:007e17df5ba0 | 71 | void at_command(){ |
andrewklmn | 27:007e17df5ba0 | 72 | if (recieved == 1) { |
andrewklmn | 27:007e17df5ba0 | 73 | |
andrewklmn | 32:aebbfe391bbc | 74 | __disable_irq(); |
andrewklmn | 32:aebbfe391bbc | 75 | |
andrewklmn | 27:007e17df5ba0 | 76 | // command was recieved |
andrewklmn | 32:aebbfe391bbc | 77 | |
andrewklmn | 32:aebbfe391bbc | 78 | if (a[0]=='A' && a[1]=='T') { |
andrewklmn | 32:aebbfe391bbc | 79 | |
andrewklmn | 32:aebbfe391bbc | 80 | if ( a[2]=='\0') { |
andrewklmn | 32:aebbfe391bbc | 81 | //pc.printf(a); |
andrewklmn | 38:a0753c2a4497 | 82 | pc.printf("RAM=%u FLASH=%u OK\r\n", eeprom_config_value, readEEPROMWord(0)); |
andrewklmn | 33:2c54746f7518 | 83 | } else { |
andrewklmn | 39:aa5f95061409 | 84 | pc.printf("Wrong command\r\n"); |
andrewklmn | 32:aebbfe391bbc | 85 | }; |
andrewklmn | 37:c9b96afec535 | 86 | |
andrewklmn | 37:c9b96afec535 | 87 | //readEEPROMWord(0x01); |
andrewklmn | 32:aebbfe391bbc | 88 | } else { |
andrewklmn | 39:aa5f95061409 | 89 | pc.printf("Bad request\r\n"); |
andrewklmn | 32:aebbfe391bbc | 90 | }; |
andrewklmn | 27:007e17df5ba0 | 91 | |
andrewklmn | 27:007e17df5ba0 | 92 | // process_command(a); |
andrewklmn | 27:007e17df5ba0 | 93 | |
andrewklmn | 32:aebbfe391bbc | 94 | __enable_irq(); |
andrewklmn | 32:aebbfe391bbc | 95 | |
andrewklmn | 27:007e17df5ba0 | 96 | // ready for new command |
andrewklmn | 27:007e17df5ba0 | 97 | recieved = 0; |
andrewklmn | 27:007e17df5ba0 | 98 | a[0] = '\0'; |
andrewklmn | 27:007e17df5ba0 | 99 | i = 0; |
andrewklmn | 27:007e17df5ba0 | 100 | }; |
andrewklmn | 27:007e17df5ba0 | 101 | }; |
andrewklmn | 39:aa5f95061409 | 102 | |
hudakz | 10:4b88be251088 | 103 | |
hudakz | 0:ab218237069e | 104 | int main() { |
andrewklmn | 12:187e02ab447f | 105 | |
hudakz | 10:4b88be251088 | 106 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
andrewklmn | 37:c9b96afec535 | 107 | |
andrewklmn | 38:a0753c2a4497 | 108 | eeprom_config_value = get_temp_config_value(); |
andrewklmn | 38:a0753c2a4497 | 109 | |
andrewklmn | 37:c9b96afec535 | 110 | wd.Configure(10.0); |
andrewklmn | 32:aebbfe391bbc | 111 | |
andrewklmn | 26:750f21025bb9 | 112 | PinDetect pb(PA_11); |
andrewklmn | 23:33d6689834c6 | 113 | pb.mode(PullUp); |
andrewklmn | 26:750f21025bb9 | 114 | |
andrewklmn | 26:750f21025bb9 | 115 | // Delay for initial pullup to take effect |
andrewklmn | 26:750f21025bb9 | 116 | wait(.005); |
andrewklmn | 26:750f21025bb9 | 117 | |
andrewklmn | 26:750f21025bb9 | 118 | pb.attach_deasserted(&pb_hit_interrupt); |
andrewklmn | 26:750f21025bb9 | 119 | pb.attach_asserted(&pb_out_interrupt); |
andrewklmn | 26:750f21025bb9 | 120 | //pb.rise(&pb_out_interrupt); |
andrewklmn | 26:750f21025bb9 | 121 | pb.setSampleFrequency(); |
andrewklmn | 22:32beca745706 | 122 | |
andrewklmn | 12:187e02ab447f | 123 | pc.attach(&rxCallback, Serial::RxIrq); |
andrewklmn | 15:721546347507 | 124 | pc.baud(115200); |
andrewklmn | 39:aa5f95061409 | 125 | pc.printf("\r\nNaumovich 2.0\r\n"); |
andrewklmn | 19:74f78a777ffa | 126 | |
andrewklmn | 27:007e17df5ba0 | 127 | |
andrewklmn | 34:09d8edcce624 | 128 | pomp_OFF = 0; |
andrewklmn | 34:09d8edcce624 | 129 | heater_OFF = 0; |
andrewklmn | 34:09d8edcce624 | 130 | wait(0.25); |
andrewklmn | 34:09d8edcce624 | 131 | pomp_OFF = 1; |
andrewklmn | 34:09d8edcce624 | 132 | heater_OFF = 1; |
andrewklmn | 34:09d8edcce624 | 133 | wait(0.25); |
andrewklmn | 34:09d8edcce624 | 134 | pomp_OFF = 0; |
andrewklmn | 34:09d8edcce624 | 135 | heater_OFF = 0; |
andrewklmn | 34:09d8edcce624 | 136 | wait(0.25); |
andrewklmn | 34:09d8edcce624 | 137 | pomp_OFF = 1; |
andrewklmn | 34:09d8edcce624 | 138 | heater_OFF = 1; |
andrewklmn | 34:09d8edcce624 | 139 | wait(0.25); |
andrewklmn | 34:09d8edcce624 | 140 | |
andrewklmn | 17:718e41e971f8 | 141 | |
andrewklmn | 27:007e17df5ba0 | 142 | queue.call( start_temp ); |
andrewklmn | 27:007e17df5ba0 | 143 | queue.call_every(100, at_command); |
andrewklmn | 37:c9b96afec535 | 144 | queue.call_every(2000, check_temp); |
andrewklmn | 41:978c2d85a0e7 | 145 | queue.call_every(500, process_temp); |
andrewklmn | 27:007e17df5ba0 | 146 | |
andrewklmn | 27:007e17df5ba0 | 147 | // Start queue |
andrewklmn | 27:007e17df5ba0 | 148 | queue.dispatch(); |
andrewklmn | 22:32beca745706 | 149 | }; |
hudakz | 8:f1432e9af6c8 | 150 |