<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[用用D1上的GPIO（封装好的函数）]]></title><description><![CDATA[<p dir="auto">由于最近要使用GPIO，所以今天就把玩了一下D1的GPIO，在使用的时候需要注意，GPIO是否可用，有没有被其他模块使用，可以下载原理图进行查看，本着扩展IO肯定没模块用的心理，就直接上扩展IO了。</p>
<p dir="auto">话不多说直接上代码，晚点发GitHub。</p>
<pre><code class="language-C">#ifndef MOTOR_H
#define MOTOR_H


#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;    
#include &lt;sys/stat.h&gt;    
#include &lt;fcntl.h&gt;

#define GPIO_PATH "/sys/class/gpio/gpio%d/value"
#define GPIO_FUNCTION_PATH "/sys/class/gpio/gpio%d/direction"


static int GPIO_Point_IN1_fd;
static int GPIO_Point_IN2_fd;
static int GPIO_Point_IN3_fd;
static int GPIO_Point_IN4_fd;


static unsigned char phasecw[4] = {0x08, 0x04, 0x02, 0x01};


//

#define GPIO_Point_IN1 2021
#define GPIO_Point_IN2 2022
#define GPIO_Point_IN3 2023
#define GPIO_Point_IN4 2024

#define GPIO_WRITEPIN(Point,value) (write(Point,value,1))

#define delay_ms(x)  (usleep(x*1000))

#define GPIO_Point_IN1_Set write(GPIO_Point_IN1_fd, "1", 1)
#define GPIO_Point_IN2_Set write(GPIO_Point_IN2_fd, "1", 1)
#define GPIO_Point_IN3_Set write(GPIO_Point_IN3_fd, "1", 1)
#define GPIO_Point_IN4_Set write(GPIO_Point_IN4_fd, "1", 1)

#define GPIO_Point_IN1_ReSet write(GPIO_Point_IN1_fd, "0", 1)
#define GPIO_Point_IN2_ReSet write(GPIO_Point_IN2_fd, "0", 1)
#define GPIO_Point_IN3_ReSet write(GPIO_Point_IN3_fd, "0", 1)
#define GPIO_Point_IN4_ReSet write(GPIO_Point_IN4_fd, "0", 1)



int Motor_Init();

void GPIO_WrietPin(int GPIO_Point,int GPIO_Value);
void MotorCW( void );
#endif

</code></pre>
<pre><code class="language-C">#include "Motor.h"

static int export_fd, direction_fd, gpiovalue_fd;

static int Motor_GPIO_Init();
static int Motor_GPIO_Function();
static int Motor_GPIO_File_FOK();



int Motor_Init(){
    //1、检查文件是否存在 4 个 GPIO 都需要检查.
    if(-1 == Motor_GPIO_File_FOK()){
        printf("Motor_GPIO_File creat Faild\n");
        return -1;
    }
    //2、设置IO功能为 out 
    if(-1 == Motor_GPIO_Function()){
        printf("Motor_GPIO_Function set Faild \n");
        return -1;
    }
    //2、IO file open return fd*
    if(-1 == Motor_GPIO_Init()){
        printf("Motor_GPIO_File Open Faild \n");
        return -1;
    }
    return 1;
}
void MotorCW( void )
{
    unsigned char i;
    unsigned char temp = 0;
    for( i = 0; i &lt; 4; i++ )
    {
        temp = phasecw[i];

        // LD = ( temp &gt;&gt; 3 ) &amp; 0x01;							//取bit4的值
        // LC = ( temp &gt;&gt; 2 ) &amp; 0x01;
        // LB = ( temp &gt;&gt; 1 ) &amp; 0x01;
        // LA = ( temp &gt;&gt; 0 ) &amp; 0x01;							//取bit0的值
        GPIO_WrietPin(GPIO_Point_IN1, ( temp &gt;&gt; 3 ) &amp; 0x01);
        GPIO_WrietPin(GPIO_Point_IN2, ( temp &gt;&gt; 2 ) &amp; 0x01);
        GPIO_WrietPin(GPIO_Point_IN3, ( temp &gt;&gt; 1 ) &amp; 0x01);
        GPIO_WrietPin(GPIO_Point_IN4, ( temp &gt;&gt; 0 ) &amp; 0x01);

        delay_ms(2); 										
    }
}

void GPIO_WrietPin(int GPIO_Point,int GPIO_Value){
    if(!(GPIO_Value == 0 || GPIO_Value == 1)){
        printf("GPIO_Value Faild %d \n",GPIO_Value);
        return -1;
    }
    switch(GPIO_Point){
        case GPIO_Point_IN1:
            //GPIO_WRITEPIN(GPIO_Point_IN1_fd,GPIO_Value);
            if(GPIO_Value) GPIO_Point_IN1_Set;
            else GPIO_Point_IN1_ReSet;
            break;
        case GPIO_Point_IN2:
            //GPIO_WRITEPIN(GPIO_Point_IN2_fd,GPIO_Value);
            if(GPIO_Value) GPIO_Point_IN2_Set;
            else GPIO_Point_IN2_ReSet;
            break;
        case GPIO_Point_IN3:
            //GPIO_WRITEPIN(GPIO_Point_IN3_fd,GPIO_Value);
            if(GPIO_Value) GPIO_Point_IN3_Set;
            else GPIO_Point_IN3_ReSet;
            break;
        case GPIO_Point_IN4:
            //GPIO_WRITEPIN(GPIO_Point_IN4_fd,GPIO_Value);
            if(GPIO_Value) GPIO_Point_IN4_Set;
            else GPIO_Point_IN4_ReSet;
            break;
        default:
            break;
    }

}

static int Motor_GPIO_File_FOK(){

    char gpio_file_path[40] = {0}; 
    char gpio_Point_File_Num[4] = {0};

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN1);
    sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN1);
    if(access(gpio_file_path,F_OK)){
        printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN1);

        export_fd = open("/sys/class/gpio/export", O_WRONLY);
        if(-1 == export_fd)
        {
                printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                return -1;
        }
        if(-1 == write(export_fd, gpio_Point_File_Num, 4))
        {
                printf("write file operation error\r\n");
                return -1;
        }
        close(export_fd);
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN2);
    sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN2);
    if(access(gpio_file_path,F_OK)){
        printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN2);

        export_fd = open("/sys/class/gpio/export", O_WRONLY);
        if(-1 == export_fd)
        {
                printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                return -1;
        }
        if(-1 == write(export_fd, gpio_Point_File_Num, 4))
        {
                printf("write file operation error\r\n");
                return -1;
        }
        close(export_fd);
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN3);
    sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN3);
    if(access(gpio_file_path,F_OK)){
        printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN3);

        export_fd = open("/sys/class/gpio/export", O_WRONLY);
        if(-1 == export_fd)
        {
                printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                return -1;
        }
        if(-1 == write(export_fd, gpio_Point_File_Num, 4))
        {
                printf("write file operation error\r\n");
                return -1;
        }
        close(export_fd);
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN4);
    sprintf(gpio_Point_File_Num,"%d",GPIO_Point_IN4);
    if(access(gpio_file_path,F_OK)){
        printf("GPIO [%d] not find creat it \n ",GPIO_Point_IN4);

        export_fd = open("/sys/class/gpio/export", O_WRONLY);
        if(-1 == export_fd)
        {
                printf("[%s]:[%d] open gpio export file error\r\n", __FUNCTION__, __LINE__);
                return -1;
        }
        if(-1 == write(export_fd, gpio_Point_File_Num, 4))
        {
                printf("write file operation error\r\n");
                return -1;
        }
        close(export_fd);
    }
    return 1;
}


static int Motor_GPIO_Function(){
    char GPIO_Function_Path[40] = {0};
    sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN1);
    direction_fd = open(GPIO_Function_Path, O_WRONLY);
    if(-1 == direction_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        
        return -1;
    }
    if(-1 == write(direction_fd, "out", sizeof("out")))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(direction_fd);
        return -1;
    }
    close(direction_fd);

     sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN2);
    direction_fd = open(GPIO_Function_Path, O_WRONLY);
    if(-1 == direction_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        
        return -1;
    }
    if(-1 == write(direction_fd, "out", sizeof("out")))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(direction_fd);
        return -1;
    }
    close(direction_fd);

     sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN3);
    direction_fd = open(GPIO_Function_Path, O_WRONLY);
    if(-1 == direction_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        
        return -1;
    }
    if(-1 == write(direction_fd, "out", sizeof("out")))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(direction_fd);
        return -1;
    }
    close(direction_fd);

     sprintf(GPIO_Function_Path,GPIO_FUNCTION_PATH,GPIO_Point_IN4);
    direction_fd = open(GPIO_Function_Path, O_WRONLY);
    if(-1 == direction_fd)
    {
        printf("[%s]:[%d] open gpio direction file error\r\n", __FUNCTION__, __LINE__);
        
        return -1;
    }
    if(-1 == write(direction_fd, "out", sizeof("out")))
    {
        printf("[%s]:[%d] write operation direction error\r\n", __FUNCTION__, __LINE__);
        close(direction_fd);
        return -1;
    }
    close(direction_fd);

    return 1;
}
// int GPIO_Point_IN1_fd;
// int GPIO_Point_IN2_fd;
// int GPIO_Point_IN3_fd;
// int GPIO_Point_IN4_fd;
static int Motor_GPIO_Init(){

    char gpio_file_path[40] = {0}; 

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN1);
    GPIO_Point_IN1_fd = open(gpio_file_path, O_WRONLY);  //RDWR
    if(-1 == GPIO_Point_IN1_fd)
    {
        printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN2);
    GPIO_Point_IN2_fd = open(gpio_file_path, O_WRONLY);  //RDWR
    if(-1 == GPIO_Point_IN2_fd)
    {
        printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN3);
    GPIO_Point_IN3_fd = open(gpio_file_path, O_WRONLY);  //RDWR
    if(-1 == GPIO_Point_IN3_fd)
    {
        printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }

    sprintf(gpio_file_path,GPIO_PATH,GPIO_Point_IN4);
    GPIO_Point_IN4_fd = open(gpio_file_path, O_WRONLY);  //RDWR
    if(-1 == GPIO_Point_IN4_fd)
    {
        printf("[%s]:[%d]: open value file error\r\n", __FUNCTION__, __LINE__);
        return -1;
    }
    return 1;
}

</code></pre>
<pre><code class="language-Makefile">CC := riscv64-unknown-linux-gnu-gcc

target = Motor

LDFLAGS ?= -lm
CFLAGS ?= -I .



CSRCS += Motor.c
MAINSRC  = ./main.c


all : default


OBJEXT ?= .o

COBJS = $(CSRCS:.c=$(OBJEXT))
MAINOBJ = $(MAINSRC:.c=$(OBJEXT))

# SRCS =  $(CSRCS) $(MAINSRC)s
%.o: %.c
	@$(CC)  $(CFLAGS) -c $&lt; -o $@
	@echo "CC $&lt;"

default:  $(COBJS) $(MAINOBJ)
	$(CC) -o $(target) $(MAINOBJ)  $(COBJS) $(LDFLAGS)
	

clean: 
	rm -f $(target) $(AOBJS) $(COBJS) $(MAINOBJ)
</code></pre>
]]></description><link>https://bbs.aw-ol.com/topic/338/用用d1上的gpio-封装好的函数</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 13:44:19 GMT</lastBuildDate><atom:link href="https://bbs.aw-ol.com/topic/338.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 24 Aug 2021 11:24:54 GMT</pubDate><ttl>60</ttl></channel></rss>