如何使用TinyGo在Arduino上控制PWM输出

huangapple go评论97阅读模式
英文:

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))

huangapple
  • 本文由 发表于 2021年7月22日 14:47:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/68480314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定