Statistics
| Revision:

## root / trunk / code / lib / src / libdragonfly / battery.c @ 7

 1 ```#include "battery.h" ``` ```#include "analog.h" ``` ```//Constants for Battery ``` ```//Constants determined experimentally 2/9/2007 - James Kong ``` ```//See Battery8_data.xls for experiment data ``` ```#define BATTERY_NEXT_READ_PRESCALAR 5 ``` ```#define MAX_LOW_COUNT 5 ``` ```//global variables ``` ```int battery_low_count; ``` ```/** ``` ``` * @defgroup battery Battery ``` ``` * @brief Functions for reading battery voltage. ``` ``` * ``` ``` * Contains functions for checking the current ``` ``` * voltage of the battery. Include battery.h to ``` ``` * access these functions. ``` ``` * ``` ``` * @{ ``` ``` **/ ``` ```/** ``` ``` * Returns the voltage of the battery as an analog8 ``` ``` * reading. 128 is approximately 5 volts. analog_init ``` ``` * must be called before using this function. ``` ``` * ``` ``` * @return the voltage of the battery as an analog8 ``` ``` * reading ``` ``` * ``` ``` * @see analog_init, battery, analog8 ``` ``` **/ ``` ```int battery8(void) ``` ```{ ``` ``` return analog8(BATT_PORT); ``` ```} ``` ```/** ``` ``` * Returns the voltage of the battery in deciVolts. ``` ``` * analog_init must be called before using this function. ``` ``` * ``` ``` * @return the voltage of the battery in deciVolts. ``` ``` * ``` ``` * @see analog_init, battery8 ``` ``` **/ ``` ```int battery(void) ``` ```{ ``` ``` /* 5 volts is the max, 255 is the max 8bit number , *2 for the divider */ ``` ``` return analog8(BATT_PORT) * 500 >>7; ``` ```} ``` ```/** ``` ``` * Checks if the battery is low. analog_init must be called before ``` ``` * this function can be used. This function waits for several low ``` ``` * battery readings in a row to avoid false positives. ``` ``` * ``` ``` * @return 1 if the battery is low, 0 otherwise ``` ``` * ``` ``` * @see analog_init ``` ``` **/ ``` ```char battery_low(void) ``` ```{ ``` ``` static char next_read = 0; ``` ``` static char ret = 0; ``` ``` ``` ``` int batt_reading = battery8(); ``` ``` if(next_read == 0) { ``` ``` if(batt_reading > BATTERY_LOWV) ``` ``` { ``` ``` ret = 0; ``` ``` battery_low_count = 0; ``` ``` } ``` ``` else ``` ``` { ``` ``` battery_low_count++; ``` ``` if( battery_low_count >= MAX_LOW_COUNT ) ``` ``` { ``` ``` ret = 1; ``` ``` battery_low_count = 0; ``` ``` } ``` ``` } ``` ``` ``` ``` next_read = BATTERY_NEXT_READ_PRESCALAR; ``` ``` } ``` ``` else next_read--; ``` ``` ``` ``` return ret; ``` ```} ``` ```/** ``` ``` * Averages n_samples battery8 readings. This function may ``` ``` * take a while to complete, and is only made available ``` ``` * for convenience. analog_init must be called before this ``` ``` * function may be used. ``` ``` * ``` ``` * @param n_samples The number of times to sample the battery voltage ``` ``` * ``` ``` * @return the average reading for the battery voltage, where 128 ``` ``` * is approximately 5 Volts. ``` ``` * ``` ``` * @see analog_init, battery ``` ``` **/ ``` ```int battery8_avg(int n_samples) ``` ```{ ``` ``` int i; ``` ``` long int sum = 0; ``` ``` ``` ``` for(i = 0; i < n_samples; i++) { ``` ``` sum += battery8(); ``` ``` } ``` ``` ``` ``` return (int)(sum / n_samples); ``` ```} ``` ```/** @} **/ //end defgroup ```