PIC单片机ADC的编程设计
2020-01-09 来源:elecfans
#include
__CONFIG(0X1F71);
#define uchar unsigned char
#define uint unsigned int
const uchar Tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
const uchar Tab1[]=
{0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//带小数点的代码
void DisPlay(uchar date1,uchar date2,uchar date3,uchar date4);
void DelayMS(uint z)
{
uint a,b;
for(a=26;a》0;a--)
for(b=z;b》0;b--);
}
/****************************************************
端口初始化函数
*****************************************************/
void PORT_Init(void)
{
TRISB=0X00;
TRISC=0XC0;
}
/****************************************************
ADC模块初始化函数
*****************************************************/
void ADC_Init(void)
{
ADCON1=0X8E;
ADCON0=0X41;
DelayMS(1);
}
/****************************************************
ADC模块开始转换函数
*****************************************************/
uint ADC_Start(void)
{
uint uiADCTemp;
ADGO=1;
while(ADGO);//等待转换完成
uiADCTemp=ADRESH;
uiADCTemp=uiADCTemp《《8|ADRESL;
uiADCTemp=uiADCTemp/1023.0*5000.0;
returnuiADCTemp;
}
void main(void)
{
uint VolTageNum;
uchar num1,num2,num3,num4;
PORT_Init();
ADC_Init();
while(1)
{
VolTageNum=ADC_Start();
num1=VolTageNum/1000;
num2=VolTageNum%1000/100;
num3=VolTageNum%100/10;
num4=VolTageNum%10;
DisPlay(num1,num2,num3,num4);
}
}
/****************************************************
显示函数
*****************************************************/
void DisPlay(uchar date1,uchar date2,uchar date3,uchar date4)
{
PORTB=Tab1[date1];
PORTC=0XFE;
DelayMS(1);
PORTB=0X00;
PORTB=Tab[date2];
PORTC=0XFD;
DelayMS(1);
PORTB=0X00;
PORTB=Tab[date3];
PORTC=0XFB;
DelayMS(1);
PORTB=0X00;
PORTB=Tab[date4];
PORTC=0XF7;
DelayMS(1);
PORTB=0X00;
}