📄 battmon.c
字号:
// Battery monitor BattMon.c // Rev 9/1/05
//Copyright (C) 2005 Alex Brown rbirac@cox.net
//This program is free software; See license at the end of this file for details.
/*
Calculates battery status. Right now it just reports the raw battery
voltage. The voltage is filtered to ignore spikes due to large loads.
Maybe do something fancier someday.
Returns voltage in millivolts from 9500 to 14500 (9.5 to 14.5 volts)
Calibration: read BattFilt to find range of voltage. This gave 9.5 volts
at 1952 counts and increased at 11666 per volt. So, the calibration
subtracts the 1952 from BattFilt then multiplies by 1/11.666 (30/350) and
adds the 9500 mv back in. This will probably require recalibration for
other controller boards.
*/
#include <hcs12dp256.h> //for registers
#include <stdio.h> //for i/o prototypes
int BattVoltage; //battery voltage in millivolts
int BattMon()
{ static long BattFilt;
int temp,temp1;
temp = ATD1DR2; //read raw battery voltage ATD value
BattFilt = (BattFilt * 60)/61 + temp; //a 1 second filter scaled *61
temp1 = (int)((BattFilt-1952)*30/350 + 9500); //calibrate to millivolts
if (temp1 > 14500) temp1 = 14500; //limit to range 9500 to 14500
if (temp1 < 9500) temp1 = 9500;
BattVoltage = temp1;
return temp1;
}
// OPEN SOURCE SOFTWARE LICENSE
/* Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -