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:
Wed Sep 12 17:40:02 2018 +0000
Revision:
18:9434e06223c7
Parent:
17:718e41e971f8
Child:
19:74f78a777ffa
second chanel DS1820

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 5:3c3ef17a17a6 1 #include "stm32f103c8t6.h"
hudakz 0:ab218237069e 2 #include "mbed.h"
andrewklmn 17:718e41e971f8 3 #include "DS1820.h"
andrewklmn 12:187e02ab447f 4
andrewklmn 12:187e02ab447f 5 char a[128] = ""; // RX command buffer
andrewklmn 12:187e02ab447f 6 char i = 0; // RX char pointer
andrewklmn 14:d954558361b9 7 static char recieved = 0;
andrewklmn 12:187e02ab447f 8
andrewklmn 17:718e41e971f8 9 Serial pc(PA_2, PA_3);
andrewklmn 17:718e41e971f8 10
andrewklmn 12:187e02ab447f 11 // This function is called when a character goes into the RX buffer.
andrewklmn 12:187e02ab447f 12 void rxCallback() {
andrewklmn 12:187e02ab447f 13 char c;
andrewklmn 14:d954558361b9 14 c = pc.getc();
andrewklmn 12:187e02ab447f 15 if (recieved == 0){ // skip if command was already received
andrewklmn 12:187e02ab447f 16 if ( c == 0x0d || c == 0x0a ) {
andrewklmn 12:187e02ab447f 17 recieved = 1;
andrewklmn 12:187e02ab447f 18 } else {
andrewklmn 12:187e02ab447f 19 if (i==16){ // max length of command from SERIAL
andrewklmn 12:187e02ab447f 20 a[0] = c;
andrewklmn 12:187e02ab447f 21 a[1] = '\0';
andrewklmn 12:187e02ab447f 22 i = 0;
andrewklmn 12:187e02ab447f 23 } else {
andrewklmn 12:187e02ab447f 24 a[i] = c;
andrewklmn 12:187e02ab447f 25 i++;
andrewklmn 12:187e02ab447f 26 a[i] = '\0';
andrewklmn 12:187e02ab447f 27 };
andrewklmn 14:d954558361b9 28 };
andrewklmn 12:187e02ab447f 29 };
andrewklmn 12:187e02ab447f 30 };
andrewklmn 12:187e02ab447f 31
hudakz 10:4b88be251088 32
hudakz 0:ab218237069e 33 int main() {
andrewklmn 12:187e02ab447f 34
hudakz 10:4b88be251088 35 confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock)
andrewklmn 12:187e02ab447f 36 DigitalOut myled(LED1);
hudakz 10:4b88be251088 37
andrewklmn 18:9434e06223c7 38 DS1820 ds1820(PA_9); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
andrewklmn 18:9434e06223c7 39 DS1820 ds1820_2(PA_8); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
andrewklmn 18:9434e06223c7 40
andrewklmn 17:718e41e971f8 41 float temp = 0;
andrewklmn 17:718e41e971f8 42 int error = 0;
andrewklmn 17:718e41e971f8 43
andrewklmn 12:187e02ab447f 44 pc.attach(&rxCallback, Serial::RxIrq);
andrewklmn 15:721546347507 45 pc.baud(115200);
andrewklmn 16:c7b77648311d 46 pc.printf("Hello world! Mbed version\r\n");
hudakz 10:4b88be251088 47
andrewklmn 17:718e41e971f8 48 if(ds1820.begin()) {
andrewklmn 18:9434e06223c7 49 pc.printf("DS1820 sensor1 found!\r\n");
andrewklmn 17:718e41e971f8 50 } else {
andrewklmn 18:9434e06223c7 51 pc.printf("No DS1820 sensor1 found!\r\n");
andrewklmn 17:718e41e971f8 52 };
andrewklmn 17:718e41e971f8 53
andrewklmn 18:9434e06223c7 54
andrewklmn 18:9434e06223c7 55 if(ds1820_2.begin()) {
andrewklmn 18:9434e06223c7 56 pc.printf("DS1820 sensor2 found!\r\n");
andrewklmn 18:9434e06223c7 57 } else {
andrewklmn 18:9434e06223c7 58 pc.printf("No DS1820 sensor2 found!\r\n");
andrewklmn 18:9434e06223c7 59 };
andrewklmn 18:9434e06223c7 60
andrewklmn 18:9434e06223c7 61
hudakz 0:ab218237069e 62 while(1) {
hudakz 5:3c3ef17a17a6 63 // The on-board LED is connected, via a resistor, to +3.3V (not to GND).
hudakz 3:c6a589f444b9 64 // So to turn the LED on or off we have to set it to 0 or 1 respectively
hudakz 7:accb2c83a007 65 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 66 wait_ms(50); // 200 millisecond
andrewklmn 12:187e02ab447f 67 myled = 1; // turn the LED off
andrewklmn 12:187e02ab447f 68 wait_ms(50); // 1000 millisecond
andrewklmn 12:187e02ab447f 69 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 70 wait_ms(50); // 200 millisecond
andrewklmn 12:187e02ab447f 71 myled = 1; // turn the LED off
andrewklmn 12:187e02ab447f 72 wait_ms(50); // 1000 millisecond
andrewklmn 12:187e02ab447f 73 myled = 0; // turn the LED on
andrewklmn 12:187e02ab447f 74 wait_ms(50); // 200 millisecond
hudakz 7:accb2c83a007 75 myled = 1; // turn the LED off
andrewklmn 17:718e41e971f8 76
andrewklmn 17:718e41e971f8 77
andrewklmn 17:718e41e971f8 78
andrewklmn 17:718e41e971f8 79 ds1820.startConversion(); // start temperature conversion from analog to digital
andrewklmn 18:9434e06223c7 80 ds1820_2.startConversion(); // start temperature conversion from analog to digital
andrewklmn 18:9434e06223c7 81
andrewklmn 18:9434e06223c7 82 wait(0.750); // let DS1820 complete the temperature conversion
andrewklmn 17:718e41e971f8 83 error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 17:718e41e971f8 84 switch(error) {
andrewklmn 17:718e41e971f8 85 case 0: // no errors -> 'temp' contains the value of measured temperature
andrewklmn 18:9434e06223c7 86 pc.printf("sensor1 temp = %3.1fC\r\n", temp, 176);
andrewklmn 17:718e41e971f8 87 break;
andrewklmn 17:718e41e971f8 88 case 1: // no sensor present -> 'temp' is not updated
andrewklmn 18:9434e06223c7 89 pc.printf("no sensor1 present\n\r");
andrewklmn 17:718e41e971f8 90 break;
andrewklmn 17:718e41e971f8 91 case 2: // CRC error -> 'temp' is not updated
andrewklmn 18:9434e06223c7 92 pc.printf("sensor1 CRC error\r\n");
andrewklmn 18:9434e06223c7 93 };
andrewklmn 18:9434e06223c7 94
andrewklmn 18:9434e06223c7 95 error = ds1820_2.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
andrewklmn 18:9434e06223c7 96 switch(error) {
andrewklmn 18:9434e06223c7 97 case 0: // no errors -> 'temp' contains the value of measured temperature
andrewklmn 18:9434e06223c7 98 pc.printf("sensor2 temp = %3.1fC\r\n", temp, 176);
andrewklmn 18:9434e06223c7 99 break;
andrewklmn 18:9434e06223c7 100 case 1: // no sensor present -> 'temp' is not updated
andrewklmn 18:9434e06223c7 101 pc.printf("no sensor2 present\n\r");
andrewklmn 18:9434e06223c7 102 break;
andrewklmn 18:9434e06223c7 103 case 2: // CRC error -> 'temp' is not updated
andrewklmn 18:9434e06223c7 104 pc.printf("sensor2 CRC error\r\n");
andrewklmn 17:718e41e971f8 105 }
andrewklmn 18:9434e06223c7 106
andrewklmn 17:718e41e971f8 107
andrewklmn 17:718e41e971f8 108
andrewklmn 17:718e41e971f8 109
andrewklmn 12:187e02ab447f 110 if (recieved == 1) {
andrewklmn 14:d954558361b9 111
andrewklmn 12:187e02ab447f 112 // command was recieved
andrewklmn 12:187e02ab447f 113 pc.printf(a);
andrewklmn 12:187e02ab447f 114
andrewklmn 12:187e02ab447f 115 // ready for new command
andrewklmn 14:d954558361b9 116 recieved = 0;
andrewklmn 12:187e02ab447f 117 a[0] = '\0';
andrewklmn 12:187e02ab447f 118 i = 0;
andrewklmn 14:d954558361b9 119 };
andrewklmn 14:d954558361b9 120
hudakz 0:ab218237069e 121 }
hudakz 0:ab218237069e 122 }
hudakz 8:f1432e9af6c8 123