53 #include "em_device.h" 64 #define DBPRINT_TIMEOUT 0 67 #define TIMEOUT_CONVERSION 70 // Before: 50 72 ADC_Init_TypeDef
init = ADC_INIT_DEFAULT;
73 ADC_InitSingle_TypeDef
initSingle = ADC_INITSINGLE_DEFAULT;
90 CMU_ClockEnable(cmuClock_HFPER,
true);
91 CMU_ClockEnable(cmuClock_ADC0,
true);
95 init.timebase = ADC_TimebaseCalc(0);
99 init.prescale = ADC_PrescaleCalc(400000, 0);
102 ADC_Init(ADC0, &
init);
112 else if (peripheral ==
CH1)
117 else if (peripheral ==
CH2)
122 else if (peripheral ==
CH3)
127 else if (peripheral ==
CH4)
135 #if DEBUG_DBPRINT == 1 136 dbcrit(
"Unknown ADC peripheral selected!");
140 CMU_ClockEnable(cmuClock_ADC0,
false);
156 ADC_IntEnable(ADC0, ADC_IEN_SINGLE);
157 NVIC_ClearPendingIRQ(ADC0_IRQn);
158 NVIC_EnableIRQ(ADC0_IRQn);
161 CMU_ClockEnable(cmuClock_ADC0,
false);
163 #if DEBUG_DBPRINT == 1 166 else if (peripheral ==
CH1)
dbinfo(
"ADC0 initialized for CH1");
167 else if (peripheral ==
CH2)
dbinfo(
"ADC0 initialized for CH2");
168 else if (peripheral ==
CH3)
dbinfo(
"ADC0 initialized for CH3");
169 else if (peripheral ==
CH4)
dbinfo(
"ADC0 initialized for CH4");
195 CMU_ClockEnable(cmuClock_ADC0,
true);
216 else if (peripheral ==
CH1)
225 else if (peripheral ==
CH2)
234 else if (peripheral ==
CH3)
243 else if (peripheral ==
CH4)
255 #if DEBUG_DBPRINT == 1 256 dbcrit(
"Unknown ADC peripheral selected!");
260 CMU_ClockEnable(cmuClock_ADC0,
false);
272 ADC_Start(ADC0, adcStartSingle);
281 #if DEBUG_DBPRINT == 1 282 dbcrit(
"Waiting time for ADC conversion reached!");
286 CMU_ClockEnable(cmuClock_ADC0,
false);
293 #if DBPRINT_TIMEOUT == 1 297 #if DEBUG_DBPRINT == 1 298 dbwarnInt(
"ADC conversion (", counter,
")");
305 value = ADC_DataSingleGet(ADC0);
310 CMU_ClockEnable(cmuClock_ADC0,
false);
321 float32_t fv = (value * 3.75) / 4.096;
324 else if (peripheral ==
CH1 || peripheral ==
CH2 || peripheral ==
CH3 || peripheral ==
CH4)
329 float32_t fv = (value * 2.5) / 409.6;
356 int32_t cal_temp_0 = ((DEVINFO->CAL & _DEVINFO_CAL_TEMP_MASK)
357 >> _DEVINFO_CAL_TEMP_SHIFT);
360 int32_t cal_value_0 = ((DEVINFO->ADC0CAL2 & _DEVINFO_ADC0CAL2_TEMP1V25_MASK)
361 >> _DEVINFO_ADC0CAL2_TEMP1V25_SHIFT);
364 float32_t t_grad = -6.27;
366 temp = (cal_temp_0 - ((cal_value_0 - adcSample) / t_grad));
382 ADC_IntClear(ADC0, flags);
void dbprintln(char *message)
Print a string (char array) to USARTx and go to the next line.
ADC_InitSingle_TypeDef initSingle
void ADC0_IRQHandler(void)
Interrupt Service Routine for ADC0.
void dbwarnInt(char *message1, int32_t value, char *message2)
Print a warning value surrounded by two strings (char array) to USARTx.
ADC functionality for reading the (battery) voltage and internal temperature.
#define TIMEOUT_CONVERSION
void dbinfo(char *message)
Print an info string (char array) to USARTx and go to the next line.
int32_t readADC(ADC_Measurement_t peripheral)
Method to read the battery voltage or internal temperature.
static float32_t convertToCelsius(int32_t adcSample)
Method to convert an ADC value to a temperature value.
void error(uint8_t number)
Error method.
volatile bool adcConversionComplete
int int32_t
Typedef defining 32 bit int type. The developer should modify this to suit the platform being deploye...
Enable or disable printing to UART with dbprint.
void initADC(ADC_Measurement_t peripheral)
Method to initialize the ADC to later check the battery voltage or internal temperature.
void dbcrit(char *message)
Print a critical error string (char array) in red to USARTx and go to the next line.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type. The developer should modify this to suit the platform be...
enum adc_measurements ADC_Measurement_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type. The developer should modify this to suit the platform bein...