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)
|
||||||
|
}
|
90
rpio.go
90
rpio.go
|
@ -3,65 +3,65 @@ Package rpio provides GPIO access on the Raspberry PI without any need
|
||||||
for external c libraries (eg. WiringPi or BCM2835).
|
for external c libraries (eg. WiringPi or BCM2835).
|
||||||
|
|
||||||
Supports simple operations such as:
|
Supports simple operations such as:
|
||||||
- Pin mode/direction (input/output/clock/pwm,alt0,alt1,alt2,alt3,alt4,alt5)
|
- Pin mode/direction (input/output/clock/pwm,alt0,alt1,alt2,alt3,alt4,alt5)
|
||||||
- Pin write (high/low)
|
- Pin write (high/low)
|
||||||
- Pin read (high/low)
|
- Pin read (high/low)
|
||||||
- Pin edge detection (no/rise/fall/any)
|
- Pin edge detection (no/rise/fall/any)
|
||||||
- Pull up/down/off
|
- Pull up/down/off
|
||||||
Also clock/pwm related oparations:
|
Also clock/pwm related oparations:
|
||||||
- Set Clock frequency
|
- Set Clock frequency
|
||||||
- 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:
|
||||||
|
|
||||||
rpio.Open()
|
rpio.Open()
|
||||||
defer rpio.Close()
|
defer rpio.Close()
|
||||||
|
|
||||||
pin := rpio.Pin(4)
|
pin := rpio.Pin(4)
|
||||||
pin.Output()
|
pin.Output()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
pin.Toggle()
|
pin.Toggle()
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
The library use the raw BCM2835 pinouts, not the ports as they are mapped
|
The library use the raw BCM2835 pinouts, not the ports as they are mapped
|
||||||
on the output pins for the raspberry pi, and not the wiringPi convention.
|
on the output pins for the raspberry pi, and not the wiringPi convention.
|
||||||
|
|
||||||
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
|
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
|
||||||
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
|
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
|
||||||
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
|
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
|
||||||
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
|
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
|
||||||
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
|
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
|
||||||
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
|
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
|
||||||
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
|
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
|
||||||
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
|
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
|
||||||
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
|
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
|
||||||
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
|
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
|
||||||
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
|
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
|
||||||
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
|
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
|
||||||
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
|
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
|
||||||
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
|
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
|
||||||
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
|
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
|
||||||
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
|
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
|
||||||
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
|
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
|
||||||
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
|
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
|
||||||
| 5 | GPIO 21 | 29 || 30 | 0v | |
|
| 5 | GPIO 21 | 29 || 30 | 0v | |
|
||||||
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
|
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
|
||||||
| 13 | GPIO 23 | 33 || 34 | 0v | |
|
| 13 | GPIO 23 | 33 || 34 | 0v | |
|
||||||
| 19 | GPIO 24 | 35 || 36 | GPIO 27 | 16 |
|
| 19 | GPIO 24 | 35 || 36 | GPIO 27 | 16 |
|
||||||
| 26 | GPIO 25 | 37 || 38 | GPIO 28 | 20 |
|
| 26 | GPIO 25 | 37 || 38 | GPIO 28 | 20 |
|
||||||
| | 0v | 39 || 40 | GPIO 29 | 21 |
|
| | 0v | 39 || 40 | GPIO 29 | 21 |
|
||||||
+-----+---------+----++----+---------+-----+
|
+-----+---------+----++----+---------+-----+
|
||||||
|
|
||||||
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