Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Support for MCP3208 (12 bit version of MCP3008) #47

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions convertors/mcp3008/mcp3008.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion samples/mcp3008.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 47 additions & 0 deletions samples/mcp3208.go
Original file line number Diff line number Diff line change
@@ -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)
}
}