@@ -49,19 +49,31 @@ func CPUFrequency() uint32 {
49
49
50
50
// InitADC initializes the registers needed for ADC.
51
51
func InitADC () {
52
- return // no specific setup on nrf52 machine.
53
- }
54
-
55
- // Configure configures an ADC pin to be able to read analog data.
56
- func (a ADC ) Configure (config ADCConfig ) {
57
52
// Enable ADC.
58
- // The ADC does not consume a noticeable amount of current simply by being
59
- // enabled.
53
+ // The ADC does not consume a noticeable amount of current by being enabled.
60
54
nrf .SAADC .ENABLE .Set (nrf .SAADC_ENABLE_ENABLE_Enabled << nrf .SAADC_ENABLE_ENABLE_Pos )
55
+ }
61
56
62
- // Use fixed resolution of 12 bits.
63
- // TODO: is it useful for users to change this?
64
- nrf .SAADC .RESOLUTION .Set (nrf .SAADC_RESOLUTION_VAL_12bit )
57
+ // Configure configures an ADC pin to be able to read analog data.
58
+ // Reference voltage can be 150, 300, 600, 1200, 1800, 2400, 3000(default), 3600 mV
59
+ // Resolution can be 8, 10, 12(default), 14 bits
60
+ // SampleTime will be ceiled to 3(default), 5, 10, 15, 20 or 40(max) µS respectively
61
+ // Samples can be 1(default), 2, 4, 8, 16, 32, 64, 128, 256 samples
62
+ func (a * ADC ) Configure (config ADCConfig ) {
63
+ var resolution uint32
64
+ switch config .Resolution {
65
+ case 8 :
66
+ resolution = SAADC_RESOLUTION_VAL_8bit
67
+ case 10 :
68
+ resolution = SAADC_RESOLUTION_VAL_10bit
69
+ case 12 :
70
+ resolution = SAADC_RESOLUTION_VAL_12bit
71
+ case 14 :
72
+ resolution = SAADC_RESOLUTION_VAL_14bit
73
+ default :
74
+ resolution = SAADC_RESOLUTION_VAL_12bit
75
+ }
76
+ nrf .SAADC .RESOLUTION .Set (resolution )
65
77
66
78
var configVal uint32 = nrf .SAADC_CH_CONFIG_RESP_Bypass << nrf .SAADC_CH_CONFIG_RESP_Pos |
67
79
nrf .SAADC_CH_CONFIG_RESP_Bypass << nrf .SAADC_CH_CONFIG_RESN_Pos |
@@ -86,7 +98,7 @@ func (a ADC) Configure(config ADCConfig) {
86
98
case 3600 : // 3.6V
87
99
configVal |= nrf .SAADC_CH_CONFIG_GAIN_Gain1_6 << nrf .SAADC_CH_CONFIG_GAIN_Pos
88
100
default :
89
- // TODO: return an error
101
+ // TODO: return an error, will that interfere with any interfaced if one will be?
90
102
}
91
103
92
104
// Source resistance, according to table 41 on page 676 of the nrf52832 datasheet.
@@ -138,7 +150,7 @@ func (a ADC) Configure(config ADCConfig) {
138
150
}
139
151
140
152
// Get returns the current value of a ADC pin in the range 0..0xffff.
141
- func (a ADC ) Get () uint16 {
153
+ func (a * ADC ) Get () uint16 {
142
154
var pwmPin uint32
143
155
var rawValue volatile.Register16
144
156
0 commit comments