xcd0 / swiper

raspberry pi pico で作る複式電鍵用エレキー。
0 stars 0 forks source link

DACを使用した波形生成 #7

Open xcd0 opened 1 month ago

xcd0 commented 1 month ago

PWM変調で十分だが、DACを使用してよりきれいな正弦波を出力できるようにしてもよいかなと

xcd0 commented 1 month ago

tinygoのSPIのDACのコード

https://blueeyes.sakura.ne.jp/2020/06/05/3240/#:~:text=%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%BE%E3%81%99%E3%80%82-,mcp4922,-MicroChip%20%E7%A4%BE%E3%81%AE

package main

import (
    "machine"
    "time"
)

const nss = machine.Pin(machine.PA4)

func main() {
    machine.SPI0.Configure(machine.SPIConfig{
        Frequency: 100000,
        Mode: 0,
    })

    nss.Configure(machine.PinConfig{Mode: machine.PinOutput})
    nss.High()

    const header = 0b0111
    var value uint16
    for {
        value += 8
        if (value & 0xF000) != 0 {
            value = 0
        }
        send((header << 12) | value)
        time.Sleep(time.Millisecond * 1)
    }
}

func send(data uint16) {
    tx := make([]byte, 2)
    rx := make([]byte, 2)

    tx[0] = byte(data >> 8)
    tx[1] = byte(data & 0xFF)

    nss.Low()
    machine.SPI0.Tx(tx, rx)
    nss.High()
}

その下にI2CでLCDを動かすコードがある

package main

import (
    "machine"
    "time"
)

const led = machine.LED
func main() {
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})

    machine.I2C0.Configure(machine.I2CConfig{})
    err := lcdInit()
    if err != nil {
    }
    time.Sleep(time.Millisecond * 200)

    for {
        lcdString("Hello")
        time.Sleep(time.Millisecond * 500)
    }
}

func lcdInit() error {
    cmds := []byte{0x38, 0x39, 0x14, 0x73, 0x56, 0x6C, 0x38, 0x01, 0x0C}

    for i := 0; i < len(cmds); i++ {
        err := machine.I2C0.Tx(0x7c >> 1, []byte{0x00, cmds[i]}, nil)
        if err != nil {
            return err
        }
        time.Sleep(time.Millisecond * 100)
    }
    return nil
}

func lcdString(str string) {
    for i := 0; i < len(str); i++ {
        machine.I2C0.Tx(0x7c >> 1, []byte{0x40, str[i]}, nil)
        time.Sleep(time.Millisecond * 1)
    }
}