Temperaturna komora sa dodatom regulacijom.

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
pavlem
Date:
Wed Jun 29 15:21:52 2022 +0000
Revision:
2:6c5d798a1092
Parent:
1:aa2a0b18bdb1
Dodata regulacija;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tzwell 0:d859694d093c 1 /*
tzwell 1:aa2a0b18bdb1 2 * Enviromental Chamber for PCB testing
tzwell 1:aa2a0b18bdb1 3 *
tzwell 1:aa2a0b18bdb1 4 * The Department of Electronics and Digital Systems
tzwell 1:aa2a0b18bdb1 5 * School of Electrical Engineering, University of Belgrade
tzwell 0:d859694d093c 6 *
tzwell 1:aa2a0b18bdb1 7 * May 2022.
tzwell 0:d859694d093c 8 *
tzwell 0:d859694d093c 9 */
tzwell 0:d859694d093c 10
tzwell 0:d859694d093c 11 #include "mbed.h"
tzwell 1:aa2a0b18bdb1 12 #include "Adafruit_GFX.h"
tzwell 1:aa2a0b18bdb1 13 #include "Adafruit_GFX_Config.h"
tzwell 1:aa2a0b18bdb1 14 #include "Adafruit_SSD1306.h"
tzwell 1:aa2a0b18bdb1 15 #include "DS1820.h"
tzwell 1:aa2a0b18bdb1 16 #include <ctype.h>
tzwell 1:aa2a0b18bdb1 17
tzwell 1:aa2a0b18bdb1 18
tzwell 1:aa2a0b18bdb1 19 // A pin used for temperature sensor
tzwell 1:aa2a0b18bdb1 20 #define DATA_PIN A0
tzwell 1:aa2a0b18bdb1 21 #define MAX_PROBES 16
tzwell 1:aa2a0b18bdb1 22
tzwell 1:aa2a0b18bdb1 23 // I2C address, 60d or 0x3c:
tzwell 1:aa2a0b18bdb1 24 #define I2C_REAL_ADD 0x3c
tzwell 1:aa2a0b18bdb1 25 #define I2C_ADDRESS I2C_REAL_ADD << 1
tzwell 0:d859694d093c 26
tzwell 1:aa2a0b18bdb1 27 // Set OLED width and heigth [pixel]:
tzwell 1:aa2a0b18bdb1 28 #define OLED_WIDTH_PX 128
tzwell 1:aa2a0b18bdb1 29 #define OLED_HEIGHT_PX 64
tzwell 1:aa2a0b18bdb1 30
tzwell 1:aa2a0b18bdb1 31 // Multipliers of POT1 and POT2 for OLED rectangle position:
tzwell 1:aa2a0b18bdb1 32 #define WIDTH_SCALER 128
tzwell 1:aa2a0b18bdb1 33 #define HEIGHT_SCALER
tzwell 1:aa2a0b18bdb1 34
tzwell 1:aa2a0b18bdb1 35 // Min and max temp:
tzwell 1:aa2a0b18bdb1 36 #define TEMP_MIN 0
tzwell 1:aa2a0b18bdb1 37 #define TEMP_MAX 70
tzwell 1:aa2a0b18bdb1 38
tzwell 1:aa2a0b18bdb1 39 // UART2 defines
tzwell 1:aa2a0b18bdb1 40 #define COMMAND_LENGTH 3
tzwell 1:aa2a0b18bdb1 41
tzwell 1:aa2a0b18bdb1 42 // Time delay for acquisition
tzwell 1:aa2a0b18bdb1 43 #define TEMP_DELAY 1.0f
tzwell 1:aa2a0b18bdb1 44
tzwell 1:aa2a0b18bdb1 45 // Time delay for debounce [ms]
tzwell 1:aa2a0b18bdb1 46 #define DEBOUNCE_PER 30
tzwell 0:d859694d093c 47
tzwell 0:d859694d093c 48 /*
tzwell 1:aa2a0b18bdb1 49 * Globar variables
tzwell 0:d859694d093c 50 */
tzwell 0:d859694d093c 51
tzwell 1:aa2a0b18bdb1 52 // Timer for temperature acquisition
tzwell 1:aa2a0b18bdb1 53 Ticker temp_timer;
tzwell 0:d859694d093c 54
tzwell 1:aa2a0b18bdb1 55 // Buttons for relay control
tzwell 1:aa2a0b18bdb1 56 InterruptIn button_sw1 (PC_9);
tzwell 1:aa2a0b18bdb1 57 InterruptIn button_sw2 (PC_8);
tzwell 1:aa2a0b18bdb1 58
tzwell 1:aa2a0b18bdb1 59 // Relay selection signals
tzwell 1:aa2a0b18bdb1 60 DigitalOut relay_select1(PA_10); // D2
tzwell 1:aa2a0b18bdb1 61 DigitalOut relay_select2(PB_3); // D3
pavlem 2:6c5d798a1092 62 DigitalOut fan_on(PB_5); // D3
tzwell 1:aa2a0b18bdb1 63
tzwell 1:aa2a0b18bdb1 64 // DS1820 sensor object
tzwell 1:aa2a0b18bdb1 65 DS1820* probe[MAX_PROBES];
tzwell 1:aa2a0b18bdb1 66
tzwell 1:aa2a0b18bdb1 67 // Temperature variables
tzwell 1:aa2a0b18bdb1 68 float temp;
tzwell 1:aa2a0b18bdb1 69 int end_temp;
tzwell 1:aa2a0b18bdb1 70
tzwell 1:aa2a0b18bdb1 71 // DS1820 status and acquisition confirmation flag
tzwell 1:aa2a0b18bdb1 72 char data_acquired = 0;
tzwell 1:aa2a0b18bdb1 73
tzwell 1:aa2a0b18bdb1 74 // UART2 flags and variables
tzwell 1:aa2a0b18bdb1 75 char command_received = 0;
tzwell 1:aa2a0b18bdb1 76 char command_buffer[COMMAND_LENGTH] = {0};
tzwell 1:aa2a0b18bdb1 77 char incorrect_command = 0;
tzwell 1:aa2a0b18bdb1 78 char intro_printed = 0;
tzwell 1:aa2a0b18bdb1 79
tzwell 1:aa2a0b18bdb1 80 // Initialize UART for CLI
tzwell 1:aa2a0b18bdb1 81 Serial uart2(PA_2, PA_3);
tzwell 1:aa2a0b18bdb1 82
tzwell 1:aa2a0b18bdb1 83 // Initialize I2C for OLED display
tzwell 1:aa2a0b18bdb1 84 I2C i2c(PB_14,PB_13);
tzwell 1:aa2a0b18bdb1 85
tzwell 1:aa2a0b18bdb1 86 // Initialize OLED display:
tzwell 1:aa2a0b18bdb1 87 Adafruit_SSD1306_I2c myOled(i2c,PB_5,I2C_ADDRESS,OLED_HEIGHT_PX,OLED_WIDTH_PX);
tzwell 1:aa2a0b18bdb1 88
tzwell 1:aa2a0b18bdb1 89 // Declarations for interrupt routines
tzwell 1:aa2a0b18bdb1 90 void ISR_button_sw1 (void);
tzwell 1:aa2a0b18bdb1 91 void ISR_button_sw2 (void);
tzwell 1:aa2a0b18bdb1 92 void ISR_temp(void);
tzwell 1:aa2a0b18bdb1 93 void ISR_Rx(void);
tzwell 1:aa2a0b18bdb1 94
tzwell 1:aa2a0b18bdb1 95 // Declaration of display refresh function
tzwell 1:aa2a0b18bdb1 96 void refresh_display (void);
tzwell 1:aa2a0b18bdb1 97
tzwell 1:aa2a0b18bdb1 98 // Declaration of message parsing function
tzwell 1:aa2a0b18bdb1 99 void parse_message (void);
tzwell 1:aa2a0b18bdb1 100
tzwell 0:d859694d093c 101 int main()
tzwell 0:d859694d093c 102 {
tzwell 1:aa2a0b18bdb1 103 char cmd = 0;
tzwell 1:aa2a0b18bdb1 104 int ref_temp = 0;
pavlem 2:6c5d798a1092 105 char regulation_on = 0;
tzwell 0:d859694d093c 106
tzwell 1:aa2a0b18bdb1 107 // Initialize OLED:
tzwell 1:aa2a0b18bdb1 108 myOled.begin();
tzwell 1:aa2a0b18bdb1 109
tzwell 1:aa2a0b18bdb1 110 // Set ISRs for buttons
tzwell 1:aa2a0b18bdb1 111 button_sw1.fall(&ISR_button_sw1);
tzwell 1:aa2a0b18bdb1 112 button_sw2.fall(&ISR_button_sw2);
tzwell 0:d859694d093c 113
tzwell 1:aa2a0b18bdb1 114 // Initialize probes
tzwell 1:aa2a0b18bdb1 115 int num_devices = 0;
tzwell 1:aa2a0b18bdb1 116 while(DS1820::unassignedProbe(DATA_PIN)) {
tzwell 1:aa2a0b18bdb1 117 probe[num_devices] = new DS1820(DATA_PIN);
tzwell 1:aa2a0b18bdb1 118 num_devices++;
tzwell 1:aa2a0b18bdb1 119 if (num_devices == MAX_PROBES)
tzwell 1:aa2a0b18bdb1 120 break;
tzwell 1:aa2a0b18bdb1 121 }
tzwell 1:aa2a0b18bdb1 122
tzwell 1:aa2a0b18bdb1 123 printf("Found %d device(s)\r\n\n", num_devices);
tzwell 1:aa2a0b18bdb1 124
tzwell 1:aa2a0b18bdb1 125 printf("Dobrodosli u Pajinu i Cveletovu komoru!\n\r");
tzwell 1:aa2a0b18bdb1 126 printf("Spremite se jer polecemo!!1!\n\r");
tzwell 1:aa2a0b18bdb1 127
tzwell 0:d859694d093c 128 while(true)
tzwell 0:d859694d093c 129 {
tzwell 1:aa2a0b18bdb1 130 if (!intro_printed)
tzwell 1:aa2a0b18bdb1 131 {
tzwell 1:aa2a0b18bdb1 132 printf("Molimo, unesite 's' za start: \n\r");
tzwell 1:aa2a0b18bdb1 133 scanf("%c", &cmd);
tzwell 1:aa2a0b18bdb1 134 intro_printed = 1;
tzwell 1:aa2a0b18bdb1 135 }
tzwell 1:aa2a0b18bdb1 136 if (cmd == 's')
tzwell 1:aa2a0b18bdb1 137 {
tzwell 1:aa2a0b18bdb1 138 printf("Molimo, unesite zeljenu temperaturu: \n\r");
tzwell 1:aa2a0b18bdb1 139 scanf("%d", &ref_temp);
tzwell 1:aa2a0b18bdb1 140 if (ref_temp > TEMP_MIN && ref_temp < TEMP_MAX)
tzwell 1:aa2a0b18bdb1 141 {
pavlem 2:6c5d798a1092 142 temp_timer.attach(&ISR_temp, TEMP_DELAY);
tzwell 1:aa2a0b18bdb1 143 }
tzwell 1:aa2a0b18bdb1 144 cmd = 0;
tzwell 1:aa2a0b18bdb1 145 }
tzwell 1:aa2a0b18bdb1 146
tzwell 1:aa2a0b18bdb1 147 if (data_acquired)
tzwell 1:aa2a0b18bdb1 148 {
tzwell 1:aa2a0b18bdb1 149 refresh_display();
pavlem 2:6c5d798a1092 150
pavlem 2:6c5d798a1092 151 if (abs(temp-ref_temp)>0.1)
pavlem 2:6c5d798a1092 152 {
pavlem 2:6c5d798a1092 153 fan_on = 1;
pavlem 2:6c5d798a1092 154 if (temp > ref_temp)
pavlem 2:6c5d798a1092 155 {
pavlem 2:6c5d798a1092 156 // Turn on the cooling
pavlem 2:6c5d798a1092 157 relay_select1 = 0;
pavlem 2:6c5d798a1092 158 relay_select2 = 1;
pavlem 2:6c5d798a1092 159 }
pavlem 2:6c5d798a1092 160 else
pavlem 2:6c5d798a1092 161 {
pavlem 2:6c5d798a1092 162 // Turn on the heating
pavlem 2:6c5d798a1092 163 relay_select1 = 1;
pavlem 2:6c5d798a1092 164 relay_select2 = 0;
pavlem 2:6c5d798a1092 165 }
pavlem 2:6c5d798a1092 166 }
pavlem 2:6c5d798a1092 167
tzwell 1:aa2a0b18bdb1 168 data_acquired = 0;
tzwell 1:aa2a0b18bdb1 169 }
pavlem 2:6c5d798a1092 170
tzwell 0:d859694d093c 171 }
tzwell 0:d859694d093c 172 }
tzwell 0:d859694d093c 173
tzwell 1:aa2a0b18bdb1 174 // Display refresh function definition
tzwell 1:aa2a0b18bdb1 175 void refresh_display (void)
tzwell 1:aa2a0b18bdb1 176 {
tzwell 1:aa2a0b18bdb1 177 myOled.clearDisplay();
tzwell 1:aa2a0b18bdb1 178 // myOled.printf("It is %3.1foC\r\n", probe.temperature());
tzwell 1:aa2a0b18bdb1 179 myOled.display();
tzwell 1:aa2a0b18bdb1 180 // printf("It is %3.1foC\r\n", probe.temperature());
tzwell 1:aa2a0b18bdb1 181 }
tzwell 0:d859694d093c 182
tzwell 1:aa2a0b18bdb1 183 // Definition of message parsing function
tzwell 1:aa2a0b18bdb1 184 void parse_message (void)
tzwell 1:aa2a0b18bdb1 185 {
tzwell 1:aa2a0b18bdb1 186 if (command_buffer[0] == 's')
tzwell 1:aa2a0b18bdb1 187 {
tzwell 1:aa2a0b18bdb1 188 if (isdigit(command_buffer[1]) && isdigit(command_buffer[2]))
tzwell 1:aa2a0b18bdb1 189 {
tzwell 1:aa2a0b18bdb1 190 end_temp = (command_buffer[1] - 48) * 10 + (command_buffer[2] - 48);
tzwell 1:aa2a0b18bdb1 191 }
tzwell 1:aa2a0b18bdb1 192 else
tzwell 1:aa2a0b18bdb1 193 {
tzwell 1:aa2a0b18bdb1 194 incorrect_command = 1;
tzwell 1:aa2a0b18bdb1 195 }
tzwell 1:aa2a0b18bdb1 196 }
tzwell 1:aa2a0b18bdb1 197 else if (command_buffer[0] == 'e')
tzwell 1:aa2a0b18bdb1 198 {
tzwell 1:aa2a0b18bdb1 199
tzwell 1:aa2a0b18bdb1 200 }
tzwell 1:aa2a0b18bdb1 201 else
tzwell 1:aa2a0b18bdb1 202 {
tzwell 1:aa2a0b18bdb1 203 incorrect_command = 1;
tzwell 1:aa2a0b18bdb1 204 }
tzwell 1:aa2a0b18bdb1 205 }
tzwell 1:aa2a0b18bdb1 206
tzwell 1:aa2a0b18bdb1 207 // ISR for temperature acquisition
tzwell 1:aa2a0b18bdb1 208 void ISR_temp (void)
tzwell 1:aa2a0b18bdb1 209 {
tzwell 1:aa2a0b18bdb1 210 probe[0]->convertTemperature(true, DS1820::all_devices);
pavlem 2:6c5d798a1092 211 temp = probe[0]->temperature();
pavlem 2:6c5d798a1092 212 printf("It is %3.1foC\r\n", temp);
tzwell 1:aa2a0b18bdb1 213 data_acquired = 1;
tzwell 1:aa2a0b18bdb1 214 }
tzwell 1:aa2a0b18bdb1 215
tzwell 1:aa2a0b18bdb1 216 // ISR for Relay 1 control
tzwell 1:aa2a0b18bdb1 217 void ISR_button_sw1 (void)
tzwell 1:aa2a0b18bdb1 218 {
tzwell 1:aa2a0b18bdb1 219 wait_ms(DEBOUNCE_PER);
tzwell 1:aa2a0b18bdb1 220
tzwell 1:aa2a0b18bdb1 221 if (!button_sw1)
tzwell 1:aa2a0b18bdb1 222 {
tzwell 1:aa2a0b18bdb1 223 relay_select1 = !relay_select1;
tzwell 1:aa2a0b18bdb1 224 }
tzwell 1:aa2a0b18bdb1 225 }
tzwell 1:aa2a0b18bdb1 226
tzwell 1:aa2a0b18bdb1 227 // ISR for Relay 2 control
tzwell 1:aa2a0b18bdb1 228 void ISR_button_sw2 (void)
tzwell 1:aa2a0b18bdb1 229 {
tzwell 1:aa2a0b18bdb1 230 wait_ms(DEBOUNCE_PER);
tzwell 1:aa2a0b18bdb1 231
tzwell 1:aa2a0b18bdb1 232 if (!button_sw2)
tzwell 1:aa2a0b18bdb1 233 {
tzwell 1:aa2a0b18bdb1 234 relay_select2 = !relay_select2;
tzwell 1:aa2a0b18bdb1 235 }
tzwell 1:aa2a0b18bdb1 236 }