mirror of
https://github.com/stevenhowes/TinyGo-Pico-PWM.git
synced 2026-05-26 15:53:47 +01:00
Working code
This commit is contained in:
@@ -1,2 +1,8 @@
|
|||||||
# TinyGo-Pico-PWM
|
# TinyGo-Pico-PWM
|
||||||
A working example of PWM for the Pi Pico using TinyGo
|
A working example of PWM for the Pi Pico using TinyGo.
|
||||||
|
|
||||||
|
I was frustraded by various examples (includig the official docs) being wrong for the Pico so I decided to make my own. It flashes the onboard LED in a 'breathe' pattern.
|
||||||
|
|
||||||
|
Makefile included, so you can 'make install', alternatively:
|
||||||
|
|
||||||
|
tinygo flash -target=pico
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"machine"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// 500Hz
|
||||||
|
var period uint64 = 1e9 / 500
|
||||||
|
|
||||||
|
pin := machine.LED
|
||||||
|
pwm := machine.PWM4
|
||||||
|
|
||||||
|
pin.Configure(machine.PinConfig{Mode: machine.PinPWM})
|
||||||
|
|
||||||
|
err := pwm.Configure(machine.PWMConfig{Period: period})
|
||||||
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
|
}
|
||||||
|
ch, err := pwm.Channel(pin)
|
||||||
|
if err != nil {
|
||||||
|
println(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
pwm.SetTop(uint32(255))
|
||||||
|
|
||||||
|
for {
|
||||||
|
// Fade in
|
||||||
|
for i := 0; i <= 255; i++ {
|
||||||
|
pwm.Set(ch, uint32(i))
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fade out
|
||||||
|
for i := 255; i >= 0; i-- {
|
||||||
|
pwm.Set(ch, uint32(i))
|
||||||
|
time.Sleep(time.Millisecond * 10)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait one second
|
||||||
|
time.Sleep(time.Millisecond * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user