MAXREFDES70# Code Documentation  V02.00
High-precision long battery life heat/flow meter
 All Data Structures Files Functions Variables Enumerator Macros Pages
batteryLevel.c
Go to the documentation of this file.
1 
2 
46 # include "batteryLevel.h"
47 
48 #include "em_chip.h"
49 #include "em_device.h"
50 #include "em_cmu.h"
51 #include "em_system.h"
52 #include "em_vcmp.h"
53 
54 /***************************************************************************/
59 {
60  /* Reenable VCMP to trigger a new warm-up cycle */
61  VCMP_Disable();
62  VCMP_Enable();
63 
64  /* Wait for VCMP warm-up */
65  while (!VCMP_Ready()) ;
66 }
67 
68 /***************************************************************************/
76 int batteryLevel(void)
77 {
78  /* Declare VCMP Init struct */
79  VCMP_Init_TypeDef vcmp =
80  {
81  false, /* Half bias current */
82  7, /* Bias current configuration */
83  false, /* Enable interrupt for falling edge */
84  false, /* Enable interrupt for rising edge */
85  vcmpWarmTime256Cycles, /* Warm-up time in clock cycles */
86  vcmpHystNone, /* Hysteresis configuration */
87  0, /* Inactive comparator output value */
88  false, /* Enable low power mode */
89  VCMP_VoltageToLevel(3.3), /* Trigger level */
90  true /* Enable VCMP after configuration */
91  };
92 
93  /* Initialize VCMP */
94  CMU_ClockEnable(cmuClock_VCMP, true);
95  VCMP_Init(&vcmp);
96 
97  /* Check from VDD = 2.4V (TRIGLEVEL = 22), to 3.6V (TRIGLEVEL = 58) */
98  int batteryLevel = 22;
99  for (batteryLevel = 22; batteryLevel < 58; batteryLevel ++)
100  {
101  /* Set VCMP trigger and check if VDD < VCMP trigger */
102  VCMP_TriggerSet(batteryLevel);
104  if(!VCMP_VDDHigher())
105  break;
106 
107  }
108 
109  VCMP_Disable();
110  return batteryLevel;
111 }