ADC使用内部1.5V参考源,P2.0作为电池电压检测口。外部使用3M:1M分压电路。电压门限选定为2.45V和2.75V,对应的ADC的值为418和468.
ADC电压检测函数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| u16 hal_ADC_measure(void)
{
//-----------开ADC电压测量-------------------------------------------------
// ,开启ADC10,允许中断,开启内部参考,V(REF+)=1.5v,V(R+) = V(REF+) and V(R-) = V(SS)
// P2DIR &= ~BIT0; //P2.0 作为输入,要在开启ADC之前配置
// delay_10us(100);
// delay2ms();delay2ms();delay2ms();
ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
ADC10AE0 |= 0x01; // P2.0 模拟信号输入使能
ADC10CTL0 &= ~ENC; //ENC=0,修改A/D控制寄存器
ADC10CTL0 |= ENC + ADC10SC + ADC10ON; //启动AD转换器
//延时,等待ADC转换完成
// Set_TimeA1_ON(10);
// __bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit等待AD转换完成
// Set_TimeA1_OFF();
delay_10us(30); //300us延时
//-----------------关闭ADC模块及IO处理---------------------------------------------------
ADC10CTL0 &= 0XFFFD; //ENC=0,修改A/D控制寄存器
ADC10CTL0 &= 0x0000;
ADC10AE0 &= 0xfe; // P2.0 模拟信号输入禁止
// P2DIR |= BIT0; //P2.0 设置为输出
|
电压检测函数为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| void app_battary_voltage_detect(u16 CallTimes)
{
static u8 TimeCnt = 0; //检测时间计数
static u8 VolThr275Cnt = 0; //2.75v门限计数器,低于该值计数
static u8 VolThr245Cnt = 0; //2.45v门限计数器,低于该值计数
u16 VoltageValue = 0;
TimeCnt++;
// if(TimeCnt >= CallTimes)
// {
// TimeCnt = 0;
VoltageValue = hal_ADC_measure(); //读取电压值
#ifdef DEBUG_PRINTF
hal_uart_printf(&quotowerDect: ",11);
hal_uart_printu16(VoltageValue);
hal_uart_printf("\r\n",2);
#endif
if(VoltageValue <= V245_VREF_V15) //电压低于2.45v
{
if (VolThr245Cnt < TIMES_CNT_BATTERY_LOW)
{
VolThr245Cnt++;
}
VolThr275Cnt = 0;
}
else if(VoltageValue <= V275_VREF_V15)//电压低于2.75v
{
if (VolThr275Cnt < TIMES_CNT_BATTERY_LOW) { VolThr275Cnt++; } VolThr245Cnt = 0; } else //电压高于2.75v { VolThr245Cnt = 0; VolThr275Cnt = 0; } if(VolThr245Cnt >= TIMES_CNT_BATTERY_LOW) //连续5次低于2.45v,200s*5 = 1000s
{
g_uBatteryState = BATTERY_LOW;
}
else if (VolThr275Cnt >= TIMES_CNT_BATTERY_LOW)//连续5次低于2.75v
{
g_uBatteryState = BATTERY_WARNING;
}
else
{
g_uBatteryState = BATTERY_OK;
}
// }
}
|
|