#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000
#include <util/delay.h>
unsigned char buffer[4]={0};
unsigned char wei_table[]={0xfe,0xfd,0xfb,0xf7};
unsigned char data[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7e,0x6f};//0~9
unsigned char pos;
void init_adc(void)
{
ADMUX=(1<<REFS1)|(1<<REFS0);//选择片内 基准电压2.56v,选择通道0
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS2)|(0<<ADPS1)|(1<<ADPS0);
//ADC使能,开始转换,自动触发,中断使能,128分频
}
void init_port(void)
{
DDRD=0XFF;//数码管共阳极显示
PORTD=0;
DDRB=0XFF;//数码管阴极控制端
PORTB=0XFF;
DDRA &= ~(1<<PA0);//pa0设置为输入,关闭内部上拉电阻
PORTA &=~(1<<PA0);
}
void adcbuf(unsigned int adc)
{
unsigned int i;
for(i=0;i<4;i++)
{
buffer
=adc%10;
adc/=10;
}
}
SIGNAL(SIG_ADC)
{
unsigned char adc_data,adc_v; //用来存放转换后的结果
adc_data=ADC ;
ADCSRA|=(1<<ADIF); //写1清除标志位
adc_v=(unsigned long)adc_data*50/20;
adcbuf(adc_v);
//ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS1)|(1<<ADPS2)|(1<<ADPS0);
}
SIGNAL(TIMER0_OVF_vect)
{
TCNT0=228;
display();
ADMUX=(1<<REFS1)|(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(1<<ADPS1)|(1<<ADPS2)|(1<<ADPS0);
}
void intt_time0()
{
TCNT0=228;//给定时器写入初值,延时2ms
TIMSK |= (1<<TOIE0);//使能溢出中断
TCCR0 |=(1<<CS00)|(0<<CS01)|(1<<CS02);//选择1024分频
}
void display(void)
{
PORTB |=0x0f;
PORTD =data[buffer[pos]];
PORTB=wei_table[pos];
if(wei_table[pos] == 0xf7)
PORTD=data[buffer[pos]] | 0x80;
if(++pos>=4)
pos=0;
}
int main(void)
{
init_adc();
init_port();
intt_time0();
sei();
while(1)
{
//TODO:: Please write your application code
}
}
代码如上,每次AD转换后结果都显示不了?为什么?
此帖出自小平头技术问答
一周热门 更多>