分类: 嵌入式
2014-01-22 13:11:50
#define F_CPU 8000000UL //定义,保证delay函数的准确 #include#include #include #include int num_tab[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; int num=0; void led_num( num) { unsigned char temp,i; for(i=0;i<4;i++) { PORTB=0XFF; temp=num%10; PORTA=num_tab[temp]&0xfe; PORTB=~(1<<(3-i)); _delay_us(20); num/=10; } } void IO_init(void) { PORTA=0Xfe; PORTB=0XFF; PORTC=0XFF; PORTD=0Xaf; DDRA=0XFe; DDRB=0XFF; DDRC=0XFF; DDRD=0Xff; } void adc_enable(void) { ADMUX=0Xc0; ADCSRA=0X86; ADCSRA|=0X40; } int read_adc(void) { char a,b; int c=0; ADCSRA|=0X40; while(1) { if(ADCSRA&0X10) { a=ADCL; b=ADCH; c=(b<<8)|a; return c; } } } int main(void) { int a,b=0,c; IO_init(); adc_enable(); while(1) { b=read_adc(); b=(b*256)/1024; for(c=0;c<100;c++) led_num(b); } }