Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
adc.c
Go to the documentation of this file.
1 /***************************************************************************/
9 /*
10  * adcbatt.c
11  *
12  * Created on: Nov 19, 2019
13  * Author: jonac
14  */
15 
16 
17 #include <adc.h>
18 #include "em_adc.h"
19 #include "em_cmu.h"
20 #include "debug_dbprint.h"
21 
22 /**************************************************************************/
32 void initADC (void)
33 {
34  // Enable ADC0 clock
35  CMU_ClockEnable(cmuClock_ADC0, true);
36 
37  // Declare init structs
38  ADC_Init_TypeDef init = ADC_INIT_DEFAULT;
39  ADC_InitSingle_TypeDef initSingle = ADC_INITSINGLE_DEFAULT;
40 
41  // Modify init structs and initialize
42  init.prescale = ADC_PrescaleCalc(ADC_FREQ, 0); // Init to max ADC clock for Series 0
43  init.timebase = ADC_TimebaseCalc(0);
44 
45  initSingle.diff = false; // single ended
46  //initSingle.reference = adcRef2V5; // internal 2.5V reference
47  initSingle.reference = adcRefVDD; // vdd reference voltage
48  initSingle.resolution = adcRes12Bit; // 12-bit resolution
49 
50  // Select ADC input. See README for corresponding EXP header pin.
51  initSingle.input = adcSingleInputCh4;
52 
53  ADC_Init(ADC0, &init);
54  ADC_InitSingle(ADC0, &initSingle);
55 
56  // Enable ADC Single Conversion Complete interrupt
57  ADC_IntEnable(ADC0, ADC_IEN_SINGLE);
58 
59  // Enable ADC interrupts
60  NVIC_ClearPendingIRQ(ADC0_IRQn);
61  NVIC_EnableIRQ(ADC0_IRQn);
62 }
63 
64 /**************************************************************************/
73 void ADC0_IRQHandler(void)
74 {
75  // Clear the interrupt flag
76  ADC_IntClear(ADC0, ADC_IFC_SINGLE);
77 
78  // Get ADC result
79  sample = ADC_DataSingleGet(ADC0);
80 
81  // Calculate input voltage in mV, input voltage reference: 3.3V (3300)
82  /* 2 V --|500k|--|1Meg|--- GND */
83  millivolts = ( sample * 2000 * 15 ) / (4096 * 5 );
84 
85  // Start next ADC conversion
86  /* This is for continuous measurements, not needed */
87  /* When we want to read the ADC, we just call ADC_Start(ADC0, adcStartSingle) */
88  //ADC_Start(ADC0, adcStartSingle);
89 }
90 
91 /**************************************************************************/
100 void ADC_Batt_Read(void)
101 {
102  // Start first conversion
103  ADC_Start(ADC0, adcStartSingle);
104 
105 }
106 
107 /**************************************************************************/
113 void ADC_Batt_print(void)
114 {
115 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
116  dbprint("Batt: ");
118 #endif /* DEBUG_DBPRINT */
119 }
120 
121 /**************************************************************************/
132 void ADC_get_batt( uint8_t *percent )
133 {
134  ADC_Batt_Read();
135 
136  if(millivolts <= 3670) percent[0] = 20;
137  else if(millivolts <= 3720) percent[0] = 40;
138  else if(millivolts <= 3800) percent[0] = 60;
139  else if(millivolts <= 3950) percent[0] = 80;
140  else percent[0] = 100;
141 
142 }
143 
ADC_Batt_print
void ADC_Batt_print(void)
Print battery in millivolts using dbprint.
Definition: adc.c:113
dbprint
void dbprint(char *message)
adc.h
Read battery voltage.
dbprintInt
void dbprintInt(int32_t value)
ADC0_IRQHandler
void ADC0_IRQHandler(void)
Interrupt handler for ADC.
Definition: adc.c:73
ADC_FREQ
#define ADC_FREQ
Definition: adc.h:23
initADC
void initADC(void)
Initialisation of the ADC.
Definition: adc.c:32
millivolts
volatile uint32_t millivolts
Definition: adc.h:26
sample
volatile uint32_t sample
Definition: adc.h:25
ADC_Batt_Read
void ADC_Batt_Read(void)
Do single ADC conversion (read)
Definition: adc.c:100
ADC_get_batt
void ADC_get_batt(uint8_t *percent)
Get battery in percentage.
Definition: adc.c:132
debug_dbprint.h
Enable or disable printing to UART with dbprint.