Debian Codegraph @

examples/temp_sensor/src/AdcTemperatureSensor.c

raw symbols SCIP
1 #include "Types.h"
2 #include "AdcTemperatureSensor.h"
3
4 static inline uint32 ConvertAdcCountsToPicovolts(uint32 counts);
5 static inline uint16 ConvertPicovoltsToMillivolts(uint32 picovolts);
6
7 //
8 // PUBLIC METHODS
9 //
10
11 void Adc_StartTemperatureSensorConversion(void)
12 {
13 AT91C_BASE_ADC->ADC_CR = AT91C_ADC_START;
14 }
15
16 bool Adc_TemperatureSensorSampleReady(void)
17 {
18 return ((AT91C_BASE_ADC->ADC_SR & AT91C_ADC_EOC4) == AT91C_ADC_EOC4);
19 }
20
21 uint16 Adc_ReadTemperatureSensor(void)
22 {
23 uint32 picovolts = ConvertAdcCountsToPicovolts(AT91C_BASE_ADC->ADC_CDR4);
24 return ConvertPicovoltsToMillivolts(picovolts);
25 }
26
27 //
28 // PRIVATE HELPERS
29 //
30
31 static inline uint32 ConvertAdcCountsToPicovolts(uint32 counts)
32 {
33 // ADC bit weight at 10-bit resolution with 3.0V reference = 2.9296875 mV/LSB
34 uint32 picovoltsPerAdcCount = 2929688;
35
36 // Shift decimal point by 6 places to preserve accuracy in fixed-point math
37 return counts * picovoltsPerAdcCount;
38 }
39
40 static inline uint16 ConvertPicovoltsToMillivolts(uint32 picovolts)
41 {
42 const uint32 halfMillivoltInPicovolts = 500000;
43 const uint32 picovoltsPerMillivolt = 1000000;
44
45 // Add 0.5 mV to result so that truncation yields properly rounded result
46 picovolts += halfMillivoltInPicovolts;
47
48 // Divide appropriately to convert to millivolts
49 return (uint16)(picovolts / picovoltsPerMillivolt);
50 }
51