mirror of https://github.com/kidoman/embd
Merge 1cd92e3c2d
into d3d8c0c5c6
This commit is contained in:
commit
2a3daef487
|
@ -8,11 +8,16 @@ import (
|
|||
|
||||
// MCP3008 represents a mcp3008 8bit DAC.
|
||||
type MCP3008 struct {
|
||||
Mode byte
|
||||
|
||||
Bus embd.SPIBus
|
||||
Mode, Bits byte
|
||||
Bus embd.SPIBus
|
||||
}
|
||||
|
||||
//How many bits does the
|
||||
const (
|
||||
Bits10 = iota //10 bit MCP300** family
|
||||
Bits12 //12 bit MCP320** family
|
||||
)
|
||||
|
||||
const (
|
||||
// SingleMode represents the single-ended mode for the mcp3008.
|
||||
SingleMode = 1
|
||||
|
@ -22,8 +27,8 @@ const (
|
|||
)
|
||||
|
||||
// New creates a representation of the mcp3008 convertor
|
||||
func New(mode byte, bus embd.SPIBus) *MCP3008 {
|
||||
return &MCP3008{mode, bus}
|
||||
func New(mode, bits byte, bus embd.SPIBus) *MCP3008 {
|
||||
return &MCP3008{Mode: mode, Bus: bus}
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -33,14 +38,28 @@ const (
|
|||
// AnalogValueAt returns the analog value at the given channel of the convertor.
|
||||
func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) {
|
||||
var data [3]uint8
|
||||
data[0] = startBit
|
||||
data[1] = uint8(m.Mode)<<7 | uint8(chanNum)<<4
|
||||
data[2] = 0
|
||||
switch m.Bits {
|
||||
case Bits10:
|
||||
data[0] = startBit
|
||||
data[1] = uint8(m.Mode)<<7 | uint8(chanNum)<<4
|
||||
data[2] = 0
|
||||
case Bits12:
|
||||
data[0] = (uint8(startBit) << 2) + (uint8(m.Mode) << 1) + (uint8(chanNum) >> 2)
|
||||
data[1] = uint8(chanNum) << 6
|
||||
data[2] = 0
|
||||
}
|
||||
|
||||
glog.V(2).Infof("mcp3008: sendingdata buffer %v", data)
|
||||
if err := m.Bus.TransferAndReceiveData(data[:]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
switch m.Bits {
|
||||
case Bits10:
|
||||
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")
|
||||
}
|
||||
|
||||
return int(uint16(data[1]&0x03)<<8 | uint16(data[2])), nil
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func main() {
|
|||
spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
|
||||
defer spiBus.Close()
|
||||
|
||||
adc := mcp3008.New(mcp3008.SingleMode, spiBus)
|
||||
adc := mcp3008.New(mcp3008.SingleMode, mcp3008.Bits10, spiBus)
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
// +build ignore
|
||||
|
||||
// this sample uses the mcp3008 package to interface with a similar MCP3208, which is a 12 bit variant. Works without code change on bbb and rpi
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/convertors/mcp3008"
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
const (
|
||||
channel = 0
|
||||
speed = 1000000
|
||||
bpw = 8
|
||||
delay = 0
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC")
|
||||
|
||||
if err := embd.InitSPI(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer embd.CloseSPI()
|
||||
|
||||
spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
|
||||
defer spiBus.Close()
|
||||
|
||||
adc := &mcp3008.MCP3008{Mode: mcp3008.SingleMode, Bus: spiBus, Bits: mcp3008.Bits12}
|
||||
|
||||
// adc := mcp3008.New(mcp3008.SingleMode, spiBus)
|
||||
|
||||
for i := 0; i < 20; i++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
val, err := adc.AnalogValueAt(0)
|
||||
if err != nil {
|
||||
fmt.Println("Error: ", err)
|
||||
}
|
||||
fmt.Printf("analog value is: %v\n", val)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue