测试STC12C4052的AD
扫描二维码
随时随地手机看文章
//测试STC12C4052的AD
//P1.7接可变电阻。P3.5 清位即开始转换
#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();
}
}