pic168xa ADC程序(c語言)
#i nclude <pic168xa.h>
__CONFIG(0x1832);
//芯片配置字,看門狗關(guān),上電延時(shí)開,掉電檢測關(guān),低壓編程關(guān),加密,4M晶體HS振蕩
const char TABLE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void DELAY();
void DELAY11();
void init();
void display(unsigned int x);
void main()
{unsigned int result=0x0000;
do
{
int i;
result=0x0000;
//for(i=4;i--;)
// {
init();
DELAY();
ADGO=0X01;
{result=ADRESH;
result=result<<8;
result=result+ADRESL;};
// }
// result=result>>3;
for(i=10;--i;)
{display(result);};
//DELAY11();};
}while(1);
}
void init()
{
TRISD=0X00;
TRISB=0XE1;
PORTD=0X00;
ADCON1=0X8E;
ADCON0=0XC1;
DELAY();
}
void display(unsigned int x)
{int qian,bai,shi,ge;
unsigned int temp;
temp=x;
qian=temp/0x3e8;
PORTD=TABLE[qian]; //查表得百位顯示的代碼
PORTB=0xef; //RA3輸出低電平,點(diǎn)亮百位顯示
// DELAY11();
bai=(temp%0x3e8)/0x64;
PORTD=TABLE[bai]; //查表得百位顯示的代碼
PORTB=0xf7; //RA3輸出低電平,點(diǎn)亮百位顯示
//DELAY11();
shi=((temp%0x3e8)%0x64)/0xa; //延時(shí)一定時(shí)間,保證顯示亮度
PORTD=TABLE[shi]; //查表得十位顯示的代碼
PORTB=0xfb; //RA4輸出低電平,點(diǎn)亮十位顯示
//DELAY11();
ge=((temp%0x3e8)%0x64)%0xa; //延時(shí)一定時(shí)間,保證亮度
PORTD=TABLE[ge]; //求個(gè)位顯示的代碼
PORTB=0xfd; //RA5輸出低電平,點(diǎn)亮個(gè)位顯示
DELAY11();
}
void DELAY()
{
int i;
for(i=0x100;i--;);
}
void DELAY11()
{
int i;
for(i=0;i<=30;i++)
continue;
}
評論