~toykeeper/flashlight-firmware/trunk

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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
/*
 * 7135x8 2.8A Nanjg 105C LED Driver
 *
 * Created: 23.01.2012 20:52:39
 * Last modified: 26.07.2013 02:57:00
 *
 * FIRMWARE VERSION: 2.6.5
 *
 * Fcpu = 4.8MHz
 *
 * This code is distributed under the GNU Public License
 * which can be found at http://www.gnu.org/licenses/gpl.txt
 *
 *  Author: Tamagotchi http://tamagotchi-007.livejournal.com/
 *                     http://avr.tamatronix.com
 *  Optimization: DooMmen http://doommen.blogspot.com/
 *				  vdavid http://forum.fonarevka.ru/member.php?u=20922
 *
 *  Download current version: http://109.87.62.61/uploads/LD_7135_v_2.6.5.zip
 *
 */

#include "7135x8v2.h"

register uint8_t short_off_counter		asm("r2");			/* WARNING */
register uint8_t adch_volt				asm("r3");			// íàïðÿæåíèå
register uint8_t tick_volt				asm("r4");

register uint8_t current_mode			asm("r5");			/* WARNING */
register uint8_t mode_saver				asm("r6");			/* WARNING */
register uint8_t ramping_reg			asm("r7");			/* WARNING */
register uint8_t flag_reg				asm("r16");
//#define flag_reg						DIDR0

#ifdef THEMPERATURE_CONTROL
register uint8_t adch_temp				asm("r8");			// òåìïåðàòóðà, íóæíî äëÿ êàëèáðîâêè
register uint8_t tick_temp				asm("r9");
register uint8_t calibrator10			asm("r10");			/* WARNING */
register uint8_t calibrator11			asm("r11");			/* WARNING */
register uint8_t MAX_THEMP				asm("r12");			// ìàêñèìàëüíàÿ òåìïåðàòóðà ÷èòàåòñÿ èç EEPROM
#else
	#ifdef TURBO_TIME
	register uint8_t turbo_timer		asm("r8");
	register uint8_t prev_mode			asm("r9");
	#endif
#endif

#ifndef CAPACITOR
register uint8_t mode_switcher			asm("r13");
#endif

#ifndef NOMEM
#ifdef WEAR_LEWELING
uint8_t eeprom_pos = 0;
#endif
#endif


static NOINLINE void WriteEEPROMByte(uint8_t adress, uint8_t byte)
{
	while(EECR & _BV(EEPE));
	EEAR = adress;								// set address
	EEDR = byte;								// set data
	cli();										// disable interrupts
	EECR |= _BV(EEMWE);							// set "write enable" bit
	EECR |= _BV(EEWE);							// set "write" bit
	sei();
}

static NOINLINE uint8_t ReadEEPROMByte(uint8_t adress)
{
	while(EECR & 0x02);
	EEAR = adress;								// set address
	EECR |= _BV(EERE);							// set "read enable" bit
	return (EEDR);
}

#ifndef NOMEM
#ifdef WEAR_LEWELING
static INLINE void save_byte(uint8_t startadr, uint8_t data)
{
	uint8_t old_eeprom_pos = eeprom_pos;
	eeprom_pos=(eeprom_pos + 1) & 0x1F;
	WriteEEPROMByte((eeprom_pos + startadr), data);
	WriteEEPROMByte((old_eeprom_pos + startadr), 0xFF);
}

static NOINLINE uint8_t read_byte(uint8_t startadr)
{
	uint8_t eepdata;
	while(1)
	{
		eepdata = ReadEEPROMByte(eeprom_pos + startadr);
		if((eepdata != 0xFF) || (eeprom_pos >= SIZE))
			break;
		eeprom_pos++;
	}
	if (eeprom_pos < SIZE)
		return eepdata;
	else
		eeprom_pos = 0;
		return 0;
}
#endif
#endif

static INLINE void start_wdt(void)
{
	wdt_reset();
	WDTCR = (_BV(WDCE) | _BV(WDE));
	WDTCR = (_BV(WDTIE) | WDTO_250MS);
}

ISR(WDT_vect)
{
	ClearNewMode();
	short_off_counter = 0;
	#ifdef TURBO_TIME

		WDTCR = (_BV(WDCE) | _BV(WDE));
		WDTCR = (_BV(WDTIE) | WDTO_2S);

		if (turbo_timer) turbo_timer--;
		else
		#ifdef REVERSE_SW
			if (current_mode == MAX_MODES) current_mode--;
		#else 
			if (current_mode == 0) current_mode++;
		#endif
	#endif
}

static NOINLINE void delay_ms(uint8_t ms)	// îôîðìëÿåì òàê, ÷òîáû ìåíüøå ìåñòà çàíèìàëî
{
	do { _delay_ms(1); }
	while(--ms);
}

static NOINLINE void delay_sec(uint8_t ms)
{
	do { _delay_ms(495); }
	while(--ms);
}

static NOINLINE void all_off(void)			// Âûêëþ÷àåì âîîáùå âñå
{
	LED_PORT = PB_CONFIG;
	OCR0A  = 0x00;
}

#ifndef FREQMOON_OFF
static NOINLINE void cpu_div(void)			// Óñòàíàâëèâàåì äåëèòåëü òàêòîâîé ÷àñòîòû
{
	CLKPR	= 0x80;
	CLKPR	= 0x02;
}
#endif

static NOINLINE void input_mode(void)
{
	all_off();
	delay_ms(150);
	mode_saver = 0xAA;
	#ifdef MODE_SET_LEVEL
		#if MAX_MODES == 3
			current_mode = ((current_mode - 1) & 0x03);		// îãðàíè÷èâàåì ñ÷åò÷èê ðåæèìîâ
		#else
			current_mode--;
			if (current_mode == 0xFF)
				current_mode = MAX_MODES;
		#endif
	#endif
}

static NOINLINE void pwr_down(void)
{
	cli();
	ADCSRA	= 0x00;	// îòêëþ÷àåì ADC
	TCCR0A	= 0x00;	// îòêëþ÷àåì òàéìåð
	TCCR0B	= 0x00;	// îòêëþ÷àåì òàéìåð
	PRR		= 0x03;	// 0000 0011
	DDRB	= 0x00;	// îòðóáàåì ïîðò
	PORTB	= 0x00;
	MCUCR	= 0x30;	// PwrDown
	sleep_cpu();
}

#ifdef THEMPERATURE_CONTROL
static INLINE void switch_adc_in(uint8_t is_temp)
{
	if (is_temp) 			// åñëè òåìïåðàòóðà
		#ifdef THERMVD
			ADMUX = 0x62;	//  0110 0010	PB4 Vref = 1.1V
		#else
			ADMUX = 0x22;	//  0010 0010	PB4 Vref = Vcc
		#endif
	else
		ADMUX = 0x61;		//  0110 0001	PB2 Vref = 1.1V
}
#endif


ISR (ADC_vect)
{
	uint8_t adch_val = ADCH;

	#ifdef THEMPERATURE_CONTROL
	switch_adc_in(IsMeasureThemp());//

	if(IsMeasureThemp())
	{
		if (MAX_THEMP)											// Åñëè 0 - îòêëþ÷àåì òåðìî çàùèòó
		{
			#ifdef REVERSE_SW
			if(adch_val < (uint8_t)(MAX_THEMP + current_mode))  // ðåæèìû 0 - ñëàáî 3 - ñèëüíî
			#else
			if(adch_val < (uint8_t)(MAX_THEMP - current_mode))  // ðåæèìû 0 - ñèëüíî 3 - ñëàáî
			#endif
				tick_temp++;									// åñëè òåìïåðàòóðà âåëèêà - íà÷èíàåì ñ÷èòàòü, ÷òîáû óáåäèòüñÿ, ÷òî ýòî íå ïîìåõà
			else
				tick_temp = 0;									// åñëè òåìïåðàòóðà íîðìàëèçîâàëàñü - îòáîé
		}
		adch_temp = adch_val;
	}
	else
	{
	#endif
		if(adch_val < OFF_VOLTAGE)
			tick_volt++;							// åñëè íàïðóãà ìàëà - íà÷èíàåì ñ÷èòàòü, ÷òîáû óáåäèòüñÿ, ÷òî ýòî íå ïîìåõà
		else
			tick_volt = 0;							// åñëè íàïðóãà íîðìàëèçîâàëàñü - îòáîé

		adch_volt = adch_val;

	#ifdef THEMPERATURE_CONTROL
	}
	InvMeasureMode();
	#endif

	#ifdef THEMPERATURE_CONTROL
	if((tick_volt > 0x40)|(tick_temp > 0x10))		// çàäåðæêà äëÿ áîðüáû ñ ïîìåõàìè
	#else
	if(tick_volt > 0x80)
	#endif
	{
		tick_volt = 0;								// îáíóëÿåì, ÷òîáû ñáðàñûâàòü íå â ñàìûé ìèíèìóì, à ïîðåæèìíî
		#ifdef THEMPERATURE_CONTROL
		tick_temp = 0;
		#endif
		#ifndef REVERSE_SW
		current_mode++;								// óìåíüøàåì ÿðêîñòü
		if (current_mode == (MAX_MODES+1))
			SetOffMode();
		#else
		current_mode--;								// çäåñü îáðàòíûé ïîðÿäîê
		if (current_mode == 0xFF)
			SetOffMode();
		#endif
	}
}


#ifdef FLASH_OFF
static INLINE void flash(uint8_t i)
#else
static NOINLINE void flash(uint8_t i)
#endif
{
	all_off();
	do{	delay_ms(20);
		OCR0A ^= 0xFF;
		}while(--i);
}


static NOINLINE void set_mode(uint8_t mode)	 // ñåé÷àñ âåðíî äëÿ ñâåðõó âíèç îòêëþ÷åíî SetMod3();
{
	switch (mode)
	{
		case 0:
			SetMod0();
			break;
		case 1:
			SetMod1();
			break;
		case 2:
			SetMod2();
			break;
		#if MAX_MODES >= 3
			case 3:
				SetMod3();
				break;
		#endif
		#if MAX_MODES == 4
			case 4:
				SetMod4();
				break;
		#endif
		default:
			pwr_down();
			break;
	}
	#ifdef TURBO_TIME
		if (mode != prev_mode)
		{
			#ifdef REVERSE_SW
				if (mode == MAX_MODES) turbo_timer=(TURBO_TIME/2);
			#else 
				if (mode == 0) turbo_timer=(TURBO_TIME/2);
			#endif
		}
	prev_mode=mode;
	#endif
}


uint8_t switch_voltage [] = {U1, U2, U3, U4};
static INLINE void display_voltage(void)
{
	uint8_t v_batt;
	uint8_t i = 0;
	input_mode();
	mode_saver = 0;
	v_batt = adch_volt;
	while(1)
	{
		OCR0A  = 0x80;
		delay_ms(75);
		OCR0A  = 0x00;
		delay_ms(100);
		if(v_batt < switch_voltage[i])
			break;
		if(++i >= 5)
			break;
	}
	delay_ms(100);

	#ifdef THEMPERATURE_CONTROL
	i = ReadEEPROMByte(CFG_CALIBRATE);
	if((!i) || (i == 0xFF))					 // åñëè òåðìîêîíòðîëü îòêëþ÷åí - ìèãàåì
		flash(10);
	#endif
}


#ifdef THEMPERATURE_CONTROL
static INLINE void calibrate(void)			 // êàëèáðîâêà òåðìîêîíòðîëÿ
{
	input_mode();
	#ifndef FLASH_OFF
		flash(20);
	#endif
	#ifndef REVERSE_SW
		current_mode = 0;
	#else
		current_mode = MAX_MODES;
	#endif
	MAX_THEMP = 0;							 // îáíóëÿåì ïåðåìåííóþ ò.å. îòêëþ÷àåì ÒÊ
	WriteEEPROMByte(CFG_CALIBRATE, 0);		 // îáíóëÿåì â eeprom
}
#endif


static INLINE void mode_line_chg(void)		 // âêëþ÷àåì MODELINECHG
#ifdef ONECLICKMOON
{
	input_mode();
	WriteEEPROMByte(CFG_MOONMODE, 0x00);
	//ClearNewMode();
}
#else
{
	input_mode();
	uint8_t m = ReadEEPROMByte(CFG_MOONMODE);
	m ^= 0xFF;
	WriteEEPROMByte(CFG_MOONMODE, m);
	#ifdef RST_LEV_MOON
		#ifdef REVERSE_SW 					 // ñáðîñ ÿðêîñòè ïîñëå ìóíëàéòà
			current_mode = 0;
		#else
			current_mode = MAX_MODES;
		#endif
	#endif
}
#endif


#ifdef RAMPING
static INLINE void ramping_loop(void)
{
	uint8_t k;
	input_mode();
	WriteEEPROMByte(CFG_RAMPING, 0);		 // îáíóëÿåì â eeprom
	#ifndef FREQMOON_OFF
		cpu_div();
	#endif
	while (1)
	{
		#ifdef FREQMOON_OFF
			ramping_reg = 2;
			k = 6;
		#else
			ramping_reg = 1;
			k = 7;
		#endif

		do
		{
			OCR0A = ramping_reg;
			#ifdef FREQMOON_OFF
				delay_ms(250);
			#else
				delay_ms(125);
			#endif
			if (IsOffMode())				 //äëÿ êîíòðîëÿ áàòàðåè
				pwr_down();
			ramping_reg = (ramping_reg<<1);
		}while(--k);
	}
}
#endif


#ifdef VELO_STROBE
static INLINE void velo_pulse(void)
{
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = VeloMod;
	#endif
	if (VeloPulse == current_mode)
		current_mode--;
	while(1)
	{
		set_mode(VeloPulse); 
		delay_ms(25);
		set_mode(current_mode);
		delay_sec(VeloOFF);
	}
}
#endif


#ifdef POLICE_MODE
static INLINE void police_pulse(void)
{
	uint8_t k;
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = SlowPulseMod;
	#endif
	do{
		k = 4;
		do
		{
			set_mode(current_mode);
			delay_ms(5);
			all_off();
			delay_ms(50);
		}while(--k);
		delay_ms(200);
		delay_ms(140);
	}while(1);
}
#endif


#ifdef SOS_MODE
uint8_t sos_delay [] = {255, 75, 75, 75, 75, 75, 225, 225, 75, 225, 75, 225, 225, 75, 75, 75, 75, 75};
static INLINE void sos_pulse(void)	// ýòî ðåæèì SOS
{
	uint8_t k;
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = SOSMod;
	#endif
	while(1)
	{
		k = 0;
		do
		{
			delay_ms(sos_delay[k]);
			k++;
			set_mode(current_mode);
			delay_ms(sos_delay[k]);
			k++;
			all_off();
		}while(k != 18);
		delay_ms(255);
	}
}
#endif


#ifdef PULSE_MODE
static INLINE void pulse(void)		// ýòî ÷àñòûé àíòèñîáà÷èé ïóëüñ (7.5Hz)
{
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = PulseMod;
	#endif
	while(1)
	{
		set_mode(current_mode);
		delay_ms(8);
		all_off();
		delay_ms(54);
	}
}
#endif


#ifdef SLOW_PULSE_MODE
static INLINE void s_pulse(void)	// ýòîò ìåäëåííûé ïóëüñ, íàñòðàèâàåòñÿ
{
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = SlowPulseMod;
	#endif
	while(1)
	{
		set_mode(current_mode);
		delay_ms(PULSE_ON);
		all_off();
		delay_sec(PULSE_OFF);
	}
}
#endif


#ifdef ALPINE_MODE
static INLINE void alpine(void)
{
	uint8_t i;
	input_mode();
	#ifndef MODE_SET_LEVEL
		current_mode = AlpineMod;
	#endif
	while(1)
	{
		i = 6;
		do
		{
			set_mode(current_mode);
			delay_ms(100);
			all_off();
			delay_sec(10);
		} while (--i);
		delay_sec(50);
	}
}
#endif


static INLINE void decode_mode(void)
{
	if (IsNewMode())												// åñëè êîðîòêîå îòêëþ÷åíèå,...
	{
		if(!short_off_counter)
		{
			if(mode_saver == 0xAA)
			{
				mode_saver = 0;
				#ifndef	NOMEM
					#ifdef VELO_STROBE
						#ifdef WEAR_LEWELING
							current_mode = read_byte(CMODE_ADR);
						#else
							current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
						#endif
					#endif
			//	#else
			//		current_mode--;
				#endif
			}
			else
				current_mode++;
		}
		short_off_counter++;
		if (short_off_counter == BATTERY_CLICKS)					//... ïðîâåðÿåì ñêîëüêî áûëî êîðîòêèõ îòêëþ÷åíèé
		{
			display_voltage();
		}
		#ifdef MODELINECHG
		else if (short_off_counter == MODELINECHG_CLICKS)
			mode_line_chg();
		#endif

		#ifdef THEMPERATURE_CONTROL
		else if (short_off_counter == CALIBRATE_CLICKS)
			calibrate();
		#endif

		#ifdef RAMPING
		else if (short_off_counter == RAMPING_CLICKS)
			ramping_loop();
		#endif

		#ifdef PULSE_MODE
		else if (short_off_counter == PULSE_MODE_CLICKS)
			pulse();
		#endif

		#ifdef SLOW_PULSE_MODE
		else if (short_off_counter == SLOW_PULSE_MODE_CLICKS)
			s_pulse();
		#endif

		#ifdef ALPINE_MODE
		else if (short_off_counter == ALPINE_MODE_CLICKS)
			alpine();
		#endif

		#ifdef VELO_STROBE
		else if (short_off_counter == VELO_STROBE_CLICKS)
			velo_pulse();
		#endif

		#ifdef POLICE_MODE
		else if (short_off_counter == POLICE_MODE_CLICKS)
			police_pulse();
		#endif

		#ifdef SOS_MODE
		else if (short_off_counter == SOS_MODE_CLICKS)
			sos_pulse();
		#endif
	}
	else
	{
		short_off_counter = 0;

	#ifndef	NOMEM												// åñëè áûëî äëèííîå (âêëþ÷èëè) - ÷èòàåì ðåæèì
		#ifdef WEAR_LEWELING
			current_mode = read_byte(CMODE_ADR);
		#else
			current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
		#endif
			if (current_mode == 0xFF)							// íà ñëó÷àé, åñëè...
			current_mode = START_MODE;
	#else
		current_mode = START_MODE;								// îáíóëÿåì, ÷òîáû âñåãäà ñòàðòîâàòü ñ ìèíèìóìà
	#endif
	}

	#if MAX_MODES == 3
		current_mode = current_mode & 0x03;						// îãðàíè÷èâàåì ñ÷åò÷èê ðåæèìîâ
	#else
		if (current_mode == 0xFF)
			current_mode = MAX_MODES;
		if (current_mode > MAX_MODES)
			current_mode = 0;
	#endif
}


static INLINE void initialize(void)
{
	CLKPR	= 0x80;
	CLKPR	= 0x01;			// óñòàíàâëèâàåì äåëèòåëü òàêòîâîé ÷àñòîòû

	// Port B initialization
	PORTB	= PB_CONFIG;	// PB4, PB5 ïîäòÿãèâàåì ê +
	DDRB	= DDRB_CONFIG;	// PB0, PB1, PB3 - âûõîäû

	ACSR	= 0x80;			// 1000 0000  Analog Comparator Disable
	DIDR0	= 0x14;

	PRR		= 0;

	ADMUX	= 0x61;  		//  0110 0001 Internal Voltage Reference (1.1V) , ADC1 , 01 PB2 - íàïðóãà (èëè 10 PB4 - òåìïåðàòóðà)
	ADCSRA	= 0xA7;  		//  0xA7 - 1010 1111 âêëþ÷àåì, íî íå àêòèâèðóåì, ADC Interrupt Enable, Division Factor = 128
	ADCSRB	= 0;

	//- âêëþ÷àåì ØÈÌ
	TCNT0	= 0x00;
	OCR0A	= 0x00;
	TCCR0B	= 0x01;
	TCCR0A	= 0x83;

	MCUCR	= 0x20;			// IdleMode
	TIMSK0	= 0x00;			// timer interrupt sources
	GIMSK	= 0x00;			// interrupt sources

	sei();					// enable interrupts
}


static NOINLINE void std_loop(void)
{
	while(1)
	{
		set_mode(current_mode);
		if (IsNewMode())
		{
			#ifndef	NOMEM
				#ifdef WEAR_LEWELING
					delay_sec(1);
					save_byte(CMODE_ADR, current_mode);
				#else
					delay_sec(2);
					if((ReadEEPROMByte(CFG_CURRENTMODE)) != current_mode)
						WriteEEPROMByte(CFG_CURRENTMODE, current_mode);
				#endif
			#endif
		}
		#ifdef THEMPERATURE_CONTROL
			calibrator10 = adch_temp;
			calibrator11 = calibrator10;
		#endif
		delay_ms(3);
	}
}


static INLINE void moon_loop(void)				 // MOONMODE
{
	mode_saver = 0xAA;
	OCR0A = ramping_reg;

	#ifndef FREQMOON_OFF
		cpu_div();
	#endif
	while(1)
	{
		sleep_cpu();
		if (IsOffMode())
			pwr_down();
		delay_ms(1);
	}
}


OS_MAIN int main(void)
{
	cli();										 //disable interrupts
	ClearFlags();

	#ifdef CAPACITOR
		if((MCUSR & (1<<BORF)) != 0)
		{
			if((MCUSR & (1<<EXTRF)) == 0)	SetNewMode();
			MCUSR=0;
		}
	#else
		if (mode_switcher == 0xAA)
			SetNewMode();
		mode_switcher = 0xAA;
	#endif

	wdt_reset();
	tick_volt = 0;
	#ifdef THEMPERATURE_CONTROL
		tick_temp = 0;
		SetMeasureVolt();
	#endif

	#ifdef TURBO_TIME
		prev_mode=255;
	#endif

	initialize();
	start_wdt();

#ifdef THEMPERATURE_CONTROL
	uint8_t temporary = ReadEEPROMByte(CFG_CALIBRATE);
	if(!temporary)
	{
		if(IsNewMode())									// åñëè êîðîòêèé êëèê çàïèñûâàåì íîâîå çíà÷åíèå ÒÊ
		{
			if(calibrator11 == calibrator10)			// à âäðóã?
			{
				#ifdef REVERSE_SW
					WriteEEPROMByte(CFG_CALIBRATE, calibrator10 - MAX_MODES);		 //
				#else
					WriteEEPROMByte(CFG_CALIBRATE, calibrator10);		 //
				#endif
				#ifndef FLASH_OFF
					flash(20);
				#endif
				pwr_down();
			}
		}
		else
			WriteEEPROMByte(CFG_CALIBRATE, 0xFF);		 // à åñëè ïðåðûâàíèå äëèííîå - îòêëþ÷àåì ÒÊ
	}
	MAX_THEMP = temporary;
	if(MAX_THEMP == 0xFF) MAX_THEMP = 0;				 // íà ñëó÷àé, åñëè...
#endif

#ifdef RAMPING
	uint8_t  ramping = ReadEEPROMByte(CFG_RAMPING);
	if (!ramping)
	{
		if(IsNewMode())									 // åñëè êîðîòêèé êëèê çàïèñûâàåì íîâîå çíà÷åíèå
		{
			WriteEEPROMByte(CFG_RAMPING, ramping_reg);
			WriteEEPROMByte(CFG_MOONMODE, 0x00);
			ClearNewMode();
		}
		else
			WriteEEPROMByte(CFG_RAMPING, 0xFF);
	}
	else
		ramping_reg = ramping;

	if (ramping_reg == 0xFF)
		#ifdef FREQMOON_OFF
			ramping_reg = 2;
		#else
			ramping_reg = 1;
		#endif
#else
	#ifdef FREQMOON_OFF
		ramping_reg = 2;
	#else
		ramping_reg = 1;
	#endif
#endif

	adc_on();

	decode_mode();

#ifndef MODELINECHG
		std_loop();
#else
	if (ReadEEPROMByte(CFG_MOONMODE) == 0xFF)
		std_loop();
	else
		#ifdef ONECLICKMOON
			if(IsNewMode())							 // åñëè êîðîòêèé êëèê âûõîäèì èç MODELINECHG
			{
				WriteEEPROMByte(CFG_MOONMODE, 0xFF);
				#ifdef RST_LEV_MOON
					#ifdef REVERSE_SW 				 // ñáðîñ ÿðêîñòè ïîñëå ìóíëàéòà
						current_mode = 0;
					#else
						current_mode = MAX_MODES;
					#endif
				#else
					ClearNewMode();
				#endif
				std_loop();
			}
			else
				moon_loop();
		#else
			moon_loop();
		#endif
#endif
}