#include
#define uint unsigned int
#define uchar unsigned char
sfr P1ASF = 0X9D; //P1口选择功能寄存器
sfr ADC_CONTR = 0XBC; //ADC控制寄存器
sfr ADC_RES = 0XBD; //ADC转换结果寄存器高
sfr ADC_RESL = 0XBE; //ADC转换结果寄存器低
sfr AUXR1 = 0XA2; //ADC转换结果调整位
//sfr IE = 0XA8; //ADC中断允许寄存器
//sfr IP = 0XB8; //ADC中断优先级控制寄存器高
//sfr IPH = 0XB7; //ADC中断优先级控制寄存器低
uint ADC_Result1, ADC_Result2, ADC_Result;
void ADC_init(void); //AD转换初始化
void Delay(uint z); //延时函数
void main(void)
{
uint gei, shi, bai, qian;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
while (1)
{
ADC_init();
ADC_Result = ADC_Result2 + ADC_Result1 * 256;
gei = ADC_Result % 10;
shi = ADC_Result / 10 % 10;
bai = ADC_Result / 100 % 10;
qian = ADC_Result / 1000;
//数码管动态显示显示采集数值
P2 = 1;
P0 = table[qian];
Delay(5);
P2 = 2;
P0 = table[bai];
Delay(5);
P2 = 3;
P0 = table[shi];
Delay(5);
P2 = 4;
P0 = table[gei];
Delay(5);
}
}
void ADC_init(void) //AD转换初始化
{
P1ASF = 0X01; //X选择P1.0作为ADC转换输入通道
ADC_CONTR = 0X80; //打开ADC转换电源
_nop_();
_nop_();
AUXR1 = 0X04; //将高两位存放在寄存器ADC_RES,低八位存放在寄存器ADC_RESL中
ADC_CONTR |= 0X68; //ADC初始化设置
_nop_();
_nop_();
_nop_();
_nop_ ();
ADC_Result1 = ADC_RES;
ADC_Result2 = ADC_RESL;
}
void Delay(uint z) //延时函数
{
uint x, y;
for (x = 0; x < z; x++)
for (y = 0; y < 140; y++);
}