-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pcf8591: add ADC only implementation for I2C ADC/DAC (#690)
Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
1bf1a11
commit ee3842f
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Connects to a pcf8591 ADC via I2C. | ||
package main | ||
|
||
import ( | ||
"machine" | ||
"time" | ||
|
||
"tinygo.org/x/drivers/pcf8591" | ||
) | ||
|
||
var ( | ||
i2c = machine.I2C0 | ||
) | ||
|
||
func main() { | ||
i2c.Configure(machine.I2CConfig{}) | ||
adc := pcf8591.New(i2c) | ||
adc.Configure() | ||
|
||
// get "CH0" aka "machine.ADC" interface to channel 0 from ADC. | ||
p := adc.CH0 | ||
|
||
for { | ||
val := p.Get() | ||
println(val) | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Package pcf8591 implements a driver for the PCF8591 Analog to Digital/Digital to Analog Converter. | ||
// | ||
// Datasheet: https://www.nxp.com/docs/en/data-sheet/PCF8591.pdf | ||
package pcf8591 // import "tinygo.org/x/drivers/pcf8591" | ||
|
||
import ( | ||
"machine" | ||
|
||
"errors" | ||
|
||
"tinygo.org/x/drivers" | ||
) | ||
|
||
// Device wraps PCF8591 ADC functions. | ||
type Device struct { | ||
bus drivers.I2C | ||
Address uint16 | ||
CH0 ADCPin | ||
CH1 ADCPin | ||
CH2 ADCPin | ||
CH3 ADCPin | ||
} | ||
|
||
// ADCPin is the implementation of the ADConverter interface. | ||
type ADCPin struct { | ||
machine.Pin | ||
d *Device | ||
} | ||
|
||
// New returns a new PCF8591 driver. Pass in a fully configured I2C bus. | ||
func New(b drivers.I2C) *Device { | ||
d := &Device{ | ||
bus: b, | ||
Address: defaultAddress, | ||
} | ||
|
||
// setup all channels | ||
d.CH0 = d.GetADC(0) | ||
d.CH1 = d.GetADC(1) | ||
d.CH2 = d.GetADC(2) | ||
d.CH3 = d.GetADC(3) | ||
|
||
return d | ||
} | ||
|
||
// Configure here just for interface compatibility. | ||
func (d *Device) Configure() { | ||
} | ||
|
||
// Read analog data from channel | ||
func (d *Device) Read(ch int) (uint16, error) { | ||
if ch < 0 || ch > 3 { | ||
return 0, errors.New("invalid channel for pcf8591 Read") | ||
} | ||
|
||
return d.GetADC(ch).Get(), nil | ||
} | ||
|
||
// GetADC returns an ADC for a specific channel. | ||
func (d *Device) GetADC(ch int) ADCPin { | ||
return ADCPin{machine.Pin(ch), d} | ||
} | ||
|
||
// Get the current reading for a specific ADCPin. | ||
func (p ADCPin) Get() uint16 { | ||
// TODO: also implement DAC | ||
tx := make([]byte, 2) | ||
tx[0] = byte(p.Pin) | ||
|
||
rx := make([]byte, 2) | ||
|
||
// The result from the measurement triggered by the first write, | ||
// however, the second write is required to get the result. | ||
// See section 8.4 "A/D Conversion" in the datasheet for more info | ||
p.d.bus.Tx(p.d.Address, tx, rx) | ||
p.d.bus.Tx(p.d.Address, tx, rx) | ||
|
||
// scale result to 16bit value like other ADCs | ||
return uint16(rx[1] << 8) | ||
} | ||
|
||
// Configure here just for interface compatibility. | ||
func (p ADCPin) Configure() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package pcf8591 | ||
|
||
// PCF8591 Default Address | ||
const defaultAddress = 0x48 | ||
|
||
// control bit for DAC | ||
const PCF8591_ENABLE_DAC = 0x40 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters