Temperaturna komora sa dodatom regulacijom.
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@1:aa2a0b18bdb1, 2022-06-29 (annotated)
- Committer:
- tzwell
- Date:
- Wed Jun 29 12:35:37 2022 +0000
- Revision:
- 1:aa2a0b18bdb1
- Parent:
- 0:d859694d093c
- Child:
- 2:6c5d798a1092
Prvi commit za klima komoru bez regulacije
Who changed what in which revision?
User | Revision | Line number | New 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 "DHT.h" |
tzwell | 1:aa2a0b18bdb1 | 16 | #include "DS1820.h" |
tzwell | 1:aa2a0b18bdb1 | 17 | #include <ctype.h> |
tzwell | 1:aa2a0b18bdb1 | 18 | |
tzwell | 1:aa2a0b18bdb1 | 19 | |
tzwell | 1:aa2a0b18bdb1 | 20 | // A pin used for temperature sensor |
tzwell | 1:aa2a0b18bdb1 | 21 | #define DATA_PIN A0 |
tzwell | 1:aa2a0b18bdb1 | 22 | #define MAX_PROBES 16 |
tzwell | 1:aa2a0b18bdb1 | 23 | |
tzwell | 1:aa2a0b18bdb1 | 24 | // I2C address, 60d or 0x3c: |
tzwell | 1:aa2a0b18bdb1 | 25 | #define I2C_REAL_ADD 0x3c |
tzwell | 1:aa2a0b18bdb1 | 26 | #define I2C_ADDRESS I2C_REAL_ADD << 1 |
tzwell | 0:d859694d093c | 27 | |
tzwell | 1:aa2a0b18bdb1 | 28 | // Set OLED width and heigth [pixel]: |
tzwell | 1:aa2a0b18bdb1 | 29 | #define OLED_WIDTH_PX 128 |
tzwell | 1:aa2a0b18bdb1 | 30 | #define OLED_HEIGHT_PX 64 |
tzwell | 1:aa2a0b18bdb1 | 31 | |
tzwell | 1:aa2a0b18bdb1 | 32 | // Multipliers of POT1 and POT2 for OLED rectangle position: |
tzwell | 1:aa2a0b18bdb1 | 33 | #define WIDTH_SCALER 128 |
tzwell | 1:aa2a0b18bdb1 | 34 | #define HEIGHT_SCALER |
tzwell | 1:aa2a0b18bdb1 | 35 | |
tzwell | 1:aa2a0b18bdb1 | 36 | // Min and max temp: |
tzwell | 1:aa2a0b18bdb1 | 37 | #define TEMP_MIN 0 |
tzwell | 1:aa2a0b18bdb1 | 38 | #define TEMP_MAX 70 |
tzwell | 1:aa2a0b18bdb1 | 39 | |
tzwell | 1:aa2a0b18bdb1 | 40 | // UART2 defines |
tzwell | 1:aa2a0b18bdb1 | 41 | #define COMMAND_LENGTH 3 |
tzwell | 1:aa2a0b18bdb1 | 42 | |
tzwell | 1:aa2a0b18bdb1 | 43 | // Time delay for acquisition |
tzwell | 1:aa2a0b18bdb1 | 44 | #define TEMP_DELAY 1.0f |
tzwell | 1:aa2a0b18bdb1 | 45 | |
tzwell | 1:aa2a0b18bdb1 | 46 | // Time delay for debounce [ms] |
tzwell | 1:aa2a0b18bdb1 | 47 | #define DEBOUNCE_PER 30 |
tzwell | 0:d859694d093c | 48 | |
tzwell | 0:d859694d093c | 49 | /* |
tzwell | 1:aa2a0b18bdb1 | 50 | * Globar variables |
tzwell | 0:d859694d093c | 51 | */ |
tzwell | 0:d859694d093c | 52 | |
tzwell | 1:aa2a0b18bdb1 | 53 | // Timer for temperature acquisition |
tzwell | 1:aa2a0b18bdb1 | 54 | Ticker temp_timer; |
tzwell | 0:d859694d093c | 55 | |
tzwell | 1:aa2a0b18bdb1 | 56 | // Buttons for relay control |
tzwell | 1:aa2a0b18bdb1 | 57 | InterruptIn button_sw1 (PC_9); |
tzwell | 1:aa2a0b18bdb1 | 58 | InterruptIn button_sw2 (PC_8); |
tzwell | 1:aa2a0b18bdb1 | 59 | |
tzwell | 1:aa2a0b18bdb1 | 60 | // Relay selection signals |
tzwell | 1:aa2a0b18bdb1 | 61 | DigitalOut relay_select1(PA_10); // D2 |
tzwell | 1:aa2a0b18bdb1 | 62 | DigitalOut relay_select2(PB_3); // 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; |
tzwell | 0:d859694d093c | 105 | |
tzwell | 1:aa2a0b18bdb1 | 106 | // Initialize OLED: |
tzwell | 1:aa2a0b18bdb1 | 107 | myOled.begin(); |
tzwell | 1:aa2a0b18bdb1 | 108 | |
tzwell | 1:aa2a0b18bdb1 | 109 | // Set ISRs for buttons |
tzwell | 1:aa2a0b18bdb1 | 110 | button_sw1.fall(&ISR_button_sw1); |
tzwell | 1:aa2a0b18bdb1 | 111 | button_sw2.fall(&ISR_button_sw2); |
tzwell | 0:d859694d093c | 112 | |
tzwell | 1:aa2a0b18bdb1 | 113 | // Initialize probes |
tzwell | 1:aa2a0b18bdb1 | 114 | int num_devices = 0; |
tzwell | 1:aa2a0b18bdb1 | 115 | while(DS1820::unassignedProbe(DATA_PIN)) { |
tzwell | 1:aa2a0b18bdb1 | 116 | probe[num_devices] = new DS1820(DATA_PIN); |
tzwell | 1:aa2a0b18bdb1 | 117 | num_devices++; |
tzwell | 1:aa2a0b18bdb1 | 118 | if (num_devices == MAX_PROBES) |
tzwell | 1:aa2a0b18bdb1 | 119 | break; |
tzwell | 1:aa2a0b18bdb1 | 120 | } |
tzwell | 1:aa2a0b18bdb1 | 121 | |
tzwell | 1:aa2a0b18bdb1 | 122 | printf("Found %d device(s)\r\n\n", num_devices); |
tzwell | 1:aa2a0b18bdb1 | 123 | |
tzwell | 1:aa2a0b18bdb1 | 124 | printf("Dobrodosli u Pajinu i Cveletovu komoru!\n\r"); |
tzwell | 1:aa2a0b18bdb1 | 125 | printf("Spremite se jer polecemo!!1!\n\r"); |
tzwell | 1:aa2a0b18bdb1 | 126 | |
tzwell | 0:d859694d093c | 127 | while(true) |
tzwell | 0:d859694d093c | 128 | { |
tzwell | 1:aa2a0b18bdb1 | 129 | if (!intro_printed) |
tzwell | 1:aa2a0b18bdb1 | 130 | { |
tzwell | 1:aa2a0b18bdb1 | 131 | printf("Molimo, unesite 's' za start: \n\r"); |
tzwell | 1:aa2a0b18bdb1 | 132 | scanf("%c", &cmd); |
tzwell | 1:aa2a0b18bdb1 | 133 | intro_printed = 1; |
tzwell | 1:aa2a0b18bdb1 | 134 | } |
tzwell | 1:aa2a0b18bdb1 | 135 | if (cmd == 's') |
tzwell | 1:aa2a0b18bdb1 | 136 | { |
tzwell | 1:aa2a0b18bdb1 | 137 | printf("Molimo, unesite zeljenu temperaturu: \n\r"); |
tzwell | 1:aa2a0b18bdb1 | 138 | scanf("%d", &ref_temp); |
tzwell | 1:aa2a0b18bdb1 | 139 | if (ref_temp > TEMP_MIN && ref_temp < TEMP_MAX) |
tzwell | 1:aa2a0b18bdb1 | 140 | { |
tzwell | 1:aa2a0b18bdb1 | 141 | temp_timer.attach(&ISR_temp, TEMP_DELAY); |
tzwell | 1:aa2a0b18bdb1 | 142 | } |
tzwell | 1:aa2a0b18bdb1 | 143 | cmd = 0; |
tzwell | 1:aa2a0b18bdb1 | 144 | } |
tzwell | 1:aa2a0b18bdb1 | 145 | |
tzwell | 1:aa2a0b18bdb1 | 146 | if (data_acquired) |
tzwell | 1:aa2a0b18bdb1 | 147 | { |
tzwell | 1:aa2a0b18bdb1 | 148 | refresh_display(); |
tzwell | 1:aa2a0b18bdb1 | 149 | data_acquired = 0; |
tzwell | 1:aa2a0b18bdb1 | 150 | } |
tzwell | 0:d859694d093c | 151 | } |
tzwell | 0:d859694d093c | 152 | } |
tzwell | 0:d859694d093c | 153 | |
tzwell | 1:aa2a0b18bdb1 | 154 | // Display refresh function definition |
tzwell | 1:aa2a0b18bdb1 | 155 | void refresh_display (void) |
tzwell | 1:aa2a0b18bdb1 | 156 | { |
tzwell | 1:aa2a0b18bdb1 | 157 | myOled.clearDisplay(); |
tzwell | 1:aa2a0b18bdb1 | 158 | // myOled.printf("It is %3.1foC\r\n", probe.temperature()); |
tzwell | 1:aa2a0b18bdb1 | 159 | myOled.display(); |
tzwell | 1:aa2a0b18bdb1 | 160 | // printf("It is %3.1foC\r\n", probe.temperature()); |
tzwell | 1:aa2a0b18bdb1 | 161 | } |
tzwell | 0:d859694d093c | 162 | |
tzwell | 1:aa2a0b18bdb1 | 163 | // Definition of message parsing function |
tzwell | 1:aa2a0b18bdb1 | 164 | void parse_message (void) |
tzwell | 1:aa2a0b18bdb1 | 165 | { |
tzwell | 1:aa2a0b18bdb1 | 166 | if (command_buffer[0] == 's') |
tzwell | 1:aa2a0b18bdb1 | 167 | { |
tzwell | 1:aa2a0b18bdb1 | 168 | if (isdigit(command_buffer[1]) && isdigit(command_buffer[2])) |
tzwell | 1:aa2a0b18bdb1 | 169 | { |
tzwell | 1:aa2a0b18bdb1 | 170 | end_temp = (command_buffer[1] - 48) * 10 + (command_buffer[2] - 48); |
tzwell | 1:aa2a0b18bdb1 | 171 | } |
tzwell | 1:aa2a0b18bdb1 | 172 | else |
tzwell | 1:aa2a0b18bdb1 | 173 | { |
tzwell | 1:aa2a0b18bdb1 | 174 | incorrect_command = 1; |
tzwell | 1:aa2a0b18bdb1 | 175 | } |
tzwell | 1:aa2a0b18bdb1 | 176 | } |
tzwell | 1:aa2a0b18bdb1 | 177 | else if (command_buffer[0] == 'e') |
tzwell | 1:aa2a0b18bdb1 | 178 | { |
tzwell | 1:aa2a0b18bdb1 | 179 | |
tzwell | 1:aa2a0b18bdb1 | 180 | } |
tzwell | 1:aa2a0b18bdb1 | 181 | else |
tzwell | 1:aa2a0b18bdb1 | 182 | { |
tzwell | 1:aa2a0b18bdb1 | 183 | incorrect_command = 1; |
tzwell | 1:aa2a0b18bdb1 | 184 | } |
tzwell | 1:aa2a0b18bdb1 | 185 | } |
tzwell | 1:aa2a0b18bdb1 | 186 | |
tzwell | 1:aa2a0b18bdb1 | 187 | // ISR for temperature acquisition |
tzwell | 1:aa2a0b18bdb1 | 188 | void ISR_temp (void) |
tzwell | 1:aa2a0b18bdb1 | 189 | { |
tzwell | 1:aa2a0b18bdb1 | 190 | printf("t\n"); |
tzwell | 1:aa2a0b18bdb1 | 191 | probe[0]->convertTemperature(true, DS1820::all_devices); |
tzwell | 1:aa2a0b18bdb1 | 192 | printf("It is %3.1foC\r\n", probe[0]->temperature()); |
tzwell | 1:aa2a0b18bdb1 | 193 | data_acquired = 1; |
tzwell | 1:aa2a0b18bdb1 | 194 | } |
tzwell | 1:aa2a0b18bdb1 | 195 | |
tzwell | 1:aa2a0b18bdb1 | 196 | // ISR for Relay 1 control |
tzwell | 1:aa2a0b18bdb1 | 197 | void ISR_button_sw1 (void) |
tzwell | 1:aa2a0b18bdb1 | 198 | { |
tzwell | 1:aa2a0b18bdb1 | 199 | wait_ms(DEBOUNCE_PER); |
tzwell | 1:aa2a0b18bdb1 | 200 | |
tzwell | 1:aa2a0b18bdb1 | 201 | if (!button_sw1) |
tzwell | 1:aa2a0b18bdb1 | 202 | { |
tzwell | 1:aa2a0b18bdb1 | 203 | relay_select1 = !relay_select1; |
tzwell | 1:aa2a0b18bdb1 | 204 | } |
tzwell | 1:aa2a0b18bdb1 | 205 | } |
tzwell | 1:aa2a0b18bdb1 | 206 | |
tzwell | 1:aa2a0b18bdb1 | 207 | // ISR for Relay 2 control |
tzwell | 1:aa2a0b18bdb1 | 208 | void ISR_button_sw2 (void) |
tzwell | 1:aa2a0b18bdb1 | 209 | { |
tzwell | 1:aa2a0b18bdb1 | 210 | wait_ms(DEBOUNCE_PER); |
tzwell | 1:aa2a0b18bdb1 | 211 | |
tzwell | 1:aa2a0b18bdb1 | 212 | if (!button_sw2) |
tzwell | 1:aa2a0b18bdb1 | 213 | { |
tzwell | 1:aa2a0b18bdb1 | 214 | relay_select2 = !relay_select2; |
tzwell | 1:aa2a0b18bdb1 | 215 | } |
tzwell | 1:aa2a0b18bdb1 | 216 | } |