測(cè)試STC12C4052的AD
//測(cè)試STC12C4052的AD
//P1.7接可變電阻。P3.5 清位即開始轉(zhuǎn)換
#include "STC12C4052AD.h"
void delay()
{
unsigned char l2;
unsigned char l3 = 10;
while(--l3)
{
l2 = 20;
while( --l2);
}
}
main()
{
P3_7 = 0;
ADC_CONTER = 0x80; //開啟ad比較電壓
while(1)
{
P3_5 = 1;
if ( P3_5 == 0 )
{
P1M0 = 0x80; //P1.7為開漏輸入
P1M1 = 0x80; //P1.7為高阻輸入
ADC_CONTER = 0xE7; //P1_7為ad
delay();
ADC_DATA = 0;
ADC_CONTER |= 0x08;
while ((ADC_CONTER & 0x10) != 0x10 )P1 = ADC_CONTER;
ADC_CONTER &= 0xe7;
P1 = ADC_DATA;
P3_7 = 1;
P1M0 = 0;
while(1);
}
}
}
//-------------------第二種方法 --------------------
#define uchar unsigned char
#define uint unsigned int
#include "stc12c5410ad.h"
void InitAd(void)
{
P1M0=0xff;
P1M1=0xff;
}
void InitUart(void)
{
SCON|=0X50;
TH1=TL1=0XFB;
TMOD|=0X20;
ES=1;
TR1=1;
}
void delay(void)
{
uint i;
for(i=0;i<0xfff0;i++);
}
void ReadAd(void)
{
uchar temp[16],i;
for(i=0;i<8;i++)
{
ADC_CONTR=i;
ADC_CONTR|=0x88;
while(!(ADC_CONTR&0x10));
ADC_CONTR=ADC_CONTR&0xef;
temp[2*i]=ADC_DATA;
temp[2*i+1]=ADC_LOW2;
}
SBUF=0xa0;
while(!TI);
TI=0;
for(i=0;i<16;i++)
{
SBUF=temp[i];
while(!TI);
TI=0;
}
SBUF=0xf6;
while(!TI);
TI=0;
}
main()
{
InitAd();
InitUart();
while(1)
{
ReadAd();
delay();
}
}