mirror of
https://github.com/kidoman/embd
synced 2025-07-01 19:18:08 +02:00
Minor tweaks to work with 12-bit variat MCP3208, which is identical to the MCP3008 execpt for being 12 bits.
Signed-off-by: npotts <npotts@users.noreply.github.com>
This commit is contained in:
parent
bfcd1345fe
commit
9f861dd162
@ -8,11 +8,16 @@ import (
|
|||||||
|
|
||||||
// MCP3008 represents a mcp3008 8bit DAC.
|
// MCP3008 represents a mcp3008 8bit DAC.
|
||||||
type MCP3008 struct {
|
type MCP3008 struct {
|
||||||
Mode byte
|
Mode, Bits byte
|
||||||
|
|
||||||
Bus embd.SPIBus
|
Bus embd.SPIBus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//How many bits does the
|
||||||
|
const (
|
||||||
|
Bits10 = iota //10 bit MCP300** family
|
||||||
|
Bits12 //12 bit MCP320** family
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// SingleMode represents the single-ended mode for the mcp3008.
|
// SingleMode represents the single-ended mode for the mcp3008.
|
||||||
SingleMode = 1
|
SingleMode = 1
|
||||||
@ -22,8 +27,8 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// New creates a representation of the mcp3008 convertor
|
// New creates a representation of the mcp3008 convertor
|
||||||
func New(mode byte, bus embd.SPIBus) *MCP3008 {
|
func New(mode, bits byte, bus embd.SPIBus) *MCP3008 {
|
||||||
return &MCP3008{mode, bus}
|
return &MCP3008{Mode: mode, Bus: bus}
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -41,6 +46,13 @@ func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) {
|
|||||||
if err := m.Bus.TransferAndReceiveData(data[:]); err != nil {
|
if err := m.Bus.TransferAndReceiveData(data[:]); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
switch m.Bits {
|
||||||
|
case Bits10:
|
||||||
return int(uint16(data[1]&0x03)<<8 | uint16(data[2])), nil
|
return int(uint16(data[1]&0x03)<<8 | uint16(data[2])), nil
|
||||||
|
case Bits12:
|
||||||
|
return int(uint16(data[1]&0x0f)<<8 | uint16(data[2])), nil
|
||||||
|
default:
|
||||||
|
panic("mcp3008: unknown number of bits")
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user