//将AD转换完成的数据送到数码管显示
#include <avr/io.h>
#include <avr/interrupt.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char table[10]={0xbf,0x30,0x5b,0x4f,0x66,0x6d,0xfd,0x07,0xff,0xef};
unsigned int b[4]={0};
unsigned int a=0;
unsigned int i;
unsigned int ad_convert(void) //读AD读取的数据
{
unsigned int temp1,temp2;
while(!(0x10|ADCSRA)) ;
temp1=(unsigned int)ADCL;
temp2=(unsigned int)ADCH;
temp2=(temp2<<8)+temp1;
ADCSRA|=0X40;
return(temp2);
}
void delay(unsigned int z)
{
unsigned int i,j;
for (i=0;i<z;i++)
{
for (j=0;j<300;j++)
{
a++;
}
}
}
void display(unsigned int z) //显示函数
{
unsigned int b[4]={0};
b[0]=z/1000;
b[1]=z%1000/100;
b[2]=z%100/10;
b[3]=z%10;
PORTD=0xfe;
PORTC=table[b[0]];
delay(10);
PORTD=0xff;
PORTD=0xfd;
PORTC=table[b[1]];
delay(10);
PORTD=0xff;
PORTD=0xfb;
PORTC=table[b[2]];
delay(10);
PORTD=0xff;
PORTD=0xf7;
PORTC=table[b[3]];
delay(10);
PORTD=0xff;
}
void ADC_init() //ADC初使化
{
ADCSRA|=0x80; //ADC使能
ADMUX=0xc0; //2.5V片内基准电压源 ADC0通道
ADCSRA|=0x43; //启动ADC转换 8分频
}
int main(void)
{
DDRA=0x00;
PORTA=0x01;
DDRD=0xff;
DDRC=0xff;
ADC_init();
while(1)
{
i=ad_convert();
display(i);//TODO:: Please write your application code
}
}
为什么我的只能显示0到200;,而不是0到1024 |