osd-contiki/platform/osd-merkur/dev/batmon.c

56 lines
823 B
C
Raw Normal View History

2013-08-20 13:02:56 +02:00
#include "contiki.h"
#include "batmon.h"
#include <util/delay.h>
int8_t batmon_init()
{
return 0;
}
int8_t batmon_get_voltage(uint16_t* voltage)
{
uint16_t volt = 0;
uint16_t resolution = 75;
uint16_t offset = 2550;
int8_t ctr = 0;
2013-08-20 13:34:01 +02:00
BATMON = 0 | _BV(BATMON_HR);
2013-08-20 13:02:56 +02:00
_delay_us(2);
2013-08-20 13:34:01 +02:00
if(BATMON & _BV(BATMON_OK))
2013-08-20 13:02:56 +02:00
{
// voltage above 2.550 V
resolution = 75;
offset = 2550;
for(ctr=15; ctr>=0; ctr--)
{
BATMON = (BATMON & 0xF0) | (ctr);
_delay_us(2);
2013-08-20 13:34:01 +02:00
if(BATMON & _BV(BATMON_OK)) break;
2013-08-20 13:02:56 +02:00
}
}
else
{
// voltage below 2.550 V
resolution = 50;
offset = 1700;
2013-08-20 13:34:01 +02:00
BATMON &= ~_BV(BATMON_HR);
2013-08-20 13:02:56 +02:00
for(ctr=15; ctr>=0; ctr--)
{
BATMON = (BATMON & 0xF0) | (ctr);
_delay_us(2);
2013-08-20 13:34:01 +02:00
if(BATMON & _BV(BATMON_OK)) break;
2013-08-20 13:02:56 +02:00
}
}
volt = resolution*ctr+offset;
*voltage=volt;
return 0;
}