描述
本文来源电子发烧友社区,作者:黄宝亚, 帖子地址:
https://bbs.elecfans.com/jishu_2289350_1_1.html使用448和450口,实现电机正传几圈,翻转几圈,一直循环程序如下
-
#include
-
#include
-
#include
-
#include
-
#include //define O_WRONLY and O_RDONLY
-
#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"
-
#define SYSFS_GPIO_RST_PIN_MOTOER1"448"
-
#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio448/direction"
-
#define SYSFS_GPIO_RST_DIR_MOTOER1"OUT"
-
#define SYSFS_GPIO_RST_MOTOER1 "/sys/class/gpio/gpio448/value"
-
#define SYSFS_GPIO_RST_MOTOER1_H "1"
-
#define SYSFS_GPIO_RST_MOTOER1 _L "0"
-
-
#define SYSFS_GPIO_EXPORT "/sys/class/gpio/export"
-
#define SYSFS_GPIO_RST_PIN_MOTOER2 "450"
-
#define SYSFS_GPIO_RST_DIR "/sys/class/gpio/gpio448/direction"
-
#define SYSFS_GPIO_RST_DIR_MOTOER2 "OUT"
-
#define SYSFS_GPIO_RST_MOTOER2 "/sys/class/gpio/gpio448/value"
-
#define SYSFS_GPIO_RST_MOTOER2 _H "1"
-
#define SYSFS_GPIO_RST_MOTOER2 _L "0"
-
-
int main()
-
{
-
int fd;
-
//打开端口/sys/class/gpio# echo 448 > export
-
fd = open(SYSFS_GPIO_EXPORT, O_WRONLY);
-
if(fd == -1)
-
{
-
printf("ERR: Radio hard reset pin open error.n");
-
return EXIT_FAILURE;
-
}
-
write(fd, SYSFS_GPIO_RST_PIN_MOTOER1 ,sizeof(SYSFS_GPIO_RST_PIN_MOTOER1 ));
-
close(fd);
-
//设置端口方向/sys/class/gpio/gpio448# echo out > direction
-
fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);
-
if(fd == -1)
-
{
-
printf("ERR: Radio hard reset pin direction open error.n");
-
return EXIT_FAILURE;
-
}
-
write(fd, SYSFS_GPIO_RST_DIR_MOTOER2 , sizeof(SYSFS_GPIO_RST_DIR_MOTOER2 ));
-
close(fd);
-
//设置端口方向/sys/class/gpio/gpio450# echo out > direction
-
fd = open(SYSFS_GPIO_RST_DIR, O_WRONLY);
-
if(fd == -1)
-
{
-
printf("ERR: Radio hard reset pin direction open error.n");
-
return EXIT_FAILURE;
-
}
-
write(fd, SYSFS_GPIO_RST_DIR_MOTOER2 , sizeof(SYSFS_GPIO_RST_DIR_MOTOER2 ));
-
close(fd);
-
-
-
-
//输出复位信号: 拉高>100ns
-
fd = open(SYSFS_GPIO_RST_MOTOER1 , O_RDWR);
-
if(fd == -1)
-
{
-
printf("ERR: Radio hard reset pin value open error.n");
-
return EXIT_FAILURE;
-
}
-
while(1)
-
{
-
write(fd, SYSFS_GPIO_RST_MOTOER1 _H, sizeof(SYSFS_GPIO_RST_MOTOER1 _H));
-
write(fd, SYSFS_GPIO_RST_MOTOER2 _H, sizeof(SYSFS_GPIO_RST_MOTOER2 _H));
-
usleep(1000000);
-
write(fd, SYSFS_GPIO_RST_ MOTOER1 _H, sizeof(SYSFS_GPIO_RST_ MOTOER1 _H));
-
write(fd, SYSFS_GPIO_RST_ MOTOER2 _H, sizeof(SYSFS_GPIO_RST_ MOTOER2 _H));
-
usleep(1000000);
-
}
-
close(fd);
-
printf("INFO: Radio hard reset pin value open error.n");
-
return 0;
-
}
复制代码
打开APP阅读更多精彩内容