|
#include "STC12C2052AD.H"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_START 0x08
#define ADC_SPEEDH 0x40
#define BAUD 9600
sbit RELAY1=P3^4;
sbit RELAY2=P3^5;
sbit LED=P1^4;
void delay(uint i)
{
uint x;
while(i--)
{x=5000;
while(x--);
}
}
void InitADC()
{
P1=P1M0=P1M1=0x01;
ADC_DATA=0; //清除AD緩存器數(shù)據(jù)
ADC_CONTR=ADC_POWER|ADC_SPEEDH; //啟動(dòng)A/D電源
delay(2);
}
uchar GetADCResult(uchar ch)
{
ADC_CONTR=ADC_POWER|ADC_SPEEDH|ADC_START|ch;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&ADC_FLAG));
ADC_CONTR=ADC_CONTR&(~ADC_FLAG);
return ADC_DATA;
}
void process()
{
uchar adcnumber=0;
uchar adcnumber_temp=0;
uchar n;
uchar i=0;
TMOD=0x01;
TH0=0x3C;
TL0=0xb0;
for (n=0;n<5;n++) //adc convert 5 times
{
adcnumber_temp=GetADCResult(0); // get p10 adc number
if(adcnumber_temp>adcnumber) // get biggest
{adcnumber=adcnumber_temp; }
}
if(adcnumber>152&adcnumber<212)
{ TR0=1;
i++;
if (i==40)
{ i=0;
RELAY1=0; //powerdown
RELAY2=0;
LED=0; //faguang
}
}
else
{ TR0=0;
RELAY1=1; //powerdown
RELAY2=1;
LED=1;
}
}
void main()
{
InitADC();
while(1)
{
process();
}
}
|
|