From a13dd6b17ab0f57e28957c892ddb557bebe1ecb6 Mon Sep 17 00:00:00 2001 From: microbuilder Date: Mon, 27 Feb 2017 11:39:48 +0100 Subject: [PATCH 1/4] Simple ADC example --- .../Hardware/adc/adc.ino | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 libraries/AdafruitFeather52Examples/Hardware/adc/adc.ino diff --git a/libraries/AdafruitFeather52Examples/Hardware/adc/adc.ino b/libraries/AdafruitFeather52Examples/Hardware/adc/adc.ino new file mode 100644 index 00000000..8041a68f --- /dev/null +++ b/libraries/AdafruitFeather52Examples/Hardware/adc/adc.ino @@ -0,0 +1,20 @@ +int adcin = A5; +int adcvalue = 0; +float mv_per_lsb = 3600.0F/1024.0F; // 10-bit ADC with 3.6V input range + +void setup() { + Serial.begin(115200); +} + +void loop() { + // Get a fresh ADC value + adcvalue = analogRead(adcin); + + // Display the results + Serial.print(adcvalue); + Serial.print(" ["); + Serial.print((float)adcvalue * mv_per_lsb); + Serial.println(" mV]"); + + delay(100); +} From 5217449e8379d50ff28d13b0c464104bef4db099 Mon Sep 17 00:00:00 2001 From: microbuilder Date: Mon, 27 Feb 2017 12:24:28 +0100 Subject: [PATCH 2/4] Added VBAT ADC example --- .../Hardware/adc_vbat/adc_vbat.ino | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino diff --git a/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino b/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino new file mode 100644 index 00000000..364439d6 --- /dev/null +++ b/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino @@ -0,0 +1,44 @@ +#define VBAT_PIN (A7) +#define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096 +#define VBAT_DIVIDER (0.71275837F) // 2M + 0.806M voltage divider on VBAT = (2M / (0.806M + 2M)) +#define VBAT_DIVIDER_COMP (1.403F) // Compensation factor for the VBAT divider + +float readVBAT(void) { + float raw; + + // Set the analog reference to 3.0V (default = 3.6V) + analogReference(AR_INTERNAL_3_0); + + // Set the resolution to 12-bit (0..4095) + analogReadResolution(12); // Can be 8, 10, 12 or 14 + + // Let the ADC settle + delay(1); + + // Get the raw 12-bit, 0..3000mV ADC value + raw = analogRead(VBAT_PIN); + + // Set the ADC back to the default settings + analogReference(AR_INTERNAL); + analogReadResolution(10); + + // Convert the raw value to mv + // ADC range is 0..3000mV and resolution is 12-bit (0..4095) + // VBAT voltage divider is 2M + 0.806M, which needs to be added back + return raw * VBAT_MV_PER_LSB * VBAT_DIVIDER_COMP; +} + +void setup() { + Serial.begin(115200); +} + +void loop() { + // Get a fresh ADC value + float vbat_val = readVBAT(); + + // Display the results + Serial.print(vbat_val); + Serial.println(" mV"); + + delay(1000); +} From 1399cdf794da4517a1ab56c27adfa7bab199f6f4 Mon Sep 17 00:00:00 2001 From: microbuilder Date: Mon, 27 Feb 2017 12:25:06 +0100 Subject: [PATCH 3/4] Additional ADC options --- cores/nRF5/wiring_analog.h | 8 ++++++-- cores/nRF5/wiring_analog_nRF52.c | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cores/nRF5/wiring_analog.h b/cores/nRF5/wiring_analog.h index 4789c7c6..aca0cf94 100644 --- a/cores/nRF5/wiring_analog.h +++ b/cores/nRF5/wiring_analog.h @@ -32,8 +32,12 @@ extern "C" { typedef enum _eAnalogReference { AR_DEFAULT, - AR_INTERNAL, - AR_VDD4 + AR_INTERNAL, // 0.6V Ref * 6 = 0..3.6V + AR_INTERNAL_3_0, // 0.6V Ref * 5 = 0..3.0V + AR_INTERNAL_2_4, // 0.6V Ref * 4 = 0..2.4V + AR_INTERNAL_1_8, // 0.6V Ref * 3 = 0..1.8V + AR_INTERNAL_1_2, // 0.6V Ref * 2 = 0..1.6V + AR_VDD4 // VDD/4 REF * 4 = 0..VDD } eAnalogReference ; #else typedef enum _eAnalogReference diff --git a/cores/nRF5/wiring_analog_nRF52.c b/cores/nRF5/wiring_analog_nRF52.c index 048992d7..9051d326 100644 --- a/cores/nRF5/wiring_analog_nRF52.c +++ b/cores/nRF5/wiring_analog_nRF52.c @@ -90,6 +90,26 @@ static inline uint32_t mapResolution( uint32_t value, uint32_t from, uint32_t to void analogReference( eAnalogReference ulMode ) { switch ( ulMode ) { + case AR_VDD4: + saadcReference = SAADC_CH_CONFIG_REFSEL_VDD1_4; + saadcGain = SAADC_CH_CONFIG_GAIN_Gain4; + break; + case AR_INTERNAL_3_0: + saadcReference = SAADC_CH_CONFIG_REFSEL_Internal; + saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_5; + break; + case AR_INTERNAL_2_4: + saadcReference = SAADC_CH_CONFIG_REFSEL_Internal; + saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_4; + break; + case AR_INTERNAL_1_8: + saadcReference = SAADC_CH_CONFIG_REFSEL_Internal; + saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_3; + break; + case AR_INTERNAL_1_2: + saadcReference = SAADC_CH_CONFIG_REFSEL_Internal; + saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_2; + break; case AR_DEFAULT: case AR_INTERNAL: default: @@ -97,10 +117,6 @@ void analogReference( eAnalogReference ulMode ) saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_6; break; - case AR_VDD4: - saadcReference = SAADC_CH_CONFIG_REFSEL_VDD1_4; - saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_4; - break; } } From 017c29acb425912e88da2a14dc6829a154f19fe9 Mon Sep 17 00:00:00 2001 From: microbuilder Date: Mon, 27 Feb 2017 12:28:13 +0100 Subject: [PATCH 4/4] Cleanup --- .../AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino b/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino index 364439d6..98f6c752 100644 --- a/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino +++ b/libraries/AdafruitFeather52Examples/Hardware/adc_vbat/adc_vbat.ino @@ -19,7 +19,7 @@ float readVBAT(void) { raw = analogRead(VBAT_PIN); // Set the ADC back to the default settings - analogReference(AR_INTERNAL); + analogReference(AR_DEFAULT); analogReadResolution(10); // Convert the raw value to mv