pic单片机AD转换使用查询法的调试程序(无数字滤波)
#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3B31);
const uchar disp[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delayms(uint x)
{
uint j,k;
for(j=x;j>0;j--)
for(k=110;k>0;k--);
}
void display(uint x )
{
PORTD= disp[x/1000]|0x80;
PORTA= 0xF1;
delayms(2);
PORTD= disp[x/100%10];
PORTA= 0xF3;
delayms(2);
PORTD= disp[x/10%10];
PORTA= 0xF5;
delayms(2);
PORTD= disp[x%10];
PORTA= 0xF7;
delayms(2);
}
void init()
{
TRISA=0x01;
TRISC=0;
PORTC=0x01;
TRISD=0;
ADCON0=0x41;//8分频(4M)、AN0、使能ADC
ADCON1=0x8E;//右对齐、AN0通道
delayms(10);
}
uint getad()
{
uint adval;
float advalf;
ADGO=1; //使能ADC转换
while(ADGO); //等待转换完成
adval=ADRESH;
adval=adval<<8|ADRESL;//10位精度
advalf=adval/1023.0*5.0;
adval=advalf*1000;
return (adval); //返回转换值到数码管显示
}
void main()
{
uint temp;
init();
while(1)
{
temp=getad();
display(temp);
}
}