forked from ezhangle/ultramicron
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
277 lines (237 loc) · 7.53 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
#include <stdio.h>
#include <stdint.h>
#include "STM32L1xx.h" // Device header
#include "main.h"
#include "delay.h"
#include "io_ports.h"
#include "rtc.h"
#include "timers.h"
#include "ext2760.h"
#include "interrupt.h"
#include "menu.h"
#include "keys.h"
#include "eeprom.h"
#include "adc.h"
#include "dac.h"
#include "comp.h"
#include "usb.h"
#include "clock.h"
#include "power.h"
#include "stm32l1xx_it.h"
#include "flash_save.h"
uint16_t key; // ìàññèâ íàæàòûõ êíîïîê [012]
uint32_t ix;
uint32_t ix_update;
uint16_t Detector_massive[120+1];
uint32_t ram_Doze_massive[doze_length+1]; // 1 ÿ÷åéêà = 10 ìèíóò, íà ïðîòÿæåíèè ñóòîê
uint32_t ram_max_fon_massive[doze_length+1]; // 1 ÿ÷åéêà = 10 ìèíóò, íà ïðîòÿæåíèè ñóòîê
uint16_t USB_maxfon_massive_pointer=0;
uint16_t USB_doze_massive_pointer=0;
uint16_t current_pulse_count=0;
uint8_t pump_count=0;
uint32_t Doze_day_count=0;
uint32_t Doze_week_count=0;
uint32_t Doze_hour_count=0;
uint32_t Doze_month_count=0;
uint32_t Max_fon=0;
uint8_t main_menu_stat=1;
uint16_t Detector_massive_pointer=0;
uint8_t auto_speedup_factor=0;
uint32_t USB_not_active=0;
uint32_t last_count_pump_on_impulse=0;
FunctionalState pump_on_impulse=DISABLE;
uint32_t menu_select=0;
#ifdef debug
uint32_t debug_wutr=0;
#endif
FunctionalState enter_menu_item=DISABLE;
uint8_t screen=1;
uint8_t stat_screen_number=0;
uint16_t pump_counter_avg_impulse_by_1sec[2];
uint32_t fon_level=0;
FunctionalState poweroff_state=DISABLE;
FunctionalState hidden_menu=DISABLE;
FunctionalState Pump_on_alarm=DISABLE;
uint32_t working_days=0;
uint32_t madorc_impulse=0;
FunctionalState Sound_key_pressed=DISABLE;
DataUpdateDef DataUpdate;
ADCDataDef ADCData;
SettingsDef Settings;
AlarmDef Alarm;
PowerDef Power;
#ifdef debug
WakeupDef Wakeup;
#endif
void sleep_mode(FunctionalState sleep)
{
if(Settings.Sleep_time>0 && !Power.USB_active)
{
Power.Pump_deny=ENABLE;
if(Power.Pump_active)Pump_now(DISABLE);
set_msi();
if(sleep)
{
RTC_ITConfig(RTC_IT_WUT, DISABLE);
Power.led_sleep_time=0;
GPIO_SetBits(GPIOC,GPIO_Pin_13);// Âûêëþ÷àåì ïîäñâåòêó
display_off(); // âûêëþ÷èòü äèñïëåé
GPIO_ResetBits(GPIOA,GPIO_Pin_7);// Ôèêñèðóåì ðåæèì 1.8 âîëüòà, ñ íèçêèì ïîòðåáëåíèåì íîæêè
delay_ms(1000); // ïîäîæäàòü óñòàíîâêè íàïðÿæåíèÿ
DataUpdate.Need_batt_voltage_update=ENABLE; // ðàçðåøèòü ðàáîòó ÀÖÏ
adc_check_event(); // çàïóñòèòü ïðåîáðàçîâàíèå
delay_ms(100); // ïîäîæäàòü óñòàíîâêè íàïðÿæåíèÿ
PWR_FastWakeUpCmd(DISABLE);
PWR_UltraLowPowerCmd(ENABLE);
PWR_PVDCmd(DISABLE);
RTC_ITConfig(RTC_IT_WUT, ENABLE);
}
else
{
RTC_ITConfig(RTC_IT_WUT, DISABLE);
GPIO_SetBits(GPIOA,GPIO_Pin_7);// Ïåðåêëþ÷àåì â ðåæèì 3 âîëüòà
delay_ms(400); // ïîäîæäàòü óñòàíîâêè íàïðÿæåíèÿ
display_on(); // âêëþ÷èòü äèñïëåé
DataUpdate.Need_batt_voltage_update=ENABLE; // ðàçðåøèòü ðàáîòó ÀÖÏ
DataUpdate.Need_display_update=ENABLE;
adc_check_event(); // çàïóñòèòü ïðåîáðàçîâàíèå
RTC_ITConfig(RTC_IT_WUT, ENABLE);
sound_deactivate();
}
Power.Pump_deny=DISABLE;
}
}
void geiger_calc_fon(void)
{
DataUpdate.Need_fon_update=DISABLE;
DataUpdate.Need_display_update=ENABLE;
if(fon_level>Settings.Alarm_level && Settings.Alarm_level>0 && Alarm.Alarm_active==DISABLE)
{
Alarm.Alarm_active=ENABLE;
Alarm.User_cancel=DISABLE;
if(Power.Display_active==DISABLE)
{
screen=1;
Power.sleep_time=Settings.Sleep_time;
Power.led_sleep_time=Settings.Sleep_time-3;
sleep_mode(DISABLE);
sound_activate();
} else sound_activate();
}
if((Alarm.Alarm_active && fon_level<Settings.Alarm_level) || (Alarm.Alarm_active && Settings.Alarm_level==0))
{
sound_deactivate();
Power.Sound_active=DISABLE;
Alarm.Alarm_active=DISABLE;
Alarm.User_cancel=DISABLE;
Alarm.Alarm_beep_count=0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x3000);
set_msi();
DBGMCU_Config(DBGMCU_SLEEP | DBGMCU_STANDBY | DBGMCU_STOP, DISABLE);
set_bor();
Power.sleep_now=DISABLE;
DataUpdate.Need_erase_flash=ENABLE;
Settings.Geiger_voltage=360; // Íàïðÿæåíèå íà äàò÷èêå 360 âîëüò
Settings.Pump_Energy=350; // ýíåðãèÿ íàêà÷êè 350 ìÒë
DataUpdate.current_flash_page=0;
io_init(); // Èíèöèàëèçàöèÿ ïîòðîâ ÌÊ
eeprom_write_default_settings(); // Ïðîâåðêà, çàïîëíåí ëè EEPROM
eeprom_read_settings(); // ×òåíèå íàñòðîåê èç EEPROM
screen=1;
Power.USB_active=DISABLE;
Power.sleep_time=Settings.Sleep_time;
Power.Display_active=ENABLE;
ADCData.DAC_voltage_raw=0x610;
dac_init();
comp_init();
comp_on();
timer9_Config(); // Êîíôèãóðèðóåì òàéìåð íàêà÷êè
timer10_Config();
tim2_Config();
sound_activate();
delay_ms(100);
sound_deactivate();
//--------------------------------------------------------------------
RTC_Config(); // Êîíôèãóðèðóåì ÷àñû
//--------------------------------------------------------------------
// èíèöèàëèçàöèÿ äèñïëåÿ
//--------------------------------------------------------------------
delay_ms(50); // ïîäîæäàòü óñòàíîâêè íàïðÿæåíèÿ
display_on();
LcdInit();
LcdClear();
//--------------------------------------------------------------------
adc_init();
delay_ms(100);
adc_calibration();
delay_ms(10);
//--------------------------------------------------------------------
EXTI8_Config();
#ifdef version_401
EXTI9_Config();
#endif
EXTI3_Config();
EXTI4_Config();
EXTI6_Config();
DataUpdate.Need_batt_voltage_update=ENABLE;
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6))hidden_menu=ENABLE; // Îòêðûòèå ñåðâèñíûõ ïóíêòîâ ìåíþ
delay_ms(500); // ïîäîæäàòü óñòàíîâêè íàïðÿæåíèÿ
while(1)
/////////////////////////////////
{
if(DataUpdate.Need_fon_update==ENABLE) geiger_calc_fon();
if(key>0) keys_proccessing();
if(DataUpdate.Need_batt_voltage_update) adc_check_event();
////////////////////////////////////////////////////
if((Power.sleep_time>0)&&(!Power.Display_active))sleep_mode(DISABLE); // Åñëè äèñïëåé åùå âûêëþ÷åí, à ñ÷åò÷èê ñíà óæå îòñ÷èòûâàåò, ïîäíèìàåì íàïðÿæåíèå è âêëþ÷àåì äèñïëåé
if(Power.Display_active)
{
if(Power.sleep_time==0 && !Alarm.Alarm_active) sleep_mode(ENABLE); // Ñ÷åò÷èê ñíà äîñ÷èòàë äî íóëÿ, à äèñïëåé åùå àêòèâåí, òî âûêëþ÷àåì åãî è ïîíèæàåì íàïðÿæåíèå
if(DataUpdate.Need_display_update==ENABLE)
{
DataUpdate.Need_display_update=DISABLE;
LcdClear_massive();
if (screen==1)main_screen();
if (screen==2)menu_screen();
if (screen==3)stat_screen();
}
///////////////////////////////////////////////////////////////////////////////
}
#ifdef version_401
if((!Power.USB_active) && (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9))){
usb_activate(0x0); // Åñëè ïèòàíèå USB íà÷àëî ïîäàâàòüñÿ âêëþ÷àåì USB
}
#endif
if(!Power.USB_active) // åñëè USB íå àêòèâåí, ìîæíî óõîäèòü â ñîí
{
if(current_pulse_count<30) // Åñëè ñ÷åò÷èê íå çàøêàëèâàåò, òî ìîæíî óéòè â ñîí
{
if(!Power.Pump_active && !Power.Sound_active)
{
PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); // Ïåðåõîäèì â ñîí
#ifdef debug
Wakeup.total_wakeup++;
DataUpdate.Need_display_update=ENABLE;
#endif
} else
{
PWR_EnterSleepMode(PWR_Regulator_ON, PWR_SLEEPEntry_WFI);
#ifdef debug
Wakeup.total_wakeup++;
DataUpdate.Need_display_update=ENABLE;
#endif
}
}
}else USB_work(); // åñëè USB àêòèâåí, ïîïðîáîâàòü ïåðåäàòü äàííûå
#ifdef debug
Wakeup.total_cycle++;
DataUpdate.Need_display_update=ENABLE;
#endif
}
///////////////////////////////////////////////////////////////////////////////
}