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和P13输出高电平。
当P04<P03时,(P06和P13输出低电平,也可以不管,因为上面默认=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);
clr_ADCF; //这里关闭ADC达到省电
/*----电压采样----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;
clr_ADCF; //这里关闭ADC达到省电看下入这位置对吗
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;
//循环1分钟后当(ADP04<=ADP03)进入掉电模式休眠省电,电压在2V以下
//当(ADP04>ADP03)电压在4-5V 外部中断唤醒休眠
}
}
这样能实现吗?关闭ADC放这位置是正确的不?菜鸟一个多多指教 |