-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.c
324 lines (289 loc) · 9.44 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// main.c
// cxg-60ewt
//
// Created by Leonid Mesentsev on 26/11/2019.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <stm8s.h>
#include <stm8s_pins.h>
#include <main.h>
#include <delay.h>
#include <pwm.h>
#include <s7c.h>
#include <adc.h>
#include <eeprom.h>
#include <clock.h>
#include <menu.h>
#include <buttons.h>
#ifndef F_CPU
#warning "F_CPU not defined, using 16MHz by default"
#define F_CPU 16000000UL
#endif
enum WorkingModes
{
NORMAL_MODE,
FORCED_MODE,
SLEEP_MODE,
DEEPSLEEP_MODE
};
#define MIN_HEAT 50
#define MAX_HEAT 450
#define MAX_ADC_RT 130
#define MIN_ADC_RT 35
#define PWM_POWER_OFF 100
#define SLEEP_TEMP 100
#define EEPROM_SAVE_TIMEOUT 2000
#define HEATPOINT_DISPLAY_DELAY 2500
uint32_t _haveToSaveData = 0;
static uint32_t _sleepTimer = 0;
static uint32_t _heatPointDisplayTime = 0;
static uint8_t _currentState = NORMAL_MODE;
struct EEPROM_DATA _eepromData;
struct Button _btnPlus = {PB7, 0, 0, 0, 0, 0};
struct Button _btnMinus = {PB6, 0, 0, 0, 0, 0};
void deepSleep();
uint8_t checkSleep(uint32_t nowTime);
void checkHeatPointValidity();
void setup()
{
// Configure the clock for maximum speed on the 16MHz HSI oscillator
// At startup the clock output is divided by 8
CLK_CKDIVR = 0x0;
disable_interrupts();
TIM4_init();
enable_interrupts();
// Configure mercury sensor and button pins
pinMode(PB5, INPUT);
pinMode(PB7, INPUT);
pinMode(PB7, INPUT);
// Configure 7-segments display
S7C_init();
// Configure PWM
pinMode(PD4, OUTPUT);
PWM_init(PWM_CH1);
PWM_duty(PWM_CH1, 100); // set heater OFF
_sleepTimer = currentMillis();
_heatPointDisplayTime = _sleepTimer + HEATPOINT_DISPLAY_DELAY;
// EEPROM
eeprom_read(EEPROM_START_ADDR, &_eepromData, sizeof(_eepromData));
// First launch, eeprom empty OR -button pressed when power the device
if (_eepromData.heatPoint == 0 || getPin(PB6) == LOW)
{
_eepromData.heatPoint = 270;
_eepromData.enableSound = 1;
_eepromData.calibrationValue = 0;
_eepromData.sleepTimeout = 3; // 3 min, heatPoint 100C
_eepromData.deepSleepTimeout = 10; // 10 min, heatPoint 0
_eepromData.forceModeIncrement = 0; // 0 degrees
eeprom_write(EEPROM_START_ADDR, &_eepromData, sizeof(_eepromData));
}
beepAlarm();
// Press +button when power the device will enter to Setup Menu
if (getPin(PB7) == LOW)
{
setup_menu();
}
// Now we can switch ON the heater at 50%
PWM_duty(PWM_CH1, 50);
}
void mainLoop()
{
static uint16_t localCnt = 0;
uint8_t displaySymbol = 0;
uint32_t nowTime = currentMillis();
// Input power sensor
static uint16_t oldADCUI = 0;
uint16_t adcUIn = ADC_read(ADC1_CSR_CH1);
adcUIn = ((oldADCUI * 7) + adcUIn) >> 3; // noise filter
oldADCUI = adcUIn;
// Temperature sensor
static uint16_t oldADCVal = MIN_ADC_RT;
uint16_t adcVal = ADC_read(ADC1_CSR_CH0);
adcVal = ((oldADCVal * 7) + adcVal) >> 3; // noise filter
oldADCVal = adcVal;
// Degrees value
adcVal = (adcVal < MIN_ADC_RT) ? MIN_ADC_RT : adcVal;
int16_t currentDegrees = (MAX_HEAT - MIN_HEAT) * (adcVal - MIN_ADC_RT) / (MAX_ADC_RT - MIN_ADC_RT);
currentDegrees += _eepromData.calibrationValue;
// ER1: short on sensor
// ER2: sensor is broken
uint8_t error = (adcVal < 10) ? 1 : (adcVal > 1000) ? 2 : 0;
if (!error)
{
PWM_duty(PWM_CH1, 100); // switch OFF the heater
S7C_setChars("ER");
S7C_setDigit(2, error);
S7C_refreshDisplay(nowTime);
beep();
return;
}
// Check for sleep
static uint8_t oldSleepState = 0;
uint8_t sleepState = checkSleep(nowTime);
if (sleepState != oldSleepState)
{
beepAlarm();
_currentState = sleepState;
oldSleepState = sleepState;
}
// Check for buttons
static uint8_t oldAction = 0;
uint16_t oldHeatPoint = _eepromData.heatPoint;
uint8_t action = checkButton(&_btnPlus, &_eepromData.heatPoint, 1, nowTime) + // ADD button
checkButton(&_btnMinus, &_eepromData.heatPoint, -1, nowTime); // MINUS button
if (action)
{
// when any buttons were pressed we will display target temperature
_heatPointDisplayTime = nowTime + HEATPOINT_DISPLAY_DELAY;
if (action != oldAction && action > 1) // two butons were pressed
{
beepAlarm();
_currentState = (_currentState == FORCED_MODE) ? NORMAL_MODE : FORCED_MODE;
}
if (oldHeatPoint != _eepromData.heatPoint)
{
checkHeatPointValidity();
_haveToSaveData = nowTime;
}
}
oldAction = action;
// Set target temperature
int16_t targetHeatPoint = 0;
switch (_currentState)
{
case SLEEP_MODE:
targetHeatPoint = SLEEP_TEMP;
break;
case DEEPSLEEP_MODE:
targetHeatPoint = 0;
break;
case FORCED_MODE:
targetHeatPoint = _eepromData.heatPoint + _eepromData.forceModeIncrement;
targetHeatPoint = targetHeatPoint > MAX_HEAT ? MAX_HEAT : targetHeatPoint;
break;
case NORMAL_MODE:
default:
targetHeatPoint = _eepromData.heatPoint;
}
// Setup heater
// 50 degrees before the heatPoint we start to slow down the heater
// before that we keep the heater at 50%
// if the diff is negative, we'll stop the heater
int16_t diff = targetHeatPoint - currentDegrees;
int16_t pwmVal = (diff < 0) ? PWM_POWER_OFF : (diff > 50) ? 50 : 90 - diff;
PWM_duty(PWM_CH1, pwmVal);
// Setup display value
// We will show the current heatPoint
// * if any button is pressed
// * till _heatPointDisplayTime timeout is reached
// * when the current temperature is in range ±10 degrees
uint16_t displayVal = (currentDegrees < 0) ? 0 : currentDegrees;
uint8_t tempInRange = (displayVal >= targetHeatPoint - 10) && (displayVal <= targetHeatPoint + 10);
if (nowTime < _heatPointDisplayTime || tempInRange)
{
displayVal = targetHeatPoint;
displaySymbol |= SYM_TEMP;
}
// Setup status symbol, flashing using local counter overflow
displaySymbol |= (_currentState >= SLEEP_MODE) && ((localCnt / 500) % 2) ? SYM_MOON : 0; // 1Hz flashing moon
displaySymbol |= pwmVal < 100 && ((localCnt / 50) % 2) ? SYM_SUN : 0; // 10Hz flashing heater
displaySymbol |= (_currentState == FORCED_MODE) ? SYM_FARS : 0; // F
if (_currentState != DEEPSLEEP_MODE)
{
displaySymbol |= SYM_CELS;
S7C_setDigit(0, displayVal / 100);
S7C_setDigit(1, (displayVal / 10) % 10);
S7C_setDigit(2, displayVal % 10);
}
else
{
// Set blank display
S7C_setSymbol(0, 0);
S7C_setSymbol(1, 0);
S7C_setSymbol(2, 0);
}
S7C_setSymbol(3, displaySymbol);
checkPendingDataSave(nowTime);
S7C_refreshDisplay(nowTime);
localCnt++;
delay_ms(1);
}
uint8_t checkSleep(uint32_t nowTime)
{
static uint8_t oldSensorState = 0;
uint8_t sensorState = getPin(PB5);
if (sensorState != oldSensorState)
{
_sleepTimer = nowTime;
oldSensorState = sensorState;
return NORMAL_MODE;
}
else if ((nowTime - _sleepTimer) > _eepromData.deepSleepTimeout * 60000)
{
//deepSleep();
return DEEPSLEEP_MODE;
}
else if ((nowTime - _sleepTimer) > _eepromData.sleepTimeout * 60000)
{
return SLEEP_MODE;
}
return NORMAL_MODE;
}
void checkHeatPointValidity()
{
if (_eepromData.heatPoint > MAX_HEAT)
_eepromData.heatPoint = MAX_HEAT;
if (_eepromData.heatPoint < MIN_HEAT)
_eepromData.heatPoint = MIN_HEAT;
}
void checkPendingDataSave(uint32_t nowTime)
{
if (_haveToSaveData && (nowTime - _haveToSaveData) > EEPROM_SAVE_TIMEOUT)
{
S7C_setSymbol(3, SYM_SAVE);
eeprom_write(EEPROM_START_ADDR, &_eepromData, sizeof(_eepromData));
_haveToSaveData = 0;
}
}
void deepSleep()
{
static uint16_t localCnt = 0;
PWM_duty(PWM_CH1, 100); // set heater OFF
// Set blank display
S7C_setSymbol(0, 0);
S7C_setSymbol(1, 0);
S7C_setSymbol(2, 0);
while (1)
{
uint8_t displaySymbol = ((localCnt / 500) % 2) ? SYM_MOON : 0; // 1Hz flashing moon
S7C_setSymbol(3, displaySymbol);
S7C_refreshDisplay(localCnt++);
delay_ms(1);
}
}
void main()
{
setup();
while (1)
{
mainLoop();
}
}