2014-05-03 21:35:09 +05:30
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kidoman/embd/convertors/mcp3008"
|
2014-05-08 23:28:29 +05:30
|
|
|
|
|
|
|
"github.com/kidoman/embd"
|
2014-05-03 21:35:09 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2014-05-08 23:28:29 +05:30
|
|
|
fmt.Println("this is a sample code for mcp3008 10bit 8 channel ADC")
|
2014-05-03 21:35:09 +05:30
|
|
|
|
2014-05-08 23:28:29 +05:30
|
|
|
if err := embd.InitSPI(); err != nil {
|
2014-05-03 21:35:09 +05:30
|
|
|
panic(err)
|
|
|
|
}
|
2014-05-08 23:28:29 +05:30
|
|
|
defer embd.CloseSPI()
|
|
|
|
|
|
|
|
spiBus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
|
|
|
|
defer spiBus.Close()
|
|
|
|
|
|
|
|
adc := mcp3008.New(mcp3008.SingleMode, spiBus)
|
2014-05-03 21:35:09 +05:30
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
val, err := adc.AnalogValueAt(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:28:29 +05:30
|
|
|
fmt.Printf("analog value is: %v\n", val)
|
2014-05-03 21:35:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|