英文:
How do I control PWM output on Arduino using TinyGo
问题
我正在尝试使用在Go语言中编写的代码,使用Arduino ATMega2560来驱动一个电机。
这里有一个使用TinyGo v0.14.1的示例:
https://create.arduino.cc/projecthub/alankrantas/tinygo-on-arduino-uno-an-introduction-6130f6
这个示例的主要代码如下:
func main() {
machine.InitPWM()
led := machine.PWM{machine.D9}
led.Configure()
value := 0
led.Set(uint16(value))
}
当我尝试调用machine.InitPWM()
时,我得到一个错误信息:InitPWM not declared by package machine
。
TinyGo的当前版本(也是我正在使用的版本)是v0.19。似乎machine包已经被修改以不同的方式使用PWM,然而,我找不到任何关于如何正确使用它的信息。
英文:
I'm trying to turn a motor using An Arduino ATMega2560 with code written in Go.
There's an example here that uses TinyGo v0.14.1:
https://create.arduino.cc/projecthub/alankrantas/tinygo-on-arduino-uno-an-introduction-6130f6
The example in essence looks like this:
func main() {
machine.InitPWM()
led := machine.PWM{machine.D9}
led.Configure()
value := 0
led.Set(uint16(value))
}
When I try to call machine.InitPWM()
I get an error InitPWM not declared by package machine
TinyGo's current version (and the one I'm running) is v0.19. It seems as though the machine package has been modified to use PWM differently, however, I cannot find anywhere how to use it correctly.
答案1
得分: 1
确实,在ATMega2560的machine
包中确实没有InitPWM
函数- https://tinygo.org/docs/reference/microcontrollers/machine/arduino-mega2560/
英文:
There is indeed no InitPWM
function in machine
package for ATMega2560 - https://tinygo.org/docs/reference/microcontrollers/machine/arduino-mega2560/
答案2
得分: 0
你必须设置 machine.Timer1 才能使用 pin9。下面的代码将完成你想要的操作,只是因为'value'被设置为0,所以不会发生任何事情。你必须使用0-256之间的值才能执行某些操作:
pwm := machine.Timer1
pin9 := machine.D9
err := pwm.Configure(machinePWMConfig{})
if err != nil {
println(err.Error())
}
ch, err := pwm.Channel(pin9)
if err != nil {
println(err.Error())
}
// 注意 pwm 的值必须在 0-256 之间:
value := uint32(0)
pwm.Set(ch, uint32(value))
英文:
You must set the machine.Timer1 in order to use pin9. The below code will do what you want except that nothing will happen because 'value' is set to 0. You must use a value between 0-256 in order to do something:
pwm := machine.Timer1
pin9 := machine.D9
err := pwm.Configure(machinePWMConfig{})
if err != nil{println(err.Error())}
ch, err := pwm.Channel(pin9)
if err != nil{println(err.Error())}
//note that values are between 0-256 for pwm:
value := uint32(0)
pwm.Set(ch, uint32(value))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论