今天用M16搞个AD程序,有个很奇怪的结果
我把ADC0接到GND,显示0.22,接到AREF上显示0.55,程序如下大家看看是哪里错了?
#include <mega16.h>
#include <delay.h>
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0x00
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
delay_us(10);
ADCSRA|=0x40;
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
unsigned int adc_v;
unsigned char da[4];
void conv(unsigned int adc )
{
unsigned char i;
for(i=0;i<3;i++)
da[i]= adc%10;
adc/=10;
}
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTC=0x00;
DDRC=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x86;
lcd_init(16);
adc_v=(unsigned long)read_adc(0)*5000/1024;
conv(adc_v);
lcd_clear();
lcd_gotoxy(5,1);
lcd_putchar(da[3]+0x30);
lcd_gotoxy(6,1);
lcd_putchar('.');
lcd_gotoxy(7,1);
lcd_putchar(da[2]+0x30);
lcd_gotoxy(8,1);
lcd_putchar(da[1]+0x30);
lcd_gotoxy(9,1);
lcd_putchar(da[0]+0x30);
while (1);
}
我知道大家不爱看程序的可我怎么能说明白呢?我是在实验板上做的硬件没问题,只有程序了。呵呵。 |