#include #include
本文引用地址:http://m.butianyuan.cn/article/201611/320512.htm#define uint unsigned int
#define uchar unsigned char
sfr P1ASF = 0X9D; //P1口選擇功能寄存器
sfr ADC_CONTR = 0XBC; //ADC控制寄存器
sfr ADC_RES = 0XBD; //ADC轉(zhuǎn)換結(jié)果寄存器高
sfr ADC_RESL = 0XBE; //ADC轉(zhuǎn)換結(jié)果寄存器低
sfr AUXR1 = 0XA2; //ADC轉(zhuǎn)換結(jié)果調(diào)整位
//sfr IE = 0XA8; //ADC中斷允許寄存器
//sfr IP = 0XB8; //ADC中斷優(yōu)先級(jí)控制寄存器高
//sfr IPH = 0XB7; //ADC中斷優(yōu)先級(jí)控制寄存器低
uint ADC_Result1, ADC_Result2, ADC_Result;
void ADC_init(void); //AD轉(zhuǎn)換初始化
void Delay(uint z); //延時(shí)函數(shù)
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;
//數(shù)碼管動(dòng)態(tài)顯示顯示采集數(shù)值
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轉(zhuǎn)換初始化
{
P1ASF = 0X01; //X選擇P1.0作為ADC轉(zhuǎn)換輸入通道
ADC_CONTR = 0X80; //打開(kāi)ADC轉(zhuǎn)換電源
_nop_();
_nop_();
AUXR1 = 0X04; //將高兩位存放在寄存器ADC_RES,低八位存放在寄存器ADC_RESL中
ADC_CONTR |= 0X68; //ADC初始化設(shè)置
_nop_();
_nop_();
_nop_();
_nop_ ();
ADC_Result1 = ADC_RES;
ADC_Result2 = ADC_RESL;
}
void Delay(uint z) //延時(shí)函數(shù)
{
uint x, y;
for (x = 0; x < z; x++)
for (y = 0; y < 140; y++);
}
評(píng)論