使用C中的libgpiod永久更改引脚状态

问题描述 投票:0回答:1

我正在编写一个C程序,该程序控制Raspberry PI上的GPIO状态。我正在使用libgpiod并调用此函数:

gpiod_ctxless_set_value(GPIO_CHIP_NAME, RESET_PIN, 1, false, "some consumer",  NULL, NULL); 

但是,函数执行后,引脚立即变回低电平。如何永久更改?或者,至少直到程序退出?

c linux raspberry-pi gpio
1个回答
0
投票

正如@ 0andriy所提到的,出于我的目的,我需要更多的低级函数,而不是无上下文的函数。这是我的带有通用libgpiod函数的测试示例。

#include <stdio.h>
#include <stdlib.h>
#include <gpiod.h>

#define LED_PIN 28

int main()
{
        struct gpiod_chip *gpiochip;
        struct gpiod_line *gpioline;
        int ret;

        gpiochip = gpiod_chip_open("/dev/gpiochip0");
        if (gpiochip == NULL)
                goto error1;
        printf("gpiochip open is ok\r\n");
        gpioline = gpiod_chip_get_line(gpiochip, TEST_PIN);
        if (gpioline == NULL)
                goto error2;
        printf("gpioline open is ok\r\n");
        ret = gpiod_line_request_output(gpioline, "gpio", 0);
        if (ret != 0)
                goto error2;
        printf("request output is ok\r\n");
        for (int i = 0; i < 5; i++)
        {
                ret = gpiod_line_set_value(gpioline, 1);
                printf("LED on\r\n");
                sleep(1);
                ret = gpiod_line_set_value(gpioline, 0);
                printf("LED off\r\n");
                sleep(1);
        }
        gpiod_line_release(gpioline);
error2:
        gpiod_chip_close(gpiochip);
error1:
        return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.