#include "12c5a.h"
#include "intrins.h"
#include "stdio.h"
#define uint unsigned int
#define uchar unsigned char
//sfr ADC_LOW2 = 0XBE;
#define ADC_POWER 0X80
#define ADC_FLAG 0X10
#define ADC_START 0X08
#define ADC_SPEEDLL 0X00
#define ADC_SPEEDL 0X20
#define ADC_SPEEDH 0X40
#define ADC_SPEEDHH 0X60
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
unsigned int AD_get(unsigned char channel) //ad初始化
{
ADC_CONTR=0x88|channel;
_nop_();
_nop_();
_nop_();
_nop_();
while(!(ADC_CONTR&0x10));
ADC_CONTR&=0xe7;
return(ADC_RES*4+ADC_RESL);
}
float AD_work(unsigned char channel) //ad求平均值
{
float AD_val;
unsigned char i;
unsigned int mun;
for(i=0;i<10;i++)
AD_val+=AD_get(channel);
AD_val/=10;
AD_val=AD_get(channel);
AD_val=AD_val*(5.0/1024);
mun=AD_val*100;
if(0<mun<320) P0=0X80;//P0控制led
else if(320<mun<380) P0=0x40;
else if(380<mun<499) P0=0x20;
return AD_val;
}
void main()
{
while(1)
{
AD_work(0);
}
}
|