mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 19:43:44 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 008dd7dfe6 |
@@ -14,7 +14,7 @@ func main() {
|
|||||||
led := machine.LED
|
led := machine.LED
|
||||||
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
|
|
||||||
sensor := machine.ADC{machine.ADC2}
|
sensor := machine.ADC{Pin: machine.ADC2}
|
||||||
sensor.Configure(machine.ADCConfig{})
|
sensor.Configure(machine.ADCConfig{})
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
@@ -9,4 +9,11 @@ type ADCConfig struct {
|
|||||||
Reference uint32 // analog reference voltage (AREF) in millivolts
|
Reference uint32 // analog reference voltage (AREF) in millivolts
|
||||||
Resolution uint32 // number of bits for a single conversion (e.g., 8, 10, 12)
|
Resolution uint32 // number of bits for a single conversion (e.g., 8, 10, 12)
|
||||||
Samples uint32 // number of samples for a single conversion (e.g., 4, 8, 16, 32)
|
Samples uint32 // number of samples for a single conversion (e.g., 4, 8, 16, 32)
|
||||||
|
Bus uint8 // bus Number of ADC
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
AdcBusAuto = 0
|
||||||
|
AdcBus0 = 1
|
||||||
|
AdcBus1 = 2
|
||||||
|
)
|
||||||
|
|||||||
@@ -44,4 +44,5 @@ func (p Pin) Low() {
|
|||||||
|
|
||||||
type ADC struct {
|
type ADC struct {
|
||||||
Pin Pin
|
Pin Pin
|
||||||
|
Bus uint8
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -813,6 +813,18 @@ func (a ADC) Configure(config ADCConfig) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
a.Pin.Configure(PinConfig{Mode: PinAnalog})
|
a.Pin.Configure(PinConfig{Mode: PinAnalog})
|
||||||
|
|
||||||
|
switch config.Bus {
|
||||||
|
case AdcBusAuto:
|
||||||
|
bus := a.getADCBus()
|
||||||
|
if bus == sam.ADC0 {
|
||||||
|
a.Bus = 0
|
||||||
|
} else {
|
||||||
|
a.Bus = 1
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
a.Bus = config.Bus
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the current value of a ADC pin, in the range 0..0xffff.
|
// Get returns the current value of a ADC pin, in the range 0..0xffff.
|
||||||
@@ -876,6 +888,12 @@ func (a ADC) Get() uint16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a ADC) getADCBus() *sam.ADC_Type {
|
func (a ADC) getADCBus() *sam.ADC_Type {
|
||||||
|
if a.Bus == AdcBus0 {
|
||||||
|
return sam.ADC0
|
||||||
|
} else if a.Bus == AdcBus1 {
|
||||||
|
return sam.ADC1
|
||||||
|
}
|
||||||
|
|
||||||
if (a.Pin >= PB04 && a.Pin <= PB07) || (a.Pin >= PC00) {
|
if (a.Pin >= PB04 && a.Pin <= PB07) || (a.Pin >= PC00) {
|
||||||
return sam.ADC1
|
return sam.ADC1
|
||||||
}
|
}
|
||||||
@@ -887,9 +905,17 @@ func (a ADC) getADCChannel() uint8 {
|
|||||||
case PA02:
|
case PA02:
|
||||||
return 0
|
return 0
|
||||||
case PB08:
|
case PB08:
|
||||||
return 2
|
if a.Bus == AdcBus1 {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return 2
|
||||||
|
}
|
||||||
case PB09:
|
case PB09:
|
||||||
return 3
|
if a.Bus == AdcBus1 {
|
||||||
|
return 1
|
||||||
|
} else {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
case PA04:
|
case PA04:
|
||||||
return 4
|
return 4
|
||||||
case PA05:
|
case PA05:
|
||||||
|
|||||||
Reference in New Issue
Block a user