Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (2.49 KB)

1
#include "battery.h"
2
#include "analog.h"
3

    
4
//Constants for Battery
5
//Constants determined experimentally 2/9/2007 - James Kong
6
//See Battery8_data.xls for experiment data
7
#define BATTERY_NEXT_READ_PRESCALAR 5
8
#define MAX_LOW_COUNT 5
9

    
10
//global variables
11
int battery_low_count;
12

    
13
/**
14
 * @defgroup battery Battery
15
 * @brief Functions for reading battery voltage.
16
 * 
17
 * Contains functions for checking the current
18
 * voltage of the battery. Include battery.h to
19
 * access these functions.
20
 *
21
 * @{
22
 **/
23

    
24
/**
25
 * Returns the voltage of the battery as an analog8
26
 * reading. 128 is approximately 5 volts. analog_init
27
 * must be called before using this function.
28
 *
29
 * @return the voltage of the battery as an analog8
30
 * reading
31
 *
32
 * @see analog_init, battery, analog8
33
 **/
34
int battery8(void)
35
{
36
  return analog8(BATT_PORT);
37
}
38

    
39
/**
40
 * Returns the voltage of the battery in deciVolts.
41
 * analog_init must be called before using this function.
42
 *
43
 * @return the voltage of the battery in deciVolts.
44
 *
45
 * @see analog_init, battery8
46
 **/
47
int battery(void)
48
{
49
  /* 5 volts is the max, 255 is the max 8bit number , *2 for the divider */
50
        return analog8(BATT_PORT) * 500 >>7;
51
}
52

    
53

    
54
/**
55
 * Checks if the battery is low. analog_init must be called before
56
 * this function can be used. This function waits for several low
57
 * battery readings in a row to avoid false positives.
58
 *
59
 * @return 1 if the battery is low, 0 otherwise
60
 *
61
 * @see analog_init
62
 **/
63
char battery_low(void)
64
{
65
  static char next_read = 0;
66
  static char ret = 0;
67
  
68
  int batt_reading = battery8();
69

    
70
  if(next_read == 0) {
71
    if(batt_reading > BATTERY_LOWV)
72
    {
73
        ret = 0;
74
        battery_low_count = 0;
75
    }
76
    else
77
    {
78
        battery_low_count++;
79
        if( battery_low_count >= MAX_LOW_COUNT )
80
        {
81
            ret = 1;
82
            battery_low_count = 0;
83
        }
84
    }
85
    
86
    next_read = BATTERY_NEXT_READ_PRESCALAR;
87
  }
88
  else next_read--;
89
  
90
  return ret;
91
}
92

    
93
/**
94
 * Averages n_samples battery8 readings. This function may
95
 * take a while to complete, and is only made available
96
 * for convenience. analog_init must be called before this
97
 * function may be used.
98
 *
99
 * @param n_samples The number of times to sample the battery voltage
100
 *
101
 * @return the average reading for the battery voltage, where 128
102
 * is approximately 5 Volts.
103
 *
104
 * @see analog_init, battery
105
 **/
106
int battery8_avg(int n_samples)
107
{
108
  int i;
109
  long int sum = 0;
110
  
111
  for(i = 0; i < n_samples; i++) {
112
    sum += battery8();
113
  }
114
  
115
  return (int)(sum / n_samples);
116
}
117

    
118
/** @} **/ //end defgroup
119