#include"STC12C5410AD.h"#define uchar unsigned charunsigned char aa,bb;void delay(uchar dd){int x,y;for(x=dd;x>0;x--)for(y=100;y>0;y--);}unsigned char Read_ADC(unsigned char adcvcc){//將P1.0--P1.1設置成適合AD轉(zhuǎn)換的模式ADC_CONTR = ADC_CONTR|0x80; //1000,0000打開A/D轉(zhuǎn)換電源delay(20); //20msADC_CONTR = ADC_CONTR0xE0; //1110,0000 清ADC_FLAG,ADC_START位和低3位ADC_CONTR = ADC_CONTR0xf8|adcvcc; //設置當前通道號delay(10); //延時使輸入電壓達到穩(wěn)定ADC_DATA = 0; //清A/D轉(zhuǎn)換結(jié)果寄存器ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,啟動轉(zhuǎn)換do { ; } while((ADC_CONTR 0x10)==0); //0001,0000等待A/D轉(zhuǎn)換結(jié)束ADC_CONTR = ADC_CONTR0xE7; //1110,0111清ADC_FLAG和ADC_START位,停止A/D轉(zhuǎn)換return ADC_DATA;}void main(){//里要加上設置ADC的端口,如)1.0,P1.1設為ADC口的話:P1M0 = 0x03; //0000,0011用于A/D轉(zhuǎn)換的P1.x口,先設為開漏P1M1 = 0xC3; //0000,0011P1.0--P1.1先設為開漏。斷開內(nèi)部上拉電阻bb=0x01;while(1){aa=Read_ADC(1); //第一路A/D轉(zhuǎn)換;如要改別的路,只要把代入通道改一下.P3=aa; //這是讓A/D的數(shù)據(jù)顯示在P3口的LED上.}}
評論