-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c.bkv1
369 lines (314 loc) · 10.4 KB
/
main.c.bkv1
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//ïðîãðàììà äëÿ óñòðîéñòâà çàùèòû áûòîâîé òåõíèêè îò ïðåâûøåíèÿ íàïðÿæåíèÿ â ñåòè
//íà îñíîâå Tiny26
#include <ioavr.h>
#include <intrinsics.h>
#include "hardware.h"
#include "segments.h"
__eeprom unsigned int UpTresholdEEPROM, DownTresholdEEPROM, K1EEPROM;
__eeprom unsigned int ProtectTimerMaxValueEEPROM;
__flash signed int segmentsDec[3]={100,10,1};
volatile unsigned char ResultSummReady;
unsigned int DisplayValue;//volatile
unsigned char String[3];
unsigned char cycle_count;//, //Brightness = 2;
volatile unsigned char VideoBuffer[3];
unsigned char keypress[2], KeyTimer;
unsigned int ProtectTimerMaxValue, ProtectTimerValue;
unsigned int Voltage;
volatile unsigned int SampleError, UpTreshold, DownTreshold;
unsigned int DisplayedVoltage;
volatile unsigned char DisplayMode = RealtimeVoltage, EnableDisplay = 128; // íåáîëüøàÿ ýêîíîìèÿ êîäà
volatile unsigned long SummUUResult; // îòäàåòñÿ âíåøíåìó öèêëó
unsigned long SummUU; // âíóòðåííåå çíà÷åíèå
volatile unsigned char PlusSamplesCounter, ZeroSamplesCounter;
volatile unsigned char PSResult ;
unsigned int K1; //677 - êàëèáðîâàííîå çíà÷åíèå
inline void __watchdog_init (void)
{
//çàïóñêàþ ñòîðîæåâîé òàéìåð íà 2 ñåêóíäû
__watchdog_reset ();
WDTCR |= ((1<<WDCE)|(1<<WDE));
WDTCR = (1<<WDE)|(7<<WDP0);
__watchdog_reset ();
}
void CharToStringDec(signed int inp) // îáðåçàííàÿ äî ñîòåí
{
unsigned char i;
String[0]=String[1]=String[2]=0;
// ïåðåâîä
for(i=0;i<3;)
{
if((inp-segmentsDec[i])>=0)
{
inp-=segmentsDec[i];
String[i]++;
}
else i++;
}
}
inline void InitPorts (void)
{
PortButton |= (1<<Button_DN)|(1<<Button_UP); // ïîäòÿãèâàþùèå ðåçèñòîðû
PortAnodeDir |= ((1<<SEG_A)|(1<<SEG_B)|(1<<SEG_C)|(1<<SEG_D)|(1<<SEG_E)|(1<<SEG_F)|(1<<SEG_G)|(1<<SEG_DP));
PortKathodeAndRelayDir |= ((1<<Kathode_1) | (1<<Kathode_2) | (1<<Kathode_3)|(1<<Relay));
}
inline void InitADC (void)
{
ADMUX = ((2<<REFS0)|(InADC << MUX0));
ADCSR = ((1 << ADEN)|(5 << ADPS0)|(0<<ADFR)|(0<<ADSC)|(0<<ADIE));
//5 << ADPS0 Prescaler = 32 f = 8000000/(32*13)=19230 Ãö
//6 << ADPS0 Prescaler = 64 f = 8000000/(64*13)=9615 Ãö
//7 << ADPS0 Prescaler = 128
}
inline void InitTimers (void)
{
TCCR0 |= (4<<CS00);//3
TCCR1B |= (4<<CS10);//3
TIMSK |= ((1<<TOIE0)|(1<<TOIE1));
}
void SaveSettings (void)
{
UpTresholdEEPROM = UpTreshold;
DownTresholdEEPROM = DownTreshold;
ProtectTimerMaxValueEEPROM = ProtectTimerMaxValue;
K1EEPROM = K1;
}
void CheckTresholdSettings (void)
{
if (UpTreshold > 270) UpTreshold = 270;
if (UpTreshold < 210) UpTreshold = 210;
if (DownTreshold > 200) DownTreshold = 200;
if (DownTreshold < 120) DownTreshold = 120;
}
void CheckTimerSettings (void)
{
if (ProtectTimerMaxValue > 600) ProtectTimerMaxValue = 600;
if (ProtectTimerMaxValue < 5) ProtectTimerMaxValue = 5;
}
void CheckK1Settings (void)
{
if (K1 > 999) K1 = 999;
if (K1 < 50) K1 = 50;
}
void LoadSettings (void)
{
UpTreshold = UpTresholdEEPROM;
DownTreshold = DownTresholdEEPROM;
ProtectTimerMaxValue = ProtectTimerMaxValueEEPROM;
K1 = K1EEPROM;
CheckTresholdSettings ();
CheckTimerSettings ();
//CheckK1Settings ();
}
inline void UpdateLedScreen (void)
{
switch (DisplayMode)
{
case RealtimeVoltage: DisplayValue = DisplayedVoltage; break;
case UpTresholdVoltage: DisplayValue = UpTreshold; break;
case DownTresholdVoltage: DisplayValue = DownTreshold; break;
case UpTresholdTune: DisplayValue = UpTreshold; break;
case DownTresholdTune: DisplayValue = DownTreshold; break;
case ProtectTimerTune: DisplayValue = ProtectTimerMaxValue; break;
case K1Tune: DisplayValue = K1; break;
case ProtectTimer: DisplayValue = ProtectTimerValue; break;
}
CharToStringDec(DisplayValue);
if (EnableDisplay)
{
for (unsigned char i=0; i<3; i++) {VideoBuffer[i] = led_digits[String[i]];}
if (DisplayMode >= UpTresholdTune) { VideoBuffer[2] |= led_digits[0x10]; }
}
else
{
VideoBuffer[0] = 0;
VideoBuffer[1] = 0;
VideoBuffer[2] = 0;
}
}
#pragma vector = TIMER1_OVF1_vect
__interrupt void ADCSampleReady (void)
{
unsigned long temp;
TCNT1 = T1_RELOAD; // ïåðåçàãðóçèòü òàéìåð
temp = ADC;
ADCSR |= (1<<ADSC);
if (temp > 0)
{
temp *= temp; // âîçâîäèì â êâàäðàò
SummUU += temp;
PlusSamplesCounter++;
ZeroSamplesCounter = 0;
}
else
{ // ñ÷èòàåì ñêîëüêî íóëåâûõ ñåìïëîâ ïîïàëîñü
ZeroSamplesCounter++;
if ((ZeroSamplesCounter > 20)&&(PlusSamplesCounter > 60)) // ïîëóâîëíà çàêîí÷èëàñü
{
SummUUResult = SummUU;//PlusSamplesCounter;
//PSResult = PlusSamplesCounter;
SummUU = 0;
PlusSamplesCounter = 0;
//ZeroSamplesCounter = 0;
ResultSummReady = 1;
}
if (PlusSamplesCounter > 110)
{
SummUU = 0;
PlusSamplesCounter = 0;
}
}
}
void PressProcessing (unsigned char code_state)
{
switch (code_state)
{
case DN_SHORT:
switch (DisplayMode)
{
case RealtimeVoltage: DisplayMode = DownTresholdVoltage; break;
case UpTresholdVoltage: DisplayMode = DownTresholdVoltage; break;
case DownTresholdTune: DownTreshold --; CheckTresholdSettings (); break;
case UpTresholdTune: UpTreshold --; CheckTresholdSettings (); break;
case ProtectTimerTune: ProtectTimerMaxValue -=5; CheckTimerSettings (); break;
case ProtectTimer: DisplayMode = DownTresholdVoltage; break;
case K1Tune: K1 --; break;
}
break;
case UP_SHORT:
switch (DisplayMode)
{
case RealtimeVoltage: DisplayMode = UpTresholdVoltage; break;
case DownTresholdVoltage: DisplayMode = UpTresholdVoltage; break;
case DownTresholdTune: DownTreshold ++; CheckTresholdSettings (); break;
case UpTresholdTune: UpTreshold ++; CheckTresholdSettings (); break;
case ProtectTimerTune: ProtectTimerMaxValue +=5; CheckTimerSettings (); break;
case ProtectTimer: DisplayMode = UpTresholdVoltage; break;
case K1Tune: K1 ++; break;
}
break;
case DN_MID:
if (DisplayMode == DownTresholdVoltage) DisplayMode = DownTresholdTune;
break;
case UP_MID:
if (DisplayMode == UpTresholdVoltage) DisplayMode = UpTresholdTune;
break;
case DN_UP_MID:
switch (DisplayMode)
{
case RealtimeVoltage: DisplayMode = ProtectTimerTune; break;
case ProtectTimerTune: DisplayMode = K1Tune; break;
}
break;
}
}
#pragma vector = TIMER0_OVF0_vect
__interrupt void DynInd (void)
{
TCNT0 = T0_RELOAD; // ïåðåçàãðóçèòü òàéìåð
if (cycle_count < NUMBER_DIGITS)
{
PortKathode |= ((1<<Kathode_1) | (1<<Kathode_2) | (1<<Kathode_3)); //ïîòóøèëè âñå ðàçðÿäû
PortAnode &= ~((1<<SEG_A)|(1<<SEG_B)|(1<<SEG_C)|(1<<SEG_D)|(1<<SEG_E)|(1<<SEG_F)|(1<<SEG_G)|(1<<SEG_DP));
PortAnode = VideoBuffer[cycle_count]; // âûäàòü íîâûå çíà÷åíèÿ äëÿ àíîäîâ
PortKathode &= ~ArrayKathodes[cycle_count];// âêëþ÷èòü êàòîä
}
if (++cycle_count > 2) cycle_count=0;
}
void KeyboardThread (void)
{
if (cycle_count == 2) // ïðîâåðÿåì ëåâóþ êíîïêó
{
if (!(PinButton & (1<<Button_DN))) {keypress[0]++; KeyTimer = KeyTimerMax;}
else
{
if ((keypress[0] < midpress)&&(keypress[0] > shortpress)) PressProcessing(DN_SHORT);
keypress[0]=0;
}
}
if (cycle_count == 0) // ïðîâåðÿåì ïðàâóþ êíîïêó
{
if (!(PinButton & (1<<Button_UP))) {keypress[1]++; KeyTimer = KeyTimerMax;}
else
{
if ((keypress[1] < midpress)&&(keypress[1] > shortpress)) PressProcessing(UP_SHORT);
keypress[1]=0;
}
}
if (keypress[1] == 0) // áûëà íàæàòà êíîïêà ÂÍÈÇ
{
if (keypress[0] == midpress) { keypress[0] = midpress+5; PressProcessing(DN_MID); }
if (keypress[0] > longpress) { keypress[0] = longpress+5; }
if ((keypress[0] == 0)&&(KeyTimer)) KeyTimer--; // èëè íå íàæàòû îáå êíîïêè
}
if (keypress[0] == 0) // áûëà íàæàòà êíîïêà ÂÂÅÐÕ
{
if (keypress[1] == midpress) { keypress[1] = midpress+5; PressProcessing(UP_MID); }
if (keypress[1] > longpress) { keypress[1] = longpress+5; }
}
if ((keypress[0] == midpress)&&(keypress[1] > shortpress)) { keypress[0] = midpress+5; keypress[1] = midpress+5; PressProcessing(DN_UP_MID); }
if ((keypress[0] > longpress)&&(keypress[1] > longpress)) { keypress[0] = longpress+5; keypress[1] = longpress+5; }
if (KeyTimer == 1) { DisplayMode = RealtimeVoltage; SaveSettings (); }
}
#define K2 1000
unsigned char QuarterSecPrescaler, SecPrescaler;
unsigned long isqrt (unsigned long v)
{
unsigned long temp, nHat=0, b=0x8000, bshft=15;
do{
if (v >= (temp = (((nHat<<1)+b)<<bshft--))) { nHat += b; v -= temp; }
}while (b>>=1);
return nHat;
}
int main( void )
{
InitPorts ();
InitTimers ();
InitADC ();
LoadSettings ();
ProtectTimerValue = ProtectTimerMaxValue;
__watchdog_init ();
__enable_interrupt();
while (1)
{
__delay_cycles((CtrlClockRate/1000)*10);
if (ResultSummReady)//
{
unsigned long temp_summ;
__watchdog_reset ();
ResultSummReady = 0;
//Voltage = SummUUResult;
//SummUUResult = 10000000;
temp_summ = SummUUResult;
////temp_summ = isqrt(temp_summ/SamplesCount);
////temp_summ = ((temp_summ*K1)/K2);//K1
////Voltage = (unsigned int)temp_summ; //
Voltage = 123;
//Voltage = PSResult;//PlusSamplesCounter;
}
if ((Voltage > UpTreshold) || (Voltage < DownTreshold))
{
//SampleError = Voltage;
//SampleError = PSResult/10;
ProtectTimerValue = ProtectTimerMaxValue;
}
KeyboardThread ();
UpdateLedScreen ();
////if ((DisplayMode == ProtectTimer)&&((ProtectTimerValue == ProtectTimerMaxValue) || (ProtectTimerValue == 0))) DisplayMode = RealtimeVoltage;
////if ((DisplayMode == RealtimeVoltage)&&(ProtectTimerValue == (ProtectTimerMaxValue - 1))) DisplayMode = ProtectTimer;
////if (ProtectTimerValue) PortRelay &= ~(1<<Relay);
//// else PortRelay |= (1<<Relay);
if (QuarterSecPrescaler++>10) //ðàç â ÷åòâåðòü ñåêóíäû ñþäà çàõîäèì
{
QuarterSecPrescaler = 0;
DisplayedVoltage = Voltage;
if ((ProtectTimerValue == ProtectTimerMaxValue)&&(DisplayMode == RealtimeVoltage)) EnableDisplay += 128; // ìèãàåì ýêðàíîì
else EnableDisplay = 128; // íå ìèãàåì
}
if (SecPrescaler++>80)
{
SecPrescaler = 0;
if (ProtectTimerValue) ProtectTimerValue--;
}
}
}