#include<iom16v.h>
#include<macros.h>
#define uchar unsigned char
#define uint unsigned int
#pragma interrupt_handler timer_1:9
const uchar TAB[]={0X3F,0X06,0X5B,0X4F,0X66,0x6d,0X7D,0X07,0X7F,0X6F};
uchar num,flag=0,a=0;
void delay_nms(uint k) //延时nms
{
uint i,j;
for(i=0;i<k;i++)
for(j=0;j<1140;j++);
}
void port_init()
{
DDRA&=~BIT(7);
PORTA=0XFF;
DDRC=0XFF;
PORTC=0XFF;
DDRD=0X0F;
PORTD=0;
DDRB=0XFF;
}
void timer1_init()
{
TCNT1L=0X96;
TCNT1H=0XE7;
TCCR1A=0;
TCCR1B=0X03;
TIMSK=0X04;
SREG|=BIT(7);
}
void ADC_init()
{
ADCSRA|=BIT(ADEN)|BIT(ADATE)|BIT(ADPS2);//使能ADC,使用自动触发源,
//16分频
ADMUX|=BIT(REFS0)|BIT(MUX2)|BIT(MUX1)|BIT(MUX0);//使用AVCC,PC7通道输入
SFIOR|=BIT(ADTS2)|BIT(ADTS1);//触发源为TC1溢出
}
uint ADC_conver()
{
uint temp,temp1;
temp=(uint)ADCL;
temp1=(uint)ADCH;
temp1=(temp1<<8)+temp;
return temp1;
}
uint digtal_deal(uint i)
{
long x;
uint y;
x=(5000*(long)i)/1023;
y=(uint)x;
return y;
}
void display(uint temp)
{
PORTC=TAB[temp%10];
PORTD=0X07;
delay_nms(1);
PORTD=0XFF;
PORTC=TAB[temp/10%10];
PORTD=0X0B;
delay_nms(1);
PORTD=0XFF;
PORTC=TAB[temp/100%10];
PORTD=0X0D;
delay_nms(1);
PORTD=0XFF;
PORTC=TAB[temp/1000];
PORTD=0X0E;
delay_nms(1);
PORTD=0XFF;
PORTC=0x80;//显示小数点
PORTD=0X0E;
delay_nms(1);
PORTD=0XFF;
}
void main()
{
uint value,dis_value=0;
port_init();
timer1_init();
ADC_init();
ADCSRA|=BIT(ADSC);//开启转换
while(1)
{
value=ADC_conver();
dis_value=digtal_deal(value);
display(dis_value);
}
}
void timer_1()//每50毫秒进入一次中断,中断标志顺便用来触发ADC转换
{
a++;
if(a<=10)//每个通道采样500ms
{
PORTB=0XFF;
ADCSRA&=~BIT(ADEN);
ADMUX&=~BIT(MUX0);//使用AVCC,PC6通道输入
ADCSRA|=BIT(ADEN);
}
else if(a<=20)//每个通道采样500ms
{
if(a==20)
a=0;
ADCSRA&=~BIT(ADEN);
ADMUX|=BIT(REFS0)|BIT(MUX2)|BIT(MUX1)|BIT(MUX0);//使用AVCC,PC7通道输入
ADCSRA|=BIT(ADEN);
PORTB=0;
}
TCNT1L=0X96;
TCNT1H=0XE7;
} |