我的源码如下。哪位大侠有空烦请帮忙优化指点下?谢谢了!
另外,是否可以休眠,休眠后P04能输入触发ADC解除休眠状态。
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
unsigned char xdata ADCdataH[10], ADCdataL[10];
unsigned int ADP03,ADP04,ADP07,ADP17;
unsigned int tempH,tempL,tempHLD,tempHLI;
/******************************************************************************
DS功能:
P17=2.5V电压
开机检测一次P07电压,不循环。当P07<P17时,P13为高电平,当P07>P17时,P13为低电平。
检测P03电压,并保存不变的固定电压值假设为a。
下面为循环到关机为止。
检测P04电压,
当P04>P03时,P06和P14输出高电平。
当P04<P03时,(P06和P14输出低电平,也可以不管,因为上面默认=0)
P0.6/P1.3/P0.0/P1.6输出高电平
P0.3/P0.4/P0.7/P17 ADC检测功能
******************************************************************************/
void main (void)
{
unsigned char i;
P06_Quasi_Mode;
P13_Quasi_Mode;
P14_Quasi_Mode;
P06=1;
P14=0;
P13=0;
//Timer1_Delay10ms(100);
Timer1_Delay10ms(100);//延时2s
/*----电源欠压采样----P07------*/
P06=0;
ADP17 =0x07FF;
for(i=0;i<10;i++)
{
Enable_ADC_AIN2; // P07
set_ADCS;
while(ADCF == 0);//等待采样完毕
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
tempH =(ADCdataH[6]+ADCdataH[7]+ADCdataH[8]+ADCdataH[9]+ADCdataH[5])/5;
tempL =(ADCdataL[5]+ADCdataL[6]+ADCdataL[7]+ADCdataL[8]+ADCdataL[9])/5;
ADP07 =((unsigned int)tempH<<4) + tempL;
if(ADP07<=(ADP17+5))
{
P13=1;
}
else if(ADP07>ADP17)
{
P13=0;
}
Timer1_Delay10ms(10);
/*----电压采样----P03------*/
for(i=0;i<10;i++)
{
Enable_ADC_AIN6; // P03
set_ADCS;
while(ADCF == 0);//等待采样完毕
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
tempH =(ADCdataH[6]+ADCdataH[7]+ADCdataH[8]+ADCdataH[9]+ADCdataH[5])/5;
tempL =(ADCdataL[5]+ADCdataL[6]+ADCdataL[7]+ADCdataL[8]+ADCdataL[9])/5;
ADP03 =((unsigned int)tempH<<4) + tempL;
while(1)
{
/*----电压采样----P04------*/
for(i=0;i<10;i++)
{
Enable_ADC_AIN5; // P04
set_ADCS;
while(ADCF == 0);//等待采样完毕
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
tempH =(ADCdataH[6]+ADCdataH[7]+ADCdataH[8]+ADCdataH[9]+ADCdataH[5])/5;
tempL =(ADCdataL[5]+ADCdataL[6]+ADCdataL[7]+ADCdataL[8]+ADCdataL[9])/5;
ADP04 =((unsigned int)tempH<<4) + tempL;
// ADP03=256;
// ADP04=265;
if(ADP04>(ADP03+20)) //100=0.1V
{
P06=1;
P14=1;
}
else if(ADP04<=ADP03)
{
P14=0;
P06=0;
}
Timer1_Delay10ms(10);
nop;
}
}