From 9f861dd16276a7b7f24ff7ac3c697d6a69a225e8 Mon Sep 17 00:00:00 2001 From: npotts Date: Sat, 9 Jan 2016 23:59:27 -0700 Subject: [PATCH 1/7] Minor tweaks to work with 12-bit variat MCP3208, which is identical to the MCP3008 execpt for being 12 bits. Signed-off-by: npotts --- convertors/mcp3008/mcp3008.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/convertors/mcp3008/mcp3008.go b/convertors/mcp3008/mcp3008.go index e5ecb99..4d54c8c 100644 --- a/convertors/mcp3008/mcp3008.go +++ b/convertors/mcp3008/mcp3008.go @@ -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 ( @@ -41,6 +46,13 @@ func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) { 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 } From 1224148572d12d49b1cdca040c885c87c5853754 Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 00:02:42 -0700 Subject: [PATCH 2/7] Added example to use 12 bit veriant Signed-off-by: npotts --- samples/mcp3208.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 samples/mcp3208.go diff --git a/samples/mcp3208.go b/samples/mcp3208.go new file mode 100644 index 0000000..497e56e --- /dev/null +++ b/samples/mcp3208.go @@ -0,0 +1,48 @@ +// +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" + "github.com/npotts/embd/convertors/mcp3008" +) + +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 { + panic(err) + } + fmt.Printf("analog value is: %v\n", val) + } +} From 24e96ba4331f6caa84da5f28b5314a110b6d7025 Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 00:23:03 -0700 Subject: [PATCH 3/7] 12 bit ADC needs to have initial request bits shifted a bit --- convertors/mcp3008/mcp3008.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/convertors/mcp3008/mcp3008.go b/convertors/mcp3008/mcp3008.go index 4d54c8c..9e31575 100644 --- a/convertors/mcp3008/mcp3008.go +++ b/convertors/mcp3008/mcp3008.go @@ -38,9 +38,17 @@ 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: + //[0x06| (channel >> 2), channel << 6, 0] + data[0] = 0x06 | (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 { From 90b7ee68d88bf1fafa77ce0320c6c13439984a39 Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 00:50:15 -0700 Subject: [PATCH 4/7] Dont panic due to potential issues (especially in light of error #24) Signed-off-by: npotts --- samples/mcp3208.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/mcp3208.go b/samples/mcp3208.go index 497e56e..4d2184b 100644 --- a/samples/mcp3208.go +++ b/samples/mcp3208.go @@ -41,7 +41,7 @@ func main() { time.Sleep(1 * time.Second) val, err := adc.AnalogValueAt(0) if err != nil { - panic(err) + fmt.Println("Error: ", err) } fmt.Printf("analog value is: %v\n", val) } From 9242ae8dd9fabdb2b057f3a1fbc79ecaff23de1c Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 00:51:02 -0700 Subject: [PATCH 5/7] Be more explicit in what is geting bitshifted Signed-off-by: npotts --- convertors/mcp3008/mcp3008.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/convertors/mcp3008/mcp3008.go b/convertors/mcp3008/mcp3008.go index 9e31575..c34a40d 100644 --- a/convertors/mcp3008/mcp3008.go +++ b/convertors/mcp3008/mcp3008.go @@ -44,8 +44,7 @@ func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) { data[1] = uint8(m.Mode)<<7 | uint8(chanNum)<<4 data[2] = 0 case Bits12: - //[0x06| (channel >> 2), channel << 6, 0] - data[0] = 0x06 | (uint8(chanNum) >> 2) + data[0] = (uint8(startBit) << 2) + (uint8(m.Mode) << 1) + (uint8(chanNum) >> 2) data[1] = uint8(chanNum) << 6 data[2] = 0 } From bb0bc69e8ca87b6ddb086d046ef0349cf1bc1f1c Mon Sep 17 00:00:00 2001 From: npotts Date: Sun, 10 Jan 2016 16:00:19 -0700 Subject: [PATCH 6/7] Reference main lib, not my fork --- samples/mcp3208.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/mcp3208.go b/samples/mcp3208.go index 4d2184b..bd71f87 100644 --- a/samples/mcp3208.go +++ b/samples/mcp3208.go @@ -9,9 +9,8 @@ import ( "time" "github.com/kidoman/embd" - // "github.com/kidoman/embd/convertors/mcp3008" + "github.com/kidoman/embd/convertors/mcp3008" _ "github.com/kidoman/embd/host/all" - "github.com/npotts/embd/convertors/mcp3008" ) const ( From 1cd92e3c2d29e67fa8d002fce445471155dade07 Mon Sep 17 00:00:00 2001 From: Nick Potts Date: Thu, 14 Jan 2016 15:28:48 -0700 Subject: [PATCH 7/7] Update mcp3008.go Add newly required 3rd argument --- samples/mcp3008.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/mcp3008.go b/samples/mcp3008.go index 791d56a..93ed45c 100644 --- a/samples/mcp3008.go +++ b/samples/mcp3008.go @@ -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)