2
* 7135x8 2.8A Nanjg 105C LED Driver
4
* Created: 23.01.2012 20:52:39
5
* Last modified: 26.07.2013 02:57:00
7
* FIRMWARE VERSION: 2.6.5
11
* This code is distributed under the GNU Public License
12
* which can be found at http://www.gnu.org/licenses/gpl.txt
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
19
* Download current version: http://109.87.62.61/uploads/LD_7135_v_2.6.5.zip
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");
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
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
43
register uint8_t turbo_timer asm("r8");
44
register uint8_t prev_mode asm("r9");
49
register uint8_t mode_switcher asm("r13");
54
uint8_t eeprom_pos = 0;
59
static NOINLINE void WriteEEPROMByte(uint8_t adress, uint8_t byte)
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
70
static NOINLINE uint8_t ReadEEPROMByte(uint8_t adress)
73
EEAR = adress; // set address
74
EECR |= _BV(EERE); // set "read enable" bit
80
static INLINE void save_byte(uint8_t startadr, uint8_t data)
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);
88
static NOINLINE uint8_t read_byte(uint8_t startadr)
93
eepdata = ReadEEPROMByte(eeprom_pos + startadr);
94
if((eepdata != 0xFF) || (eeprom_pos >= SIZE))
98
if (eeprom_pos < SIZE)
107
static INLINE void start_wdt(void)
110
WDTCR = (_BV(WDCE) | _BV(WDE));
111
WDTCR = (_BV(WDTIE) | WDTO_250MS);
117
short_off_counter = 0;
120
WDTCR = (_BV(WDCE) | _BV(WDE));
121
WDTCR = (_BV(WDTIE) | WDTO_2S);
123
if (turbo_timer) turbo_timer--;
126
if (current_mode == MAX_MODES) current_mode--;
128
if (current_mode == 0) current_mode++;
133
static NOINLINE void delay_ms(uint8_t ms) // ��������� ���, ����� ������ ����� ��������
139
static NOINLINE void delay_sec(uint8_t ms)
141
do { _delay_ms(495); }
145
static NOINLINE void all_off(void) // ��������� ������ ���
147
LED_PORT = PB_CONFIG;
152
static NOINLINE void cpu_div(void) // ������������� �������� �������� �������
159
static NOINLINE void input_mode(void)
164
#ifdef MODE_SET_LEVEL
166
current_mode = ((current_mode - 1) & 0x03); // ������������ ������� �������
169
if (current_mode == 0xFF)
170
current_mode = MAX_MODES;
175
static NOINLINE void pwr_down(void)
178
ADCSRA = 0x00; // ��������� ADC
179
TCCR0A = 0x00; // ��������� ������
180
TCCR0B = 0x00; // ��������� ������
181
PRR = 0x03; // 0000 0011
182
DDRB = 0x00; // �������� ����
184
MCUCR = 0x30; // PwrDown
188
#ifdef THEMPERATURE_CONTROL
189
static INLINE void switch_adc_in(uint8_t is_temp)
191
if (is_temp) // ���� �����������
193
ADMUX = 0x62; // 0110 0010 PB4 Vref = 1.1V
195
ADMUX = 0x22; // 0010 0010 PB4 Vref = Vcc
198
ADMUX = 0x61; // 0110 0001 PB2 Vref = 1.1V
205
uint8_t adch_val = ADCH;
207
#ifdef THEMPERATURE_CONTROL
208
switch_adc_in(IsMeasureThemp());//
212
if (MAX_THEMP) // ���� 0 - ��������� ����� ������
215
if(adch_val < (uint8_t)(MAX_THEMP + current_mode)) // ������ 0 - ����� 3 - ������
217
if(adch_val < (uint8_t)(MAX_THEMP - current_mode)) // ������ 0 - ������ 3 - �����
219
tick_temp++; // ���� ����������� ������ - �������� �������, ����� ���������, ��� ��� �� ������
221
tick_temp = 0; // ���� ����������� ��������������� - �����
223
adch_temp = adch_val;
228
if(adch_val < OFF_VOLTAGE)
229
tick_volt++; // ���� ������� ���� - �������� �������, ����� ���������, ��� ��� �� ������
231
tick_volt = 0; // ���� ������� ��������������� - �����
233
adch_volt = adch_val;
235
#ifdef THEMPERATURE_CONTROL
240
#ifdef THEMPERATURE_CONTROL
241
if((tick_volt > 0x40)|(tick_temp > 0x10)) // �������� ��� ������ � ��������
246
tick_volt = 0; // ��������, ����� ���������� �� � ����� �������, � ���������
247
#ifdef THEMPERATURE_CONTROL
251
current_mode++; // ��������� �������
252
if (current_mode == (MAX_MODES+1))
255
current_mode--; // ����� �������� �������
256
if (current_mode == 0xFF)
264
static INLINE void flash(uint8_t i)
266
static NOINLINE void flash(uint8_t i)
276
static NOINLINE void set_mode(uint8_t mode) // ������ ����� ��� ������ ���� ��������� SetMod3();
304
if (mode != prev_mode)
307
if (mode == MAX_MODES) turbo_timer=(TURBO_TIME/2);
309
if (mode == 0) turbo_timer=(TURBO_TIME/2);
317
uint8_t switch_voltage [] = {U1, U2, U3, U4};
318
static INLINE void display_voltage(void)
331
if(v_batt < switch_voltage[i])
338
#ifdef THEMPERATURE_CONTROL
339
i = ReadEEPROMByte(CFG_CALIBRATE);
340
if((!i) || (i == 0xFF)) // ���� ������������� �������� - ������
346
#ifdef THEMPERATURE_CONTROL
347
static INLINE void calibrate(void) // ���������� �������������
356
current_mode = MAX_MODES;
358
MAX_THEMP = 0; // �������� ���������� �.�. ��������� ��
359
WriteEEPROMByte(CFG_CALIBRATE, 0); // �������� � eeprom
364
static INLINE void mode_line_chg(void) // �������� MODELINECHG
368
WriteEEPROMByte(CFG_MOONMODE, 0x00);
374
uint8_t m = ReadEEPROMByte(CFG_MOONMODE);
376
WriteEEPROMByte(CFG_MOONMODE, m);
378
#ifdef REVERSE_SW // ����� ������� ����� ��������
381
current_mode = MAX_MODES;
389
static INLINE void ramping_loop(void)
393
WriteEEPROMByte(CFG_RAMPING, 0); // �������� � eeprom
415
if (IsOffMode()) //��� �������� �������
417
ramping_reg = (ramping_reg<<1);
425
static INLINE void velo_pulse(void)
428
#ifndef MODE_SET_LEVEL
429
current_mode = VeloMod;
431
if (VeloPulse == current_mode)
437
set_mode(current_mode);
445
static INLINE void police_pulse(void)
449
#ifndef MODE_SET_LEVEL
450
current_mode = SlowPulseMod;
456
set_mode(current_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
474
#ifndef MODE_SET_LEVEL
475
current_mode = SOSMod;
482
delay_ms(sos_delay[k]);
484
set_mode(current_mode);
485
delay_ms(sos_delay[k]);
496
static INLINE void pulse(void) // ��� ������ ����������� ����� (7.5Hz)
499
#ifndef MODE_SET_LEVEL
500
current_mode = PulseMod;
504
set_mode(current_mode);
513
#ifdef SLOW_PULSE_MODE
514
static INLINE void s_pulse(void) // ���� ��������� �����, �������������
517
#ifndef MODE_SET_LEVEL
518
current_mode = SlowPulseMod;
522
set_mode(current_mode);
525
delay_sec(PULSE_OFF);
532
static INLINE void alpine(void)
536
#ifndef MODE_SET_LEVEL
537
current_mode = AlpineMod;
544
set_mode(current_mode);
555
static INLINE void decode_mode(void)
557
if (IsNewMode()) // ���� �������� ����������,...
559
if(!short_off_counter)
561
if(mode_saver == 0xAA)
567
current_mode = read_byte(CMODE_ADR);
569
current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
580
if (short_off_counter == BATTERY_CLICKS) //... ��������� ������� ���� �������� ����������
585
else if (short_off_counter == MODELINECHG_CLICKS)
589
#ifdef THEMPERATURE_CONTROL
590
else if (short_off_counter == CALIBRATE_CLICKS)
595
else if (short_off_counter == RAMPING_CLICKS)
600
else if (short_off_counter == PULSE_MODE_CLICKS)
604
#ifdef SLOW_PULSE_MODE
605
else if (short_off_counter == SLOW_PULSE_MODE_CLICKS)
610
else if (short_off_counter == ALPINE_MODE_CLICKS)
615
else if (short_off_counter == VELO_STROBE_CLICKS)
620
else if (short_off_counter == POLICE_MODE_CLICKS)
625
else if (short_off_counter == SOS_MODE_CLICKS)
631
short_off_counter = 0;
633
#ifndef NOMEM // ���� ���� ������� (��������) - ������ �����
635
current_mode = read_byte(CMODE_ADR);
637
current_mode = ReadEEPROMByte(CFG_CURRENTMODE);
639
if (current_mode == 0xFF) // �� ������, ����...
640
current_mode = START_MODE;
642
current_mode = START_MODE; // ��������, ����� ������ ���������� � ��������
647
current_mode = current_mode & 0x03; // ������������ ������� �������
649
if (current_mode == 0xFF)
650
current_mode = MAX_MODES;
651
if (current_mode > MAX_MODES)
657
static INLINE void initialize(void)
660
CLKPR = 0x01; // ������������� �������� �������� �������
662
// Port B initialization
663
PORTB = PB_CONFIG; // PB4, PB5 ����������� � +
664
DDRB = DDRB_CONFIG; // PB0, PB1, PB3 - ������
666
ACSR = 0x80; // 1000 0000 Analog Comparator Disable
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
681
MCUCR = 0x20; // IdleMode
682
TIMSK0 = 0x00; // timer interrupt sources
683
GIMSK = 0x00; // interrupt sources
685
sei(); // enable interrupts
689
static NOINLINE void std_loop(void)
693
set_mode(current_mode);
699
save_byte(CMODE_ADR, current_mode);
702
if((ReadEEPROMByte(CFG_CURRENTMODE)) != current_mode)
703
WriteEEPROMByte(CFG_CURRENTMODE, current_mode);
707
#ifdef THEMPERATURE_CONTROL
708
calibrator10 = adch_temp;
709
calibrator11 = calibrator10;
716
static INLINE void moon_loop(void) // MOONMODE
734
OS_MAIN int main(void)
736
cli(); //disable interrupts
740
if((MCUSR & (1<<BORF)) != 0)
742
if((MCUSR & (1<<EXTRF)) == 0) SetNewMode();
746
if (mode_switcher == 0xAA)
748
mode_switcher = 0xAA;
753
#ifdef THEMPERATURE_CONTROL
765
#ifdef THEMPERATURE_CONTROL
766
uint8_t temporary = ReadEEPROMByte(CFG_CALIBRATE);
769
if(IsNewMode()) // ���� �������� ���� ���������� ����� �������� ��
771
if(calibrator11 == calibrator10) // � �����?
774
WriteEEPROMByte(CFG_CALIBRATE, calibrator10 - MAX_MODES); //
776
WriteEEPROMByte(CFG_CALIBRATE, calibrator10); //
785
WriteEEPROMByte(CFG_CALIBRATE, 0xFF); // � ���� ���������� ������� - ��������� ��
787
MAX_THEMP = temporary;
788
if(MAX_THEMP == 0xFF) MAX_THEMP = 0; // �� ������, ����...
792
uint8_t ramping = ReadEEPROMByte(CFG_RAMPING);
795
if(IsNewMode()) // ���� �������� ���� ���������� ����� ��������
797
WriteEEPROMByte(CFG_RAMPING, ramping_reg);
798
WriteEEPROMByte(CFG_MOONMODE, 0x00);
802
WriteEEPROMByte(CFG_RAMPING, 0xFF);
805
ramping_reg = ramping;
807
if (ramping_reg == 0xFF)
828
if (ReadEEPROMByte(CFG_MOONMODE) == 0xFF)
832
if(IsNewMode()) // ���� �������� ���� ������� �� MODELINECHG
834
WriteEEPROMByte(CFG_MOONMODE, 0xFF);
836
#ifdef REVERSE_SW // ����� ������� ����� ��������
839
current_mode = MAX_MODES;