mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 02:31:05 +01:00
add an example to test PWM balanced mode
This commit is contained in:
parent
915fc229cf
commit
c34d95b74c
44
examples/pwmbalanced/pwm.go
Normal file
44
examples/pwmbalanced/pwm.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
A PWM example by @youngkin, using the go-rpio library
|
||||||
|
|
||||||
|
Fades a PWM hardware pin in and out using PWM mode balanced (vs. markspace)
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stianeikeland/go-rpio/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
err := rpio.Open()
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer rpio.Close()
|
||||||
|
|
||||||
|
pin := rpio.Pin(19)
|
||||||
|
pin.Mode(rpio.Pwm)
|
||||||
|
pin.Freq(64000)
|
||||||
|
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced)
|
||||||
|
// the LED will be blinking at 2000Hz
|
||||||
|
// (source frequency divided by cycle length => 64000/32 = 2000)
|
||||||
|
|
||||||
|
// five times smoothly fade in and out
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
for i := uint32(0); i < 32; i++ { // increasing brightness
|
||||||
|
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced)
|
||||||
|
time.Sleep(time.Second / 32)
|
||||||
|
}
|
||||||
|
for i := uint32(32); i > 0; i-- { // decreasing brightness
|
||||||
|
pin.DutyCycleWithPwmMode(i, 32, rpio.Balanced)
|
||||||
|
time.Sleep(time.Second / 32)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pin.DutyCycleWithPwmMode(0, 32, rpio.Balanced)
|
||||||
|
}
|
6
rpio.go
6
rpio.go
|
@ -13,8 +13,8 @@ Also clock/pwm related oparations:
|
||||||
- Set Duty cycle
|
- Set Duty cycle
|
||||||
And SPI oparations:
|
And SPI oparations:
|
||||||
- SPI transmit/recieve/exchange bytes
|
- SPI transmit/recieve/exchange bytes
|
||||||
- Chip select
|
|
||||||
- Set speed
|
- Set speed
|
||||||
|
- Chip select
|
||||||
|
|
||||||
Example of use:
|
Example of use:
|
||||||
|
|
||||||
|
@ -60,8 +60,8 @@ on the output pins for the raspberry pi, and not the wiringPi convention.
|
||||||
|
|
||||||
See the spec for full details of the BCM2835 controller:
|
See the spec for full details of the BCM2835 controller:
|
||||||
|
|
||||||
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
||||||
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
|
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
|
||||||
|
|
||||||
Changes to support the BCM2711, used on the Raspberry Pi 4, were cribbed from https://github.com/RPi-Distro/raspi-gpio/
|
Changes to support the BCM2711, used on the Raspberry Pi 4, were cribbed from https://github.com/RPi-Distro/raspi-gpio/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user