树莓派3B开发Go语言案例-3.PCF8591模数模块使用

栏目: 软件资讯 · 发布时间: 6年前

内容简介:title: 树莓派3B开发Go语言案例-3.PCF8591模数模块使用tags: 树莓派,go,golang,3B,3B+,PCF8591该案例主要实现模拟信号和数字信号的转换,通过调节PCF8591模块上的可调电阻,控制LED灯的亮度,该节涉及几个知识点:I2C、PCF8591,这里不做讲解,可以自行百度

title: 树莓派3B开发 Go 语言案例-3.PCF8591模数模块使用

tags: 树莓派,go,golang,3B,3B+,PCF8591

该案例主要实现模拟信号和数字信号的转换,通过调节PCF8591模块上的可调电阻,控制LED灯的亮度,该节涉及几个知识点:I2C、PCF8591,这里不做讲解,可以自行百度

PCF8591

接线操作,将PCF8591的SDA接到树莓派的第3个针脚,SCL接到树莓派的第5个针脚,AOUT接LED灯正极,可调电阻和AIN0针脚关联

代码如下,第一个文件是参考 python 的PCF8591驱动写的go版驱动

package pcf8591

import (
    "periph.io/x/periph/conn"
    "periph.io/x/periph/conn/i2c"
)

// Channel 模拟信号输入
type Channel uint8

// AIN0~AIN3 模拟信号输入端
const (
    AIO0 Channel = 0
    AIO1 Channel = 1
    AIO2 Channel = 2
    AIO3 Channel = 3
)

// Dev is a handle to an initialized PCF8591 device.
type Dev struct {
    d    conn.Conn
    name string
}

// NewI2C returns an object that communicates over I²C to PCF8591
func NewI2C(b i2c.Bus, addr uint16) (*Dev, error) {
    d := &Dev{d: &i2c.Dev{Bus: b, Addr: addr}}
    return d, nil
}

// Read 读取结果
func (d *Dev) Read(channel Channel) (result byte) {
    tmp := make([]byte, 1)
    switch channel {
    case AIO0:
        d.d.Tx([]byte{0x40}, tmp)
    case AIO1:
        d.d.Tx([]byte{0x41}, tmp)
    case AIO2:
        d.d.Tx([]byte{0x42}, tmp)
    case AIO3:
        d.d.Tx([]byte{0x43}, tmp)
    }
    result = tmp[0]
    return
}

// Write 写入数据
func (d *Dev) Write(val byte) {
    d.d.Tx([]byte{0x40, val}, nil)
}

这段代码主要实现功能,进行模数数模转换

package main

import (
    "log"
    "raspbarry/device/pcf8591"
    "time"

    "periph.io/x/periph/conn/i2c/i2creg"
    "periph.io/x/periph/host"
)

func main() {
    // 加载所有驱动
    if _, err := host.Init(); err != nil {
        log.Fatal(err)
    }

    // 打开第一个可用I²C总线的handle
    bus, err := i2creg.Open("")
    if err != nil {
        log.Fatal(err)
    }
    defer bus.Close()

    // 通过执行命令 'sudo i2cdetect -y 1' 查看 PCF8591 设备地址
    dev, err := pcf8591.NewI2C(bus, 0x48)
    if err != nil {
        log.Fatal(err)
    }

    var tmp1 byte
    var tmp2 uint16
    for {
        // 读取可调电阻值
        tmp1 = dev.Read(pcf8591.AIO0)
        tmp2 = BytesToUint16([]byte{tmp1})
        // LED灯亮度值125~255
        tmp2 = tmp2*(255-125)/255 + 125
        // 调节LED灯的亮度
        dev.Write(Uint16ToBytes(tmp2)[0])

        log.Println(tmp1)
        // 亮度为0时退出程序
        if tmp1 == 0 {
            break
        }
        time.Sleep(50)
    }
}

// Uint16ToBytes unit16 -> bytes
func Uint16ToBytes(n uint16) []byte {
    return []byte{
        byte(n),
        byte(n >> 8),
    }
}

// BytesToUint16 bytes -> unit16
func BytesToUint16(array []byte) uint16 {
    var data uint16 = 0
    for i := 0; i < len(array); i++ {
        data = data + uint16(uint(array[i])<<uint(8*i))
    }

    return data
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming Concurrency on the JVM

Programming Concurrency on the JVM

Venkat Subramaniam / The Pragmatic Bookshelf / 2011-6-1 / USD 35.00

Concurrency on the Java platform has evolved, from the synchronization model of JDK to software transactional memory (STM) and actor-based concurrency. This book is the first to show you all these con......一起来看看 《Programming Concurrency on the JVM》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具