Master Thesis  V1.0
Research and Design of Sensor Node for NMSD Treatment
adc.h File Reference

Read battery voltage. More...

#include "stdint.h"
Include dependency graph for adc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ADC_FREQ   13000000 /* ADC sample frequency */
 

Functions

void initADC (void)
 Initialisation of the ADC. More...
 
void ADC0_IRQHandler (void)
 Interrupt handler for ADC. More...
 
void ADC_Batt_Read (void)
 Do single ADC conversion (read) More...
 
void ADC_Batt_print (void)
 Print battery in millivolts using dbprint. More...
 
void ADC_get_batt (uint8_t *percent)
 Get battery in percentage. More...
 

Variables

volatile uint32_t sample
 
volatile uint32_t millivolts
 

Detailed Description

Read battery voltage.

Version
1.0
Author
Jona Cappelle

Definition in file adc.h.

Macro Definition Documentation

◆ ADC_FREQ

#define ADC_FREQ   13000000 /* ADC sample frequency */

Definition at line 23 of file adc.h.

Function Documentation

◆ ADC0_IRQHandler()

void ADC0_IRQHandler ( void  )

Interrupt handler for ADC.

Conversion from ADC read (0-4096) to millivolts

Definition at line 73 of file adc.c.

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 }

References millivolts, and sample.

◆ ADC_Batt_print()

void ADC_Batt_print ( void  )

Print battery in millivolts using dbprint.

Definition at line 113 of file adc.c.

114 {
115 #if DEBUG_DBPRINT == 1 /* DEBUG_DBPRINT */
116  dbprint("Batt: ");
118 #endif /* DEBUG_DBPRINT */
119 }

References dbprint(), dbprintInt(), and millivolts.

◆ ADC_Batt_Read()

void ADC_Batt_Read ( void  )

Do single ADC conversion (read)

Note
Takes minimal 1 µs

Definition at line 100 of file adc.c.

101 {
102  // Start first conversion
103  ADC_Start(ADC0, adcStartSingle);
104 
105 }

Referenced by ADC_get_batt().

◆ ADC_get_batt()

void ADC_get_batt ( uint8_t *  percent)

Get battery in percentage.

20 - 40 - 60 - 80 - 100 percent

Parameters
[out]percentPercentage of the remaining battery capacity

Definition at line 132 of file adc.c.

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 }

References ADC_Batt_Read(), and millivolts.

Referenced by CheckIMUidle().

◆ initADC()

void initADC ( void  )

Initialisation of the ADC.

Reference: VDD (=2V) Resolution 12bits Channel 4

Definition at line 32 of file adc.c.

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 }

Variable Documentation

◆ millivolts

volatile uint32_t millivolts

Definition at line 26 of file adc.h.

Referenced by ADC0_IRQHandler(), ADC_Batt_print(), and ADC_get_batt().

◆ sample

volatile uint32_t sample

Definition at line 25 of file adc.h.

Referenced by ADC0_IRQHandler().

dbprint
void dbprint(char *message)
dbprintInt
void dbprintInt(int32_t value)
ADC_FREQ
#define ADC_FREQ
Definition: adc.h:23
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