Skip to content

Commit f51faa7

Browse files
authored
Add the ability to change SAADC acquisition time
Provide the user with the ability to change the SAADC acquisition time to accommodate higher source resistance signals.
1 parent eb3a6d6 commit f51faa7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

cores/nRF5/wiring_analog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ extern void analogReadResolution(int res);
107107
*/
108108
extern void analogWriteResolution(uint8_t res);
109109

110+
/*
111+
* \brief Set the ADC sample time. Default is 3us (appropriate for low source resistance).
112+
* See the chart of appropriate sample time vs. source resistance in the SAADC section
113+
* of the Nordic nrf52 product specification.
114+
*
115+
* \param time Should be set to 3, 5, 10, 15, 20 or 40.
116+
*/
117+
extern void analogSampleTime(uint8_t sTime);
118+
110119
extern void analogOutputInit( void ) ;
111120

112121
#ifdef __cplusplus

cores/nRF5/wiring_analog_nRF52.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919

2020
#if defined(NRF52) || defined(NRF52_SERIES)
2121

22-
#include "nrf.h"
22+
#include <nrf.h>
2323

24-
#include "Arduino.h"
25-
#include "wiring_private.h"
24+
#include <Arduino.h>
25+
#include <wiring_private.h>
2626

2727
#ifdef __cplusplus
2828
extern "C" {
2929
#endif
3030

3131
static uint32_t saadcReference = SAADC_CH_CONFIG_REFSEL_Internal;
3232
static uint32_t saadcGain = SAADC_CH_CONFIG_GAIN_Gain1_6;
33+
static uint32_t saadcSampleTime = SAADC_CH_CONFIG_TACQ_3us;
3334

3435
static bool saadcBurst = SAADC_CH_CONFIG_BURST_Disabled;
3536

@@ -140,6 +141,28 @@ void analogOversampling( uint32_t ulOversampling )
140141
}
141142
}
142143

144+
void analogSampleTime( uint8_t sTime)
145+
{
146+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_3us; // default is 3 us
147+
switch (sTime) {
148+
case 5:
149+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_5us;
150+
break;
151+
case 10:
152+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_10us;
153+
break;
154+
case 15:
155+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_15us;
156+
break;
157+
case 20:
158+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_20us;
159+
break;
160+
case 40:
161+
saadcSampleTime = SAADC_CH_CONFIG_TACQ_40us;
162+
break;
163+
}
164+
}
165+
143166
static uint32_t analogRead_internal( uint32_t psel )
144167
{
145168
uint32_t saadcResolution;
@@ -171,7 +194,7 @@ static uint32_t analogRead_internal( uint32_t psel )
171194
| ((SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESN_Pos) & SAADC_CH_CONFIG_RESN_Msk)
172195
| ((saadcGain << SAADC_CH_CONFIG_GAIN_Pos) & SAADC_CH_CONFIG_GAIN_Msk)
173196
| ((saadcReference << SAADC_CH_CONFIG_REFSEL_Pos) & SAADC_CH_CONFIG_REFSEL_Msk)
174-
| ((SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
197+
| ((saadcSampleTime << SAADC_CH_CONFIG_TACQ_Pos) & SAADC_CH_CONFIG_TACQ_Msk)
175198
| ((SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) & SAADC_CH_CONFIG_MODE_Msk)
176199
| ((saadcBurst << SAADC_CH_CONFIG_BURST_Pos) & SAADC_CH_CONFIG_BURST_Msk);
177200
NRF_SAADC->CH[0].PSELN = psel;

0 commit comments

Comments
 (0)