
#pragma used+
sfrb PINB=3;
sfrb DDRB=4;
sfrb PORTB=5;
sfrb PINC=6;
sfrb DDRC=7;
sfrb PORTC=8;
sfrb PIND=9;
sfrb DDRD=0xa;
sfrb PORTD=0xb;
sfrb TIFR0=0x15;
sfrb TIFR1=0x16;
sfrb TIFR2=0x17;
sfrb PCIFR=0x1b;
sfrb EIFR=0x1c;
sfrb EIMSK=0x1d;
sfrb GPIOR0=0x1e;
sfrb EECR=0x1f;
sfrb EEDR=0x20;
sfrb EEARL=0x21;
sfrb EEARH=0x22;
sfrw EEAR=0x21;   
sfrb GTCCR=0x23;
sfrb TCCR0A=0x24;
sfrb TCCR0B=0x25;
sfrb TCNT0=0x26;
sfrb OCR0A=0x27;
sfrb OCR0B=0x28;
sfrb GPIOR1=0x2a;
sfrb GPIOR2=0x2b;
sfrb SPCR=0x2c;
sfrb SPSR=0x2d;
sfrb SPDR=0x2e;
sfrb ACSR=0x30;
sfrb SMCR=0x33;
sfrb MCUSR=0x34;
sfrb MCUCR=0x35;
sfrb SPMCSR=0x37;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x01
	.EQU __sm_mask=0x0E
	.EQU __sm_adc_noise_red=0x02
	.EQU __sm_powerdown=0x04
	.EQU __sm_powersave=0x06
	.EQU __sm_standby=0x0C
	.EQU __sm_ext_standby=0x0E
	.SET power_ctrl_reg=smcr
	#endif
#endasm

#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-

unsigned char cifra[4];

eeprom unsigned int T_zad_rez_eep=2505; 
eeprom unsigned int T_zad_THA_eep=2505; 

interrupt [17] void timer0_ovf_isr(void)  
{static unsigned char numer=1;
unsigned char simbol;

TCNT0=0x63;

numer++;
if(numer>3){numer=1;};
simbol=cifra[numer];
PORTC|=0b00111000;

switch(simbol)
{case 0x30: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=0;break;
case 0x31: PORTC.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=0;PORTD.4=0;PORTD.3=0;break;
case 0x32: PORTC.2=1;PORTB.0=1;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=0;PORTD.3=1;break;
case 0x33: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=0;PORTD.4=0;PORTD.3=1;break;
case 0x34: PORTC.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=0;PORTD.4=1;PORTD.3=1;break;
case 0x35: PORTC.2=1;PORTB.0=0;PORTD.7=1;PORTD.6=1;PORTD.5=0;PORTD.4=1;PORTD.3=1;break;
case 0x36: PORTC.2=1;PORTB.0=0;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x37: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=0;PORTD.4=0;PORTD.3=0;break;
case 0x38: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x39: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=0;PORTD.4=1;PORTD.3=1;break;
case 0x41: PORTC.2=1;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x43: PORTC.2=1;PORTB.0=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=0;break;
case 0x63: PORTC.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=0;PORTD.3=1;break;
case 0x48: PORTC.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=0;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x2D: PORTC.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTD.4=0;PORTD.3=1;break;
case 0x55: PORTC.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=0;break;
case 0x50: PORTC.2=1;PORTB.0=1;PORTD.7=0;PORTD.6=0;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x45: PORTC.2=1;PORTB.0=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x24: PORTC.2=1;PORTB.0=1;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTD.4=1;PORTD.3=1;break;
case 0x64: PORTC.2=0;PORTB.0=1;PORTD.7=1;PORTD.6=1;PORTD.5=1;PORTD.4=0;PORTD.3=1;break;
case 0x74: PORTC.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=1;PORTD.5=1;PORTD.4=1;PORTD.3=1;break;
case 0x72: PORTC.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=0;PORTD.5=1;PORTD.4=0;PORTD.3=1;break;
default: PORTC.2=0;PORTB.0=0;PORTD.7=0;PORTD.6=0;PORTD.5=0;PORTD.4=0;PORTD.3=0;
};

if(numer==cifra[0]){PORTD.2=1;}else{PORTD.2=0;};

switch(numer)
{case 1: PORTC.5=0;break;
case 2: PORTC.4=0;break;
case 3: PORTC.3=0;break;
};
}

unsigned int read_adc(unsigned char adc_input)
{
(*(unsigned char *) 0x7c)=adc_input | (0xC0 & 0xff);

delay_us(10);

(*(unsigned char *) 0x7a)|=0x40;

while (((*(unsigned char *) 0x7a) & 0x10)==0);
(*(unsigned char *) 0x7a)|=0x10;
return (*(unsigned int *) 0x78) ;
}

void main(void)
{

const unsigned int const_drt=160;  
const unsigned int const_rt0=373; 
const unsigned int const_THA=303; 
const unsigned int const_THA0=230; 
unsigned int buf=0,T_izm,T_zad,OCR_1=0,Tsm=20;
unsigned char a,cnt_proh=0,cnt_proh_sm=0;     
unsigned int sum_Tizm=0;
unsigned char flg_tch=0;   
unsigned char flg_payaln=1; 
unsigned char menu=0;  
unsigned long buf_l;
unsigned int zap_max=0;  

#pragma optsize-
(*(unsigned char *) 0x61)=0x80;
(*(unsigned char *) 0x61)=0x00;
#pragma optsize+

PORTB=0xD4;
DDRB=0x2B;

PORTC=0x40;
DDRC=0x3C;

PORTD=0x03;
DDRD=0xFC;

TCCR0A=0x00;
TCCR0B=0x04;
TCNT0=0x63;
OCR0A=0x00;
OCR0B=0x00;

(*(unsigned char *) 0x80)=0x82;
(*(unsigned char *) 0x81)=0x09;
(*(unsigned char *) 0x85)=0x00;
(*(unsigned char *) 0x84)=0x00;
(*(unsigned char *) 0x87)=0x00;
(*(unsigned char *) 0x86)=0x00;
(*(unsigned char *) 0x89)=0x00;      
(*(unsigned char *) 0x88)=0x00;
(*(unsigned char *) 0x8b)=0x00;
(*(unsigned char *) 0x8a)=0x00;

(*(unsigned char *) 0xb6)=0x00;
(*(unsigned char *) 0xb0)=0x00;
(*(unsigned char *) 0xb1)=0x00;
(*(unsigned char *) 0xb2)=0x00;
(*(unsigned char *) 0xb3)=0x00;
(*(unsigned char *) 0xb4)=0x00;

(*(unsigned char *) 0x69)=0x00;
EIMSK=0x00;
(*(unsigned char *) 0x68)=0x00;

(*(unsigned char *) 0x6e)=0x01;

(*(unsigned char *) 0x6f)=0x00;

(*(unsigned char *) 0x70)=0x00;

ACSR=0x80;
(*(unsigned char *) 0x7b)=0x00;

(*(unsigned char *) 0x7e)=0x3C;
(*(unsigned char *) 0x7c)=0xC0 & 0xff;
(*(unsigned char *) 0x7a)=0x87;

delay_ms(1000);

start:
#asm("cli")
(*(unsigned char *) 0x89) = 0;
(*(unsigned char *) 0x88) = 0;
#asm("sei")
buf=0;
for(a=1;a<=10;a++)
{buf+=read_adc(1);
};
buf=buf/10;
if(buf<1000){flg_payaln=1;}else{flg_payaln=0;}; 
if(flg_payaln==0)  
{buf=0;
for(a=1;a<=10;a++)
{buf+=read_adc(0);
};
buf=buf/10;
if(buf>800)
{cifra[1]=0x45;cifra[2]=0x72; cifra[3]=0x72;   
delay_ms(1000);goto start; 
};
}else       
{buf=0;
for(a=1;a<=10;a++)
{buf+=read_adc(1);
};
buf=buf/10;
if((buf<const_rt0)||(buf>950)) 
{cifra[1]=0x45;cifra[2]=0x72; cifra[3]=0x72;   
delay_ms(1000);goto start; 
};
};
if(flg_payaln==1){T_zad=T_zad_rez_eep;}else{T_zad=T_zad_THA_eep;}; 
while (1)
{cnt_proh++;
buf=0;

if(flg_payaln==1) 
{for(a=1;a<=10;a++)   
{buf+=read_adc(1);
};
buf=buf/10;
if((buf>1010)||(buf<const_rt0)){goto start;}; 

buf=buf-const_rt0;
buf_l=buf;
buf_l=buf_l*1000;
buf_l=buf_l/const_drt;
T_izm=(int)buf_l; 
}
else      
{for(a=1;a<=10;a++)   
{buf+=read_adc(0);
};
buf=buf/10;
if(buf>1010){goto start;}; 
buf_l=const_THA0+((long)buf*1000)/const_THA; 
T_izm=(int)buf_l; 
};

if((T_zad+Tsm)>T_izm) 
{buf_l=(((long)T_zad+(long)Tsm-(long)T_izm))*0x1FF/100;
buf=(int)buf_l;
if(buf>zap_max)    
{zap_max=zap_max+10;
if(zap_max>0x1FF){zap_max=0x1FF;};
buf=zap_max;
};
}else
{buf=0;
};

if(flg_payaln==1)
{if(buf>(0x1FF*7/10)){buf=0x1FF*7/10;};
}else
{if(buf>0x1FF){buf=0x1FF;};   
};
OCR_1=buf;
#asm("cli")
(*(unsigned char *) 0x89) = (char)(OCR_1>>8);
(*(unsigned char *) 0x88) = (char)OCR_1;
#asm("sei")
sum_Tizm=sum_Tizm+T_izm; 
if(cnt_proh>9)
{cnt_proh=0;buf=sum_Tizm/100; 
if(!menu)    
{#asm("cli")
cifra[1]=(char)((buf/100)%10+0x30);cifra[2]=(char)((buf/10)%10+0x30); cifra[3]=(char)(buf%10+0x30);
#asm("sei")
};
buf=sum_Tizm/10;
if(((T_zad+Tsm)>=buf)&&(buf>=(T_zad+Tsm-100))) 
{cnt_proh_sm++; 
flg_tch=0;
}else
{cnt_proh_sm=0;
flg_tch=1; 
}; 
sum_Tizm=0;
if(cnt_proh_sm>3) 
{if((buf>T_zad)&&(Tsm>0)){Tsm--;};
if((buf<T_zad)&&(Tsm<100)){Tsm++;};
cnt_proh_sm=0;
};
};

#asm("cli")
if(flg_tch)
{ if(cifra[0]==0){cifra[0]=3;}else{cifra[0]=0;}; 
} else 
{if(flg_payaln==1){cifra[0]=0;}else{cifra[0]=3;}; 
};                                                
#asm("sei")

if((!PINB.6)||(!PINB.7))
{menu=50;
if((!PINB.6)&&(T_zad<=3450)){T_zad=T_zad+50;};
if((!PINB.7)&&(T_zad>250)){T_zad=T_zad-50;};
};

if(menu)   
{#asm("cli")
if((cnt_proh>=5)||(!PINB.6)||(!PINB.7))
{buf=T_zad/10;
cifra[1]=(char)((buf/100)%10+0x30);cifra[2]=(char)((buf/10)%10+0x30); cifra[3]=(char)(buf%10+0x30);
}else
{cifra[1]=0;cifra[2]=0; cifra[3]=0;
};
#asm("sei")
};

delay_ms(154);
if(menu>0)
{menu--;      

if(menu==1)  
{if(flg_payaln==1){buf=T_zad_rez_eep;}else{buf=T_zad_THA_eep;};
if(T_zad!=buf)
{if(flg_payaln==1){T_zad_rez_eep=T_zad;}else{T_zad_THA_eep=T_zad;};
};
menu=0;
}; 
}; 
if(PINB.3==0){PORTB.3=1;}else{PORTB.3=0;}; 

};
}
