~toykeeper/flashlight-firmware/trunk

« back to all changes in this revision

Viewing changes to Tamagotchi/7135x8v2/7135x8v2.c

  • Committer: Selene Scriven
  • Date: 2015-04-06 20:33:22 UTC
  • Revision ID: ubuntu@toykeeper.net-20150406203322-5j64r10cfkm6wpkc
Added Tamagotchi/7135x8v2 firmware from fonarevka.ru

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 7135x8 2.8A Nanjg 105C LED Driver
 
3
 *
 
4
 * Created: 23.01.2012 20:52:39
 
5
 * Last modified: 26.07.2013 02:57:00
 
6
 *
 
7
 * FIRMWARE VERSION: 2.6.5
 
8
 *
 
9
 * Fcpu = 4.8MHz
 
10
 *
 
11
 * This code is distributed under the GNU Public License
 
12
 * which can be found at http://www.gnu.org/licenses/gpl.txt
 
13
 *
 
14
 *  Author: Tamagotchi http://tamagotchi-007.livejournal.com/
 
15
 *                     http://avr.tamatronix.com
 
16
 *  Optimization: DooMmen http://doommen.blogspot.com/
 
17
 *                                vdavid http://forum.fonarevka.ru/member.php?u=20922
 
18
 *
 
19
 *  Download current version: http://109.87.62.61/uploads/LD_7135_v_2.6.5.zip
 
20
 *
 
21
 */
 
22
 
 
23
#include "7135x8v2.h"
 
24
 
 
25
register uint8_t short_off_counter              asm("r2");                      /* WARNING */
 
26
register uint8_t adch_volt                              asm("r3");                      // ����������
 
27
register uint8_t tick_volt                              asm("r4");
 
28
 
 
29
register uint8_t current_mode                   asm("r5");                      /* WARNING */
 
30
register uint8_t mode_saver                             asm("r6");                      /* WARNING */
 
31
register uint8_t ramping_reg                    asm("r7");                      /* WARNING */
 
32
register uint8_t flag_reg                               asm("r16");
 
33
//#define flag_reg                                              DIDR0
 
34
 
 
35
#ifdef THEMPERATURE_CONTROL
 
36
register uint8_t adch_temp                              asm("r8");                      // �����������, ����� ��� ����������
 
37
register uint8_t tick_temp                              asm("r9");
 
38
register uint8_t calibrator10                   asm("r10");                     /* WARNING */
 
39
register uint8_t calibrator11                   asm("r11");                     /* WARNING */
 
40
register uint8_t MAX_THEMP                              asm("r12");                     // ������������ ����������� �������� �� EEPROM
 
41
#else
 
42
        #ifdef TURBO_TIME
 
43
        register uint8_t turbo_timer            asm("r8");
 
44
        register uint8_t prev_mode                      asm("r9");
 
45
        #endif
 
46
#endif
 
47
 
 
48
#ifndef CAPACITOR
 
49
register uint8_t mode_switcher                  asm("r13");
 
50
#endif
 
51
 
 
52
#ifndef NOMEM
 
53
#ifdef WEAR_LEWELING
 
54
uint8_t eeprom_pos = 0;
 
55
#endif
 
56
#endif
 
57
 
 
58
 
 
59
static NOINLINE void WriteEEPROMByte(uint8_t adress, uint8_t byte)
 
60
{
 
61
        while(EECR & _BV(EEPE));
 
62
        EEAR = adress;                                                          // set address
 
63
        EEDR = byte;                                                            // set data
 
64
        cli();                                                                          // disable interrupts
 
65
        EECR |= _BV(EEMWE);                                                     // set "write enable" bit
 
66
        EECR |= _BV(EEWE);                                                      // set "write" bit
 
67
        sei();
 
68
}
 
69
 
 
70
static NOINLINE uint8_t ReadEEPROMByte(uint8_t adress)
 
71
{
 
72
        while(EECR & 0x02);
 
73
        EEAR = adress;                                                          // set address
 
74
        EECR |= _BV(EERE);                                                      // set "read enable" bit
 
75
        return (EEDR);
 
76
}
 
77
 
 
78
#ifndef NOMEM
 
79
#ifdef WEAR_LEWELING
 
80
static INLINE void save_byte(uint8_t startadr, uint8_t data)
 
81
{
 
82
        uint8_t old_eeprom_pos = eeprom_pos;
 
83
        eeprom_pos=(eeprom_pos + 1) & 0x1F;
 
84
        WriteEEPROMByte((eeprom_pos + startadr), data);
 
85
        WriteEEPROMByte((old_eeprom_pos + startadr), 0xFF);
 
86
}
 
87
 
 
88
static NOINLINE uint8_t read_byte(uint8_t startadr)
 
89
{
 
90
        uint8_t eepdata;
 
91
        while(1)
 
92
        {
 
93
                eepdata = ReadEEPROMByte(eeprom_pos + startadr);
 
94
                if((eepdata != 0xFF) || (eeprom_pos >= SIZE))
 
95
                        break;
 
96
                eeprom_pos++;
 
97
        }
 
98
        if (eeprom_pos < SIZE)
 
99
                return eepdata;
 
100
        else
 
101
                eeprom_pos = 0;
 
102
                return 0;
 
103
}
 
104
#endif
 
105
#endif
 
106
 
 
107
static INLINE void start_wdt(void)
 
108
{
 
109
        wdt_reset();
 
110
        WDTCR = (_BV(WDCE) | _BV(WDE));
 
111
        WDTCR = (_BV(WDTIE) | WDTO_250MS);
 
112
}
 
113
 
 
114
ISR(WDT_vect)
 
115
{
 
116
        ClearNewMode();
 
117
        short_off_counter = 0;
 
118
        #ifdef TURBO_TIME
 
119
 
 
120
                WDTCR = (_BV(WDCE) | _BV(WDE));
 
121
                WDTCR = (_BV(WDTIE) | WDTO_2S);
 
122
 
 
123
                if (turbo_timer) turbo_timer--;
 
124
                else
 
125
                #ifdef REVERSE_SW
 
126
                        if (current_mode == MAX_MODES) current_mode--;
 
127
                #else 
 
128
                        if (current_mode == 0) current_mode++;
 
129
                #endif
 
130
        #endif
 
131
}
 
132
 
 
133
static NOINLINE void delay_ms(uint8_t ms)       // ��������� ���, ����� ������ ����� ��������
 
134
{
 
135
        do { _delay_ms(1); }
 
136
        while(--ms);
 
137
}
 
138
 
 
139
static NOINLINE void delay_sec(uint8_t ms)
 
140
{
 
141
        do { _delay_ms(495); }
 
142
        while(--ms);
 
143
}
 
144
 
 
145
static NOINLINE void all_off(void)                      // ��������� ������ ���
 
146
{
 
147
        LED_PORT = PB_CONFIG;
 
148
        OCR0A  = 0x00;
 
149
}
 
150
 
 
151
#ifndef FREQMOON_OFF
 
152
static NOINLINE void cpu_div(void)                      // ������������� �������� �������� �������
 
153
{
 
154
        CLKPR   = 0x80;
 
155
        CLKPR   = 0x02;
 
156
}
 
157
#endif
 
158
 
 
159
static NOINLINE void input_mode(void)
 
160
{
 
161
        all_off();
 
162
        delay_ms(150);
 
163
        mode_saver = 0xAA;
 
164
        #ifdef MODE_SET_LEVEL
 
165
                #if MAX_MODES == 3
 
166
                        current_mode = ((current_mode - 1) & 0x03);             // ������������ ������� �������
 
167
                #else
 
168
                        current_mode--;
 
169
                        if (current_mode == 0xFF)
 
170
                                current_mode = MAX_MODES;
 
171
                #endif
 
172
        #endif
 
173
}
 
174
 
 
175
static NOINLINE void pwr_down(void)
 
176
{
 
177
        cli();
 
178
        ADCSRA  = 0x00; // ��������� ADC
 
179
        TCCR0A  = 0x00; // ��������� ������
 
180
        TCCR0B  = 0x00; // ��������� ������
 
181
        PRR             = 0x03; // 0000 0011
 
182
        DDRB    = 0x00; // �������� ����
 
183
        PORTB   = 0x00;
 
184
        MCUCR   = 0x30; // PwrDown
 
185
        sleep_cpu();
 
186
}
 
187
 
 
188
#ifdef THEMPERATURE_CONTROL
 
189
static INLINE void switch_adc_in(uint8_t is_temp)
 
190
{
 
191
        if (is_temp)                    // ���� �����������
 
192
                #ifdef THERMVD
 
193
                        ADMUX = 0x62;   //  0110 0010   PB4 Vref = 1.1V
 
194
                #else
 
195
                        ADMUX = 0x22;   //  0010 0010   PB4 Vref = Vcc
 
196
                #endif
 
197
        else
 
198
                ADMUX = 0x61;           //  0110 0001   PB2 Vref = 1.1V
 
199
}
 
200
#endif
 
201
 
 
202
 
 
203
ISR (ADC_vect)
 
204
{
 
205
        uint8_t adch_val = ADCH;
 
206
 
 
207
        #ifdef THEMPERATURE_CONTROL
 
208
        switch_adc_in(IsMeasureThemp());//
 
209
 
 
210
        if(IsMeasureThemp())
 
211
        {
 
212
                if (MAX_THEMP)                                                                                  // ���� 0 - ��������� ����� ������
 
213
                {
 
214
                        #ifdef REVERSE_SW
 
215
                        if(adch_val < (uint8_t)(MAX_THEMP + current_mode))  // ������ 0 - ����� 3 - ������
 
216
                        #else
 
217
                        if(adch_val < (uint8_t)(MAX_THEMP - current_mode))  // ������ 0 - ������ 3 - �����
 
218
                        #endif
 
219
                                tick_temp++;                                                                    // ���� ����������� ������ - �������� �������, ����� ���������, ��� ��� �� ������
 
220
                        else
 
221
                                tick_temp = 0;                                                                  // ���� ����������� ��������������� - �����
 
222
                }
 
223
                adch_temp = adch_val;
 
224
        }
 
225
        else
 
226
        {
 
227
        #endif
 
228
                if(adch_val < OFF_VOLTAGE)
 
229
                        tick_volt++;                                                    // ���� ������� ���� - �������� �������, ����� ���������, ��� ��� �� ������
 
230
                else
 
231
                        tick_volt = 0;                                                  // ���� ������� ��������������� - �����
 
232
 
 
233
                adch_volt = adch_val;
 
234
 
 
235
        #ifdef THEMPERATURE_CONTROL
 
236
        }
 
237
        InvMeasureMode();
 
238
        #endif
 
239
 
 
240
        #ifdef THEMPERATURE_CONTROL
 
241
        if((tick_volt > 0x40)|(tick_temp > 0x10))               // �������� ��� ������ � ��������
 
242
        #else
 
243
        if(tick_volt > 0x80)
 
244
        #endif
 
245
        {
 
246
                tick_volt = 0;                                                          // ��������, ����� ���������� �� � ����� �������, � ���������
 
247
                #ifdef THEMPERATURE_CONTROL
 
248
                tick_temp = 0;
 
249
                #endif
 
250
                #ifndef REVERSE_SW
 
251
                current_mode++;                                                         // ��������� �������
 
252
                if (current_mode == (MAX_MODES+1))
 
253
                        SetOffMode();
 
254
                #else
 
255
                current_mode--;                                                         // ����� �������� �������
 
256
                if (current_mode == 0xFF)
 
257
                        SetOffMode();
 
258
                #endif
 
259
        }
 
260
}
 
261
 
 
262
 
 
263
#ifdef FLASH_OFF
 
264
static INLINE void flash(uint8_t i)
 
265
#else
 
266
static NOINLINE void flash(uint8_t i)
 
267
#endif
 
268
{
 
269
        all_off();
 
270
        do{     delay_ms(20);
 
271
                OCR0A ^= 0xFF;
 
272
                }while(--i);
 
273
}
 
274
 
 
275
 
 
276
static NOINLINE void set_mode(uint8_t mode)      // ������ ����� ��� ������ ���� ��������� SetMod3();
 
277
{
 
278
        switch (mode)
 
279
        {
 
280
                case 0:
 
281
                        SetMod0();
 
282
                        break;
 
283
                case 1:
 
284
                        SetMod1();
 
285
                        break;
 
286
                case 2:
 
287
                        SetMod2();
 
288
                        break;
 
289
                #if MAX_MODES >= 3
 
290
                        case 3:
 
291
                                SetMod3();
 
292
                                break;
 
293
                #endif
 
294
                #if MAX_MODES == 4
 
295
                        case 4:
 
296
                                SetMod4();
 
297
                                break;
 
298
                #endif
 
299
                default:
 
300
                        pwr_down();
 
301
                        break;
 
302
        }
 
303
        #ifdef TURBO_TIME
 
304
                if (mode != prev_mode)
 
305
                {
 
306
                        #ifdef REVERSE_SW
 
307
                                if (mode == MAX_MODES) turbo_timer=(TURBO_TIME/2);
 
308
                        #else 
 
309
                                if (mode == 0) turbo_timer=(TURBO_TIME/2);
 
310
                        #endif
 
311
                }
 
312
        prev_mode=mode;
 
313
        #endif
 
314
}
 
315
 
 
316
 
 
317
uint8_t switch_voltage [] = {U1, U2, U3, U4};
 
318
static INLINE void display_voltage(void)
 
319
{
 
320
        uint8_t v_batt;
 
321
        uint8_t i = 0;
 
322
        input_mode();
 
323
        mode_saver = 0;
 
324
        v_batt = adch_volt;
 
325
        while(1)
 
326
        {
 
327
                OCR0A  = 0x80;
 
328
                delay_ms(75);
 
329
                OCR0A  = 0x00;
 
330
                delay_ms(100);
 
331
                if(v_batt < switch_voltage[i])
 
332
                        break;
 
333
                if(++i >= 5)
 
334
                        break;
 
335
        }
 
336
        delay_ms(100);
 
337
 
 
338
        #ifdef THEMPERATURE_CONTROL
 
339
        i = ReadEEPROMByte(CFG_CALIBRATE);
 
340
        if((!i) || (i == 0xFF))                                  // ���� ������������� �������� - ������
 
341
                flash(10);
 
342
        #endif
 
343
}
 
344
 
 
345
 
 
346
#ifdef THEMPERATURE_CONTROL
 
347
static INLINE void calibrate(void)                       // ���������� �������������
 
348
{
 
349
        input_mode();
 
350
        #ifndef FLASH_OFF
 
351
                flash(20);
 
352
        #endif
 
353
        #ifndef REVERSE_SW
 
354
                current_mode = 0;
 
355
        #else
 
356
                current_mode = MAX_MODES;
 
357
        #endif
 
358
        MAX_THEMP = 0;                                                   // �������� ���������� �.�. ��������� ��
 
359
        WriteEEPROMByte(CFG_CALIBRATE, 0);               // �������� � eeprom
 
360
}
 
361
#endif
 
362
 
 
363
 
 
364
static INLINE void mode_line_chg(void)           // �������� MODELINECHG
 
365
#ifdef ONECLICKMOON
 
366
{
 
367
        input_mode();
 
368
        WriteEEPROMByte(CFG_MOONMODE, 0x00);
 
369
        //ClearNewMode();
 
370
}
 
371
#else
 
372
{
 
373
        input_mode();
 
374
        uint8_t m = ReadEEPROMByte(CFG_MOONMODE);
 
375
        m ^= 0xFF;
 
376
        WriteEEPROMByte(CFG_MOONMODE, m);
 
377
        #ifdef RST_LEV_MOON
 
378
                #ifdef REVERSE_SW                                        // ����� ������� ����� ��������
 
379
                        current_mode = 0;
 
380
                #else
 
381
                        current_mode = MAX_MODES;
 
382
                #endif
 
383
        #endif
 
384
}
 
385
#endif
 
386
 
 
387
 
 
388
#ifdef RAMPING
 
389
static INLINE void ramping_loop(void)
 
390
{
 
391
        uint8_t k;
 
392
        input_mode();
 
393
        WriteEEPROMByte(CFG_RAMPING, 0);                 // �������� � eeprom
 
394
        #ifndef FREQMOON_OFF
 
395
                cpu_div();
 
396
        #endif
 
397
        while (1)
 
398
        {
 
399
                #ifdef FREQMOON_OFF
 
400
                        ramping_reg = 2;
 
401
                        k = 6;
 
402
                #else
 
403
                        ramping_reg = 1;
 
404
                        k = 7;
 
405
                #endif
 
406
 
 
407
                do
 
408
                {
 
409
                        OCR0A = ramping_reg;
 
410
                        #ifdef FREQMOON_OFF
 
411
                                delay_ms(250);
 
412
                        #else
 
413
                                delay_ms(125);
 
414
                        #endif
 
415
                        if (IsOffMode())                                 //��� �������� �������
 
416
                                pwr_down();
 
417
                        ramping_reg = (ramping_reg<<1);
 
418
                }while(--k);
 
419
        }
 
420
}
 
421
#endif
 
422
 
 
423
 
 
424
#ifdef VELO_STROBE
 
425
static INLINE void velo_pulse(void)
 
426
{
 
427
        input_mode();
 
428
        #ifndef MODE_SET_LEVEL
 
429
                current_mode = VeloMod;
 
430
        #endif
 
431
        if (VeloPulse == current_mode)
 
432
                current_mode--;
 
433
        while(1)
 
434
        {
 
435
                set_mode(VeloPulse); 
 
436
                delay_ms(25);
 
437
                set_mode(current_mode);
 
438
                delay_sec(VeloOFF);
 
439
        }
 
440
}
 
441
#endif
 
442
 
 
443
 
 
444
#ifdef POLICE_MODE
 
445
static INLINE void police_pulse(void)
 
446
{
 
447
        uint8_t k;
 
448
        input_mode();
 
449
        #ifndef MODE_SET_LEVEL
 
450
                current_mode = SlowPulseMod;
 
451
        #endif
 
452
        do{
 
453
                k = 4;
 
454
                do
 
455
                {
 
456
                        set_mode(current_mode);
 
457
                        delay_ms(5);
 
458
                        all_off();
 
459
                        delay_ms(50);
 
460
                }while(--k);
 
461
                delay_ms(200);
 
462
                delay_ms(140);
 
463
        }while(1);
 
464
}
 
465
#endif
 
466
 
 
467
 
 
468
#ifdef SOS_MODE
 
469
uint8_t sos_delay [] = {255, 75, 75, 75, 75, 75, 225, 225, 75, 225, 75, 225, 225, 75, 75, 75, 75, 75};
 
470
static INLINE void sos_pulse(void)      // ��� ����� SOS
 
471
{
 
472
        uint8_t k;
 
473
        input_mode();
 
474
        #ifndef MODE_SET_LEVEL
 
475
                current_mode = SOSMod;
 
476
        #endif
 
477
        while(1)
 
478
        {
 
479
                k = 0;
 
480
                do
 
481
                {
 
482
                        delay_ms(sos_delay[k]);
 
483
                        k++;
 
484
                        set_mode(current_mode);
 
485
                        delay_ms(sos_delay[k]);
 
486
                        k++;
 
487
                        all_off();
 
488
                }while(k != 18);
 
489
                delay_ms(255);
 
490
        }
 
491
}
 
492
#endif
 
493
 
 
494
 
 
495
#ifdef PULSE_MODE
 
496
static INLINE void pulse(void)          // ��� ������ ����������� ����� (7.5Hz)
 
497
{
 
498
        input_mode();
 
499
        #ifndef MODE_SET_LEVEL
 
500
                current_mode = PulseMod;
 
501
        #endif
 
502
        while(1)
 
503
        {
 
504
                set_mode(current_mode);
 
505
                delay_ms(8);
 
506
                all_off();
 
507
                delay_ms(54);
 
508
        }
 
509
}
 
510
#endif
 
511
 
 
512
 
 
513
#ifdef SLOW_PULSE_MODE
 
514
static INLINE void s_pulse(void)        // ���� ��������� �����, �������������
 
515
{
 
516
        input_mode();
 
517
        #ifndef MODE_SET_LEVEL
 
518
                current_mode = SlowPulseMod;
 
519
        #endif
 
520
        while(1)
 
521
        {
 
522
                set_mode(current_mode);
 
523
                delay_ms(PULSE_ON);
 
524
                all_off();
 
525
                delay_sec(PULSE_OFF);
 
526
        }
 
527
}
 
528
#endif
 
529
 
 
530
 
 
531
#ifdef ALPINE_MODE
 
532
static INLINE void alpine(void)
 
533
{
 
534
        uint8_t i;
 
535
        input_mode();
 
536
        #ifndef MODE_SET_LEVEL
 
537
                current_mode = AlpineMod;
 
538
        #endif
 
539
        while(1)
 
540
        {
 
541
                i = 6;
 
542
                do
 
543
                {
 
544
                        set_mode(current_mode);
 
545
                        delay_ms(100);
 
546
                        all_off();
 
547
                        delay_sec(10);
 
548
                } while (--i);
 
549
                delay_sec(50);
 
550
        }
 
551
}
 
552
#endif
 
553
 
 
554
 
 
555
static INLINE void decode_mode(void)
 
556
{
 
557
        if (IsNewMode())                                                                                                // ���� �������� ����������,...
 
558
        {
 
559
                if(!short_off_counter)
 
560
                {
 
561
                        if(mode_saver == 0xAA)
 
562
                        {
 
563
                                mode_saver = 0;
 
564
                                #ifndef NOMEM
 
565
                                        #ifdef VELO_STROBE
 
566
                                                #ifdef WEAR_LEWELING
 
567
                                                        current_mode = read_byte(CMODE_ADR);
 
568
                                                #else
 
569
                                                        current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
 
570
                                                #endif
 
571
                                        #endif
 
572
                        //      #else
 
573
                        //              current_mode--;
 
574
                                #endif
 
575
                        }
 
576
                        else
 
577
                                current_mode++;
 
578
                }
 
579
                short_off_counter++;
 
580
                if (short_off_counter == BATTERY_CLICKS)                                        //... ��������� ������� ���� �������� ����������
 
581
                {
 
582
                        display_voltage();
 
583
                }
 
584
                #ifdef MODELINECHG
 
585
                else if (short_off_counter == MODELINECHG_CLICKS)
 
586
                        mode_line_chg();
 
587
                #endif
 
588
 
 
589
                #ifdef THEMPERATURE_CONTROL
 
590
                else if (short_off_counter == CALIBRATE_CLICKS)
 
591
                        calibrate();
 
592
                #endif
 
593
 
 
594
                #ifdef RAMPING
 
595
                else if (short_off_counter == RAMPING_CLICKS)
 
596
                        ramping_loop();
 
597
                #endif
 
598
 
 
599
                #ifdef PULSE_MODE
 
600
                else if (short_off_counter == PULSE_MODE_CLICKS)
 
601
                        pulse();
 
602
                #endif
 
603
 
 
604
                #ifdef SLOW_PULSE_MODE
 
605
                else if (short_off_counter == SLOW_PULSE_MODE_CLICKS)
 
606
                        s_pulse();
 
607
                #endif
 
608
 
 
609
                #ifdef ALPINE_MODE
 
610
                else if (short_off_counter == ALPINE_MODE_CLICKS)
 
611
                        alpine();
 
612
                #endif
 
613
 
 
614
                #ifdef VELO_STROBE
 
615
                else if (short_off_counter == VELO_STROBE_CLICKS)
 
616
                        velo_pulse();
 
617
                #endif
 
618
 
 
619
                #ifdef POLICE_MODE
 
620
                else if (short_off_counter == POLICE_MODE_CLICKS)
 
621
                        police_pulse();
 
622
                #endif
 
623
 
 
624
                #ifdef SOS_MODE
 
625
                else if (short_off_counter == SOS_MODE_CLICKS)
 
626
                        sos_pulse();
 
627
                #endif
 
628
        }
 
629
        else
 
630
        {
 
631
                short_off_counter = 0;
 
632
 
 
633
        #ifndef NOMEM                                                                                           // ���� ���� ������� (��������) - ������ �����
 
634
                #ifdef WEAR_LEWELING
 
635
                        current_mode = read_byte(CMODE_ADR);
 
636
                #else
 
637
                        current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
 
638
                #endif
 
639
                        if (current_mode == 0xFF)                                                       // �� ������, ����...
 
640
                        current_mode = START_MODE;
 
641
        #else
 
642
                current_mode = START_MODE;                                                              // ��������, ����� ������ ���������� � ��������
 
643
        #endif
 
644
        }
 
645
 
 
646
        #if MAX_MODES == 3
 
647
                current_mode = current_mode & 0x03;                                             // ������������ ������� �������
 
648
        #else
 
649
                if (current_mode == 0xFF)
 
650
                        current_mode = MAX_MODES;
 
651
                if (current_mode > MAX_MODES)
 
652
                        current_mode = 0;
 
653
        #endif
 
654
}
 
655
 
 
656
 
 
657
static INLINE void initialize(void)
 
658
{
 
659
        CLKPR   = 0x80;
 
660
        CLKPR   = 0x01;                 // ������������� �������� �������� �������
 
661
 
 
662
        // Port B initialization
 
663
        PORTB   = PB_CONFIG;    // PB4, PB5 ����������� � +
 
664
        DDRB    = DDRB_CONFIG;  // PB0, PB1, PB3 - ������
 
665
 
 
666
        ACSR    = 0x80;                 // 1000 0000  Analog Comparator Disable
 
667
        DIDR0   = 0x14;
 
668
 
 
669
        PRR             = 0;
 
670
 
 
671
        ADMUX   = 0x61;                 //  0110 0001 Internal Voltage Reference (1.1V) , ADC1 , 01 PB2 - ������� (��� 10 PB4 - �����������)
 
672
        ADCSRA  = 0xA7;                 //  0xA7 - 1010 1111 ��������, �� �� ����������, ADC Interrupt Enable, Division Factor = 128
 
673
        ADCSRB  = 0;
 
674
 
 
675
        //- �������� ���
 
676
        TCNT0   = 0x00;
 
677
        OCR0A   = 0x00;
 
678
        TCCR0B  = 0x01;
 
679
        TCCR0A  = 0x83;
 
680
 
 
681
        MCUCR   = 0x20;                 // IdleMode
 
682
        TIMSK0  = 0x00;                 // timer interrupt sources
 
683
        GIMSK   = 0x00;                 // interrupt sources
 
684
 
 
685
        sei();                                  // enable interrupts
 
686
}
 
687
 
 
688
 
 
689
static NOINLINE void std_loop(void)
 
690
{
 
691
        while(1)
 
692
        {
 
693
                set_mode(current_mode);
 
694
                if (IsNewMode())
 
695
                {
 
696
                        #ifndef NOMEM
 
697
                                #ifdef WEAR_LEWELING
 
698
                                        delay_sec(1);
 
699
                                        save_byte(CMODE_ADR, current_mode);
 
700
                                #else
 
701
                                        delay_sec(2);
 
702
                                        if((ReadEEPROMByte(CFG_CURRENTMODE)) != current_mode)
 
703
                                                WriteEEPROMByte(CFG_CURRENTMODE, current_mode);
 
704
                                #endif
 
705
                        #endif
 
706
                }
 
707
                #ifdef THEMPERATURE_CONTROL
 
708
                        calibrator10 = adch_temp;
 
709
                        calibrator11 = calibrator10;
 
710
                #endif
 
711
                delay_ms(3);
 
712
        }
 
713
}
 
714
 
 
715
 
 
716
static INLINE void moon_loop(void)                               // MOONMODE
 
717
{
 
718
        mode_saver = 0xAA;
 
719
        OCR0A = ramping_reg;
 
720
 
 
721
        #ifndef FREQMOON_OFF
 
722
                cpu_div();
 
723
        #endif
 
724
        while(1)
 
725
        {
 
726
                sleep_cpu();
 
727
                if (IsOffMode())
 
728
                        pwr_down();
 
729
                delay_ms(1);
 
730
        }
 
731
}
 
732
 
 
733
 
 
734
OS_MAIN int main(void)
 
735
{
 
736
        cli();                                                                           //disable interrupts
 
737
        ClearFlags();
 
738
 
 
739
        #ifdef CAPACITOR
 
740
                if((MCUSR & (1<<BORF)) != 0)
 
741
                {
 
742
                        if((MCUSR & (1<<EXTRF)) == 0)   SetNewMode();
 
743
                        MCUSR=0;
 
744
                }
 
745
        #else
 
746
                if (mode_switcher == 0xAA)
 
747
                        SetNewMode();
 
748
                mode_switcher = 0xAA;
 
749
        #endif
 
750
 
 
751
        wdt_reset();
 
752
        tick_volt = 0;
 
753
        #ifdef THEMPERATURE_CONTROL
 
754
                tick_temp = 0;
 
755
                SetMeasureVolt();
 
756
        #endif
 
757
 
 
758
        #ifdef TURBO_TIME
 
759
                prev_mode=255;
 
760
        #endif
 
761
 
 
762
        initialize();
 
763
        start_wdt();
 
764
 
 
765
#ifdef THEMPERATURE_CONTROL
 
766
        uint8_t temporary = ReadEEPROMByte(CFG_CALIBRATE);
 
767
        if(!temporary)
 
768
        {
 
769
                if(IsNewMode())                                                                 // ���� �������� ���� ���������� ����� �������� ��
 
770
                {
 
771
                        if(calibrator11 == calibrator10)                        // � �����?
 
772
                        {
 
773
                                #ifdef REVERSE_SW
 
774
                                        WriteEEPROMByte(CFG_CALIBRATE, calibrator10 - MAX_MODES);                //
 
775
                                #else
 
776
                                        WriteEEPROMByte(CFG_CALIBRATE, calibrator10);            //
 
777
                                #endif
 
778
                                #ifndef FLASH_OFF
 
779
                                        flash(20);
 
780
                                #endif
 
781
                                pwr_down();
 
782
                        }
 
783
                }
 
784
                else
 
785
                        WriteEEPROMByte(CFG_CALIBRATE, 0xFF);            // � ���� ���������� ������� - ��������� ��
 
786
        }
 
787
        MAX_THEMP = temporary;
 
788
        if(MAX_THEMP == 0xFF) MAX_THEMP = 0;                             // �� ������, ����...
 
789
#endif
 
790
 
 
791
#ifdef RAMPING
 
792
        uint8_t  ramping = ReadEEPROMByte(CFG_RAMPING);
 
793
        if (!ramping)
 
794
        {
 
795
                if(IsNewMode())                                                                  // ���� �������� ���� ���������� ����� ��������
 
796
                {
 
797
                        WriteEEPROMByte(CFG_RAMPING, ramping_reg);
 
798
                        WriteEEPROMByte(CFG_MOONMODE, 0x00);
 
799
                        ClearNewMode();
 
800
                }
 
801
                else
 
802
                        WriteEEPROMByte(CFG_RAMPING, 0xFF);
 
803
        }
 
804
        else
 
805
                ramping_reg = ramping;
 
806
 
 
807
        if (ramping_reg == 0xFF)
 
808
                #ifdef FREQMOON_OFF
 
809
                        ramping_reg = 2;
 
810
                #else
 
811
                        ramping_reg = 1;
 
812
                #endif
 
813
#else
 
814
        #ifdef FREQMOON_OFF
 
815
                ramping_reg = 2;
 
816
        #else
 
817
                ramping_reg = 1;
 
818
        #endif
 
819
#endif
 
820
 
 
821
        adc_on();
 
822
 
 
823
        decode_mode();
 
824
 
 
825
#ifndef MODELINECHG
 
826
                std_loop();
 
827
#else
 
828
        if (ReadEEPROMByte(CFG_MOONMODE) == 0xFF)
 
829
                std_loop();
 
830
        else
 
831
                #ifdef ONECLICKMOON
 
832
                        if(IsNewMode())                                                  // ���� �������� ���� ������� �� MODELINECHG
 
833
                        {
 
834
                                WriteEEPROMByte(CFG_MOONMODE, 0xFF);
 
835
                                #ifdef RST_LEV_MOON
 
836
                                        #ifdef REVERSE_SW                                // ����� ������� ����� ��������
 
837
                                                current_mode = 0;
 
838
                                        #else
 
839
                                                current_mode = MAX_MODES;
 
840
                                        #endif
 
841
                                #else
 
842
                                        ClearNewMode();
 
843
                                #endif
 
844
                                std_loop();
 
845
                        }
 
846
                        else
 
847
                                moon_loop();
 
848
                #else
 
849
                        moon_loop();
 
850
                #endif
 
851
#endif
 
852
}
 
853