PIC单片机AD转换程序

#include
#define uchar unsigned char
#define uint unsigned int
__CONFIG(0x3FB1);
void init();
void Delay();
unsigned int getad();
void Display(unsigned char a,unsigned char b,unsigned char c);

unsigned char DIG_CODE[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
void main()
{
unsigned int led;
unsigned char a ,b,c;

init();
while(1)
{
led = getad();
a = led/100;
b=led%100/10;

c=led%10;
Display(a,b,c);
}


}

void Display(unsigned char a,unsigned char b,unsigned char c)
{

PORTB=DIG_CODE[c];
RC4=1;RC5=0;RC6=0;
Delay();
PORTB=DIG_CODE[b];
RC4=0;RC5=1;RC6=0;
Delay();
PORTB=DIG_CODE[a];
RC4=1;RC5=1;RC6=0;
Delay();

}

unsigned int getad()
{
unsigned int ad8;

ADGO =1;
while(ADGO);
ad8 = ADRES;

return(ad8);
}

void init()
{
TRISA =1;
TRISB = 0; //portB 输出
TRISC = 0;
ADCON0 = 0x41;
ADCON1 = 0x00;
Delay();
}

void Delay()
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=10;b>0;b--)
for(a=5;a>0;a--);
}

相关文档
最新文档