-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
306 lines (232 loc) · 8.36 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Keil project for DS18B20 library example
*
ces on 1wire bus
* Start conversion on all devices
* Read temperatures and display it
* Checks for devices with alarm flag set
*
* Before you start, select your target, on the right of the "Load" button
*
* @author Tilen Majerle
* @email [email protected]
* @website http://stm32f4-discovery.com
* @ide Keil uVision 5
* @packs STM32F4xx Keil packs version 2.2.0 or greater required
* @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required
*/
/* Include core modules */
#include "stm32f4xx.h"
/* Include my libraries here */
#include "defines.h"
#include "tm_stm32f4_delay.h"
#include "tm_stm32f4_onewire.h"
#include "tm_stm32f4_usart.h"
#include "tm_stm32f4_ds18b20.h"
#include "tm_stm32f4_disco.h"
#include <stdio.h>
#include "door.h"
#include "interupt.h"
#include "adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_exti.h"
#include "misc.h"
#include "stm32f4xx_usart.h"
#include "tm_stm32f4_i2c.h"
/* How many sensors we are expecting on 1wire bus? */
#define EXPECTING_SENSORS 1
uint32_t volatile light_offset, temp_offset, emty_bar_offset, water_level_offset, humidity_offset;
uint32_t volatile ConvertedValueOffset = 0;
uint32_t volatile day_time = 1;
uint32_t volatile night_time = 1;
uint32_t volatile time_mode = 1;
uint32_t volatile heating_on, heating_off, heating_mode, heating_offset, Value_Offset;
//uint16_t volatile ADC3ConvertedValue[5] = {0,0,0,0,0};
void EXTI0_IRQHandler();
void EXTI15_10_IRQHandler();
int main(void) {
char buf[40];
uint16_t light_value, tempvalue, devices, i, j, count, alarm_count;
uint8_t device[EXPECTING_SENSORS][8], alarm_device[EXPECTING_SENSORS][8];
float temps[EXPECTING_SENSORS];
/* OneWire working struct */
TM_OneWire_t OneWire1;
/* Initialize system */
SystemInit();
/* Initialize delay */
TM_DELAY_Init();
/* Initialize OneWire on pin PD0 */
TM_OneWire_Init(&OneWire1, GPIOC, GPIO_Pin_7);
/* Initialize USART, TX: PB6, RX: PB7 */
TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200);
/* Initialize I2C, SCL: PB6 and SDA: PB7 with 100kHt serial clock */
TM_I2C_Init(I2C1, TM_I2C_PinsPack_1, 100000);
/* Turn leds on */
TM_DISCO_LedOn(LED_ALL);
/* Checks for any device on 1-wire */
count = 0;
devices = TM_OneWire_First(&OneWire1);
while (devices) {
/* Increase counter */
count++;
/* Get full ROM value, 8 bytes, give location of first byte where to save */
TM_OneWire_GetFullROM(&OneWire1, device[count - 1]);
/* Get next device */
devices = TM_OneWire_Next(&OneWire1);
}
/* If any devices on 1wire */
if (count > 0) {
sprintf(buf, "device on 1-wire: %d\n", count);
TM_DISCO_LedOn(LED_GREEN);
Delayms(1000);
TM_DISCO_LedOff(LED_GREEN);
TM_USART_Puts(USART1, buf);
/* Display 64bit rom code for each device */
for (j = 0; j < count; j++) {
for (i = 0; i < 8; i++) {
sprintf(buf, "0x%02X ", device[j][i]);
TM_USART_Puts(USART3, buf);
}
TM_USART_Puts(USART3, "\n");
}
} else {
TM_USART_Puts(USART3, "No devices on OneWire.\n\r");
TM_DISCO_LedOff(LED_RED);
TM_DISCO_LedOff(LED_BLUE);
}
/* Go through all connected devices and set resolution to 12bits */
for (i = 0; i < count; i++) {
/* Set resolution to 12bits */
TM_DS18B20_SetResolution(&OneWire1, device[i], TM_DS18B20_Resolution_12bits);
}
/* Set high temperature alarm on device number 0, 25 degrees celcius */
TM_DS18B20_SetAlarmHighTemperature(&OneWire1, device[0], 25);
/* Disable alarm temperatures on device number 1 */
TM_DS18B20_DisableAlarmTemperature(&OneWire1, device[1]);
adc_configure();
//Doors_init();
Doors_Init(FrontDoor);
Doors_Init(OutsideDoor);
/* System init */
//SystemInit();
Configure_PA0();
Configure_PB12();
while (1) {
/* Start temperature conversion on all devices on one bus */
TM_DS18B20_StartAll(&OneWire1);
/* Wait until all are done on one onewire port */
while (!TM_DS18B20_AllDone(&OneWire1));
/* Read temperature from each device separatelly */
for (i = 0; i < count; i++) {
/* Read temperature from ROM address and store it to temps variable */
if (TM_DS18B20_Read(&OneWire1, device[i], &temps[i])) {
tempvalue = *temps;
sprintf(buf,"Temp: %d temperatūra = %u\n\r",i,tempvalue);
/* Print temperature */
TM_USART_Puts(USART3, buf);
} else {
/* Reading error */
TM_USART_Puts(USART3, "Reading error;\n");
}
}
/* Reset alarm count */
alarm_count = 0;
/* Check if any device has alarm flag set */
while (TM_DS18B20_AlarmSearch(&OneWire1)) {
/* Store ROM of device which has alarm flag set */
TM_OneWire_GetFullROM(&OneWire1, alarm_device[alarm_count]);
/* Increase count */
alarm_count++;
}
/* Format string and send over USART for debug */
sprintf(buf, "Devices with alarm: %d\n\r", alarm_count);
TM_USART_Puts(USART3, buf);
/* Any device has alarm flag set? */
if (alarm_count > 0) {
/* Show rom of this devices */
for (j = 0; j < alarm_count; j++) {
TM_USART_Puts(USART3, "Device with alarm: ");
for (i = 0; i < 8; i++) {
sprintf(buf, "0x%02X ", alarm_device[j][i]);
TM_USART_Puts(USART3, buf);
}
TM_USART_Puts(USART3, "\n\r ");
}
TM_USART_Puts(USART3, "ALARM devices recognized!\n\r");
}
/* Print separator */
TM_USART_Puts(USART3, "----------\n\r");
/* Some delay */
Delayms(1000);
uint32_t volatile *day_time_pointer;
day_time_pointer = &day_time;
uint32_t volatile *night_time_pointer;
night_time_pointer = &night_time;
uint32_t volatile *time_mode_pointer;
time_mode_pointer = &time_mode;
uint32_t volatile *Value_Offset_pointer;
Value_Offset_pointer = &Value_Offset;
temp_offset = ADC3ConvertedValue[0];
light_offset = ADC3ConvertedValue[1];
light_value = ADC3ConvertedValue[2];
emty_bar_offset = ADC3ConvertedValue[3];
water_level_offset = ADC3ConvertedValue[4];
ConvertedValueOffset = light_value + *Value_Offset_pointer;
if (ConvertedValueOffset > light_offset) {
*time_mode_pointer = 1;
if (((*time_mode_pointer) == 1) && ((*day_time_pointer) == 1) ) {
Doors_Open(FrontDoor);
TM_USART_Puts(USART3, "It is a day!\n\r");
TM_USART_Puts(USART3, "Doors start opening\n\r");
*day_time_pointer = 0;
*night_time_pointer = 1;
*Value_Offset_pointer = 100;
}
} else if (ConvertedValueOffset < light_offset) {
*time_mode_pointer = 0;
if ((*time_mode_pointer == 0) && (*night_time_pointer == 1) ) {
TM_USART_Puts(USART3, "It is a night!\n\r");
TM_USART_Puts(USART3, "Doors start closing\n\r");
Doors_Open(OutsideDoor);
*time_mode_pointer = 1;
*day_time_pointer = 1;
*night_time_pointer = 0;
*Value_Offset_pointer = -100;
}
}
uint32_t volatile *heating_on_pointer;
heating_on_pointer = &heating_on;
uint32_t volatile *heating_off_pointer;
heating_off_pointer = &heating_off;
uint32_t volatile *heating_mode_pointer;
heating_mode_pointer = &heating_mode;
uint32_t volatile *heating_offset_pointer;
heating_offset_pointer = &heating_offset;
uint32_t volatile temp_converted_value_offset = 0;
temp_converted_value_offset = tempvalue + *heating_offset_pointer;
if (tempvalue > temp_offset) {
*heating_mode_pointer = 1;
if (((*heating_mode_pointer) == 1) && ((*heating_on_pointer) == 1) ) {
Doors_Open(FrontDoor);
TM_USART_Puts(USART3, "water is cold!\n\r");
TM_USART_Puts(USART3, "start heating!\n\r");
*heating_on_pointer = 0;
*heating_off_pointer = 1;
*heating_offset_pointer = 3;
}
} else if (temp_converted_value_offset < temp_offset) {
*heating_mode_pointer = 0;
if ((*heating_mode_pointer == 0) && (*heating_off_pointer == 1) ) {
Doors_Open(OutsideDoor);
TM_USART_Puts(USART3, "water is warm!\n\r");
TM_USART_Puts(USART3, "stop heating!\n\r");
*heating_mode_pointer = 1;
*heating_on_pointer = 1;
*heating_off_pointer = 0;
*heating_offset_pointer = -3;
}
}
}
}