繁体   English   中英

树莓派 3 PWM LED 脉冲

[英]Raspberry Pi 3 PWM Led Pulse

我想使用 bcm2835.h 和纯 C 语言通过 PWM 控制 LED 二极管。 我的代码不起作用。 我错过了什么?

我试过“gpio”控制台命令,它工作正常,所以我知道 led 连接到正确的端口。 我可以使用控制台命令打开它:

gpio pwm 1 1024


我的代码:

#include <bcm2835.h>
#include <stdio.h>

// PWM output on RPi Plug P1 pin 12 (which is GPIO pin 18) in alt fun 5.
// Note that this is the _only_ PWM pin available on the RPi IO headers
#define PIN RPI_GPIO_P1_12

// and it is controlled by PWM channel 0
#define PWM_CHANNEL 0

// This controls the max range of the PWM signal
#define RANGE 1024


int main(int argc, char **argv)
{
    if (!bcm2835_init())
    {
        return 1;
    }

    // Set the output pin to Alt Fun 5, to allow PWM channel 0 to be output there
    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_ALT5);

    // Clock divider is set to 16.
    // With a divider of 16 and a RANGE of 1024, in MARKSPACE mode,
    // the pulse repetition frequency will be
    // 1.2MHz/1024 = 1171.875Hz, suitable for driving a DC motor with PWM
    bcm2835_pwm_set_clock(BCM2835_PWM_CLOCK_DIVIDER_16);

    bcm2835_pwm_set_mode(PWM_CHANNEL, 1, 1);

    bcm2835_pwm_set_range(PWM_CHANNEL, RANGE);

    while(1)
    {
        bcm2835_pwm_set_data(PWM_CHANNEL, 1024);
        bcm2835_delay(10);
    }


    bcm2835_close();
    return 0;
}

我希望我的 LED 会亮起。

这很愚蠢,但在检查了数学、代码和接线后,我发现应用程序需要以 root 权限运行才能在板上访问引脚。 它工作正常。 话题可以关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM