Contiki 2.6

battery-monitor.c

00001 #include "contiki.h"
00002 #include "dev/battery-sensor.h"
00003 #include "lib/sensors.h"
00004 #include <stdio.h> /* For printf() */
00005 /*---------------------------------------------------------------------------*/
00006 PROCESS(battery_monitor_process, "Battery Voltage Monitor");
00007 AUTOSTART_PROCESSES(&battery_monitor_process);
00008 /*---------------------------------------------------------------------------*/
00009 PROCESS_THREAD(battery_monitor_process, ev, data)
00010 {
00011   static struct etimer et;
00012 
00013   PROCESS_BEGIN();
00014 
00015   SENSORS_ACTIVATE(battery_sensor);
00016 
00017   while(1) {
00018 
00019     etimer_set(&et, CLOCK_SECOND * 2);
00020     PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
00021     /*
00022      * Battery voltage calculation formula
00023      *
00024      *     V(Battery Voltage) = v(Voltage Reference) * 1024 / ADC
00025      *
00026      *     Where:
00027      *          v = 1.223
00028      * 
00029      */
00030     printf("ADC value : %d\n", battery_sensor.value(0));
00031   }
00032   PROCESS_END();
00033 }
00034 /*---------------------------------------------------------------------------*/