Open xcd0 opened 1 month ago
tinygoのSPIのDACのコード
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)
}
}
PWM変調で十分だが、DACを使用してよりきれいな正弦波を出力できるようにしてもよいかなと