//ICC-AVR application builder : 2007/6/30 下
// Target : T13
// Crystal: 9.2160Mhz
// Select internal 1.1V referenct--select channel 2
#include <iot13v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uint AD_result; // save ad result;
uint con_volt;
uint discon_vlot;
uchar Pro_Iso_flg;
uchar always_on;
uchar Short_Check_flg=0;
uint timer[2];
uchar timer_dou=0;
// desired value: 27uSec
// actual value: 0.027mSec (99.5%)
#pragma interrupt_handler timer0_ovf_isr:4
void timer0_ovf_isr (void)
{
TCNT0 =0x01; //reload counter value
if(timer_dou++ > 250)//6750us
{
timer_dou=0;
if(Pro_Iso_flg == 2) timer[0]++;
if(Short_Check_flg == 1)
{
if(timer[1]++ > 10)//6
{
timer[1]=0;
Short_Check_flg=2;
}
}
if(Short_Check_flg == 3)
{
if(timer[1]++ >= 2222)
{
Short_Check_flg=0;
always_on=0;
Pro_Iso_flg=0;
}
}
}
}
uint sampling (void)
{
unsigned char AD_H,AD_L;
ADCSRA|=0b01000000;//start convert
while(ADCSRA & 0b01000000);
AD_L=ADCL;
AD_H=ADCH;
return((uint)((AD_H<<8)+AD_L));
}
// 1.1/1024=0.00107V 205.895/5.895=34.927*0.00107=0.03737
void AD_value (void)
{
AD_result=sampling()*37;//1024*1.0mV
}
void control (void)
{
if(Short_Check_flg != 3)
{
if (!(PINB & 0x08))
{
if((AD_result > 16300) && (Pro_Iso_flg != 0))
{
Pro_Iso_flg=0;
TCCR0A=0x03;
PORTB&=0XFE; //Clear PB0
Short_Check_flg=0;
}
else if ((AD_result > con_volt && AD_result < 15500) && (Pro_Iso_flg == 0)) // on >12.0 <14.0v
{
TCCR0A = 0x83;
Pro_Iso_flg=1;
always_on=0;
timer[0]=0;
Short_Check_flg=1;
timer[1]=0;
}
else if((AD_result < discon_vlot) && (Pro_Iso_flg != 0)) //off <11.0v
{
Pro_Iso_flg=2;
if(timer[0] >= 2222)
{
TCCR0A = 0x03;
PORTB&=0XFE; //Clear PB0
Pro_Iso_flg=0;
Short_Check_flg=0;
}
}
else
{
always_on=0;
}
}
else if((PINB&0x08) && (always_on == 0)) // all the time switch on
{
TCCR0A=0x83;
always_on=1;
Pro_Iso_flg=1;
Short_Check_flg=1;
timer[1]=0;
timer[0]=2200;
}
}
}
void main (void)
{
MCUCR = 0x00;
TIMSK0 = 0x00; //timer interrupt sources
GIMSK = 0x00; //interrupt sources
PORTB = 0x00;
DDRB = 0x01;
WDTCR=0x18;//disable watchdog
WDTCR=0x00;
//ADC initialize
// Conversion time: 750uS
ADCSRA = 0x00; //disable adc
ADCMUX = 0x41; // Select internal 1.1V referenct--select channel 2
//ACSR = 0x80;
ADCSRB = 0x00;
ADCSRA=0b10000111;//division factor=128;adc interrupt disable;single mode
//TIMER0 initialize - prescale:1
// WGM: PWM Fast
// desired value: 25uSec
// actual value: 0.027mSec (99.5%)
TCCR0B = 0x00; //stop
OCR0A = 18;
OCR0B = 0xFF;
TCNT0 = 0x01; //set count
TCCR0A =0x03;
TCCR0B = 0x01; //start timer
MCUCR = 0x00;
TIMSK0 = 0x02; //timer interrupt sources
GIMSK = 0x00; //interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
PORTB&=0XFE; //Clear PB0
always_on=0;
Pro_Iso_flg=0;
while(1)
{
if(PINB&0x10) // Isolator
{
con_volt=13700;
discon_vlot=12800;
}
else // Protector
{
con_volt=12980;
discon_vlot=11850;
}
if(!(PINB&0x02) && (Short_Check_flg == 2))
{
TCCR0A=0x03;
PORTB&=0XFE;
Short_Check_flg=3;
always_on=0;
Pro_Iso_flg=0;
timer[1]=0;
}
AD_value();
control();
}
}