问题如题
程序如下
#define F_CPU 8000000UL
#include<avr/io.h>
#include<util/delay.h>
#define INT8U unsigned char
#define INT16U unsigned int
const INT8U SEG_CODE[]={0XC0,0Xf9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XFF};
INT8U Display_Buffer[]={0,0,0,10,};
INT8U i;
void ADC_Convert()
{
int Result;
ACSR=0X80;
ADCSRA=0X00;
ADCSRA=0XC6;
ADCSRB=0X00;
ADMUX=0XC6;
_delay_us(100);
if (!(ADCSRA&(ADIF<<1)))
{
Result=ADCH*110/1023;
Display_Buffer[0]=Result/100;
Display_Buffer[1]=Result%100/10;
Display_Buffer[2]=Result%10;
for(i=0;i<4;i++)
{
PORTD=0xff;
PORTB=_BV(i);
PORTD=SEG_CODE[Display_Buffer
];
if(i==0)
PORTD&=0X7f;
_delay_ms(4);
}
ADCSRA|=ADIF<<1;
}
}
int main()
{
DDRD=0XFF;PORTD=0XFF;
DDRB=0X0F;PORTB=0X00;
_delay_ms(256);
while(1)
{
ADC_Convert();
}
}
电路如下 仿真时候总是显示0.00
此帖出自小平头技术问答
下边是注解
#define F_CPU 8000000UL
#include<avr/io.h>
#include<util/delay.h>
#define INT8U unsigned char
#define INT16U unsigned int
const INT8U SEG_CODE[]={0XC0,0Xf9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XFF};
INT8U Display_Buffer[]={0,0,0,10,};
INT8U i;
void ADC_Convert()
{
int Result;
ACSR=0X80; //关闭比较器
ADCSRA=0X00;
ADCSRA=0XC6;//选择使能,开始转换,64分频
ADCSRB=0X00;//连续转换
ADMUX=0XC6;//内部基准电压
_delay_us(100);
if (!(ADCSRA&(ADIF<<1))) //半段是否结束
{
Result=ADCH*110/1023;
Display_Buffer[0]=Result/100;
Display_Buffer[1]=Result%100/10;
Display_Buffer[2]=Result%10;
for(i=0;i<4;i++)
{
PORTD=0xff; //始化端口
PORTB=_BV(i);
PORTD=SEG_CODE[Display_Buffer];//输出的数值
if(i==0)
PORTD&=0X7f; //小数点显示
_delay_ms(4);
}
ADCSRA|=ADIF<<1; //清寄存器
}
}
int main()
{
DDRD=0XFF;PORTD=0XFF;
DDRB=0X0F;PORTB=0X00;
_delay_ms(256);
while(1)
{
ADC_Convert();
}
}
一周热门 更多>