-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.go
executable file
·54 lines (46 loc) · 1.18 KB
/
adc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//go:build pico
package pico
import (
// Namespace imports
. "github.com/djthorpe/go-pico/pkg/sdk"
)
//////////////////////////////////////////////////////////////////////////////
// TYPES
// ADC represents an Analog to Digital Converter. On the RP2040, there are
// four ADC's.
type ADC struct {
Pin Pin
Num uint32
temp bool
}
//////////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// Get returns the raw ADC value, which is the first 12 bits
func (a *ADC) Get() uint16 {
ADC_select_input(a.Num)
for {
if ADC_is_ready() {
break
}
}
return ADC_read()
}
// Return voltage given the value of the reference voltage
func (a *ADC) GetVoltage(vref float32) float32 {
return float32(a.Get()) * vref / float32(1<<12)
}
// Return temperature ReadTemperature does a one-shot sample of the internal
// temperature sensor and returns a celsius reading.
//
// Only works on channel five. Other channels will return 0
func (a *ADC) GetTemperature() float32 {
if a.Num != ADC_temperature_input() {
return 0
}
if a.temp == false {
ADC_set_temp_sensor_enabled(true)
a.temp = true
}
// Section 4.9.5
return 27 - (a.GetVoltage(3.3)-0.706)/0.001721
}