#include <iot26v.h>
#include <macros.h>
int time1;
unsigned char yy;
unsigned int adc0,adc1,time,tt;
#define out1(x) x?(PORTB|=1<<1):(PORTB&=~(1<<1))
#define out2(x) x?(PORTB|=1<<3):(PORTB&=~(1<<3))
void adc_init(void);
void timer0_init(void);
float adc_v,adcv;
unsigned int adc_v1,adc_v11,ADC1,ADC2,adc_v2,adc_v3;
unsigned char zheng;
void delay_ms(unsigned int t)
{
unsigned int i;
for(;t>0;t--)
for(i=20;i>0;i--);
}
void adc_init(void)
{
ADCSR= 0x00;
ADCMUX = 0x00;
ACSR = (1<<ACD);
ADCSR=0xe1;
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1 = 0xa0;
OCR1A = 0x3E;
OCR1B = 0x3E;
OCR1C = 0x80;
TCCR1A = 0x00;
TCCR1B = 0x06; //start Timer
}
#pragma interrupt_handler timer1_compa_isr:4
void timer1_compa_isr(void)
{
tt++;
if(tt>=yy)
{
tt=0;
if(time1>0)
{
out1(1);
out2(0);
time1--;
}
else if(time1<0)
{
out1(0);
out2(1);
time1++;
}
else
{
out1(0);
out2(0);
time1=0;
}}
/*
if((time1>10)|(time1<-10))
{
time++;
if(time>=1000)
{
time1=0;
time=0;
}
}
else
time=0;*/
}
void main()
{
unsigned int value;
unsigned char xx;
OSCCAL=0x55; //bf
CLI(); //disable all interrupts
adc_init();
timer1_init();
MCUCR = 0x00;
TIMSK = 0x40; //timer interrupt sources
GIMSK = 0x00; //interrupt sources
PLLCSR = 0x00; //PLL
SEI(); //re-enable interrupts
SEI();
PORTA=0X00;
DDRA=0X00;
PORTB=0XFF;
DDRB=0XFF;
delay_ms(150);
if(ADCSR&(1<<ADIF))
{
value=ADCL; //Read 8 low bits first (important)
value|=ADCH << 8; //read 2 high bits and shift into top byte
adc0=(unsigned int)(value);
ADCMUX = 0x00;
}
adc1=adc0;
time1=0;
//OCR1A/B最大255,最小0;
while(1)
{
delay_ms(80);
if(ADCSR&(1<<ADIF))
{
value=ADCL; //Read 8 low bits first (important)
value|=ADCH << 8; //read 2 high bits and shift into top byte
adc0=(unsigned int)(value);
ADCMUX = 0x00;
}
if((adc0>(adc1+1))|(adc0<(adc1-1)))
{
time1+=(adc0-adc1);
adc1=adc0;
}
yy=1;
/*
if((time1<=5)&(time1>=-5))
{
yy=7;
xx=5;
}
else if((time1>10)|(time1<-10))
{
yy=15;
xx=2;
}*/
}
}