基于STM32F10X的GPIO驱动

  1. 完善我的文章《MCU BSP Driver分层设计》,本次提供GPIO控制驱动。

  1. 本片完成了GPIO控制驱动操作。

  1. 本人是基于野火指南者STM32开发板。

  1. 本驱动基于STM32F10X官方固件库。

  1. 主程序

int main(void)
{
    WHT_LED_Init_Config();
    for(;;)
    {
        WHT_LED_Color_Set(LED_RED);
        Delay(500);
        WHT_LED_Color_Set(LED_GREEN);
        Delay(500);
        WHT_LED_Color_Set(LED_BLUE);
        Delay(500);
        WHT_LED_Color_Set(LED_RGBOFF);
        Delay(100);
    }
}
  1. LED灯驱动程序

led头文件如下:

#ifndef __LED_DRIVER_H__
#define    __LED_DRIVER_H__

#include "../../BSP/GPIO_BSP/gpio_bsp.h"


//Red
#define LED0_GPIO_Port   PortB
#define LED0_GPIO_Pin    Pin5
//Green
#define LED1_GPIO_Port   PortB
#define LED1_GPIO_Pin    Pin0
//Blue
#define LED2_GPIO_Port   PortB
#define LED2_GPIO_Pin    Pin1

typedef enum
{
    LED_RED    = 0,//红色
    LED_GREEN  = 1,//绿色
    LED_BLUE   = 2,//蓝色
    LED_YELLOW = 3,//黄色(红+绿)
    LED_PURPLE = 4,//紫色(红+蓝)
    LED_CYAN   = 5,//青色(绿+蓝)
    LED_WHITE  = 6,//白色(红+绿+蓝)
    LED_RGBOFF = 7,//黑色(全部关闭)
}WHT_LED_Color_enum;

extern void WHT_LED_Init_Config(void);
extern void WHT_LED_Color_Set(WHT_LED_Color_enum LED_Color);

#endif /* __LED_DRIVER_H__ */

led源文件如下:

#include "led_driver.h"

static WHT_GPIO_Device_t led_device = { 0 };

static void wht_gpio_init_config(void)
{
    WHT_LED_Color_Set(LED_WHITE);
    led_device.WHT_Set_GPIO_Mode(LED0_GPIO_Port, LED0_GPIO_Pin, Mode_Out_PP);
    led_device.WHT_Set_GPIO_Mode(LED1_GPIO_Port, LED1_GPIO_Pin, Mode_Out_PP);
    led_device.WHT_Set_GPIO_Mode(LED2_GPIO_Port, LED2_GPIO_Pin, Mode_Out_PP);
}

void WHT_LED_Color_Set(WHT_LED_Color_enum LED_Color)
{
    WHT_GPIO_State_enum LED_Red_State;
    WHT_GPIO_State_enum LED_Green_State;
    WHT_GPIO_State_enum LED_Blue_State;

    switch (LED_Color)
    {
    case LED_RED:
        LED_Red_State = Low;
        LED_Green_State = Hig;
        LED_Blue_State = Hig;
        break;
    case LED_GREEN:
        LED_Red_State = Hig;
        LED_Green_State = Low;
        LED_Blue_State = Hig;
        break;
    case LED_BLUE:
        LED_Red_State = Hig;
        LED_Green_State = Hig;
        LED_Blue_State = Low;
        break;
    case LED_YELLOW:
        LED_Red_State = Low;
        LED_Green_State = Low;
        LED_Blue_State = Hig;
        break;
    case LED_PURPLE:
        LED_Red_State = Low;
        LED_Green_State = Hig;
        LED_Blue_State = Low;
        break;
    case LED_CYAN:
        LED_Red_State = Hig;
        LED_Green_State = Low;
        LED_Blue_State = Low;
        break;
    case LED_WHITE:
        LED_Red_State = Low;
        LED_Green_State = Low;
        LED_Blue_State = Low;
        break;
    case LED_RGBOFF:
        LED_Red_State = Hig;
        LED_Green_State = Hig;
        LED_Blue_State = Hig;
        break;
    default:
        return;
    }
    led_device.WHT_Set_GPIO_State(LED0_GPIO_Port, LED0_GPIO_Pin, LED_Red_State);
    led_device.WHT_Set_GPIO_State(LED1_GPIO_Port, LED1_GPIO_Pin, LED_Green_State);
    led_device.WHT_Set_GPIO_State(LED2_GPIO_Port, LED2_GPIO_Pin, LED_Blue_State);
}

void WHT_LED_Init_Config(void)
{
    WHT_GPIO_Init_Config(&led_device);
    wht_gpio_init_config();
}
  1. GPIO驱动如下

GPIO头文件:

#ifndef __GPIO_BSP_H__
#define __GPIO_BSP_H__

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"

typedef enum
{
    PortA = (uint32_t)GPIOA,
    PortB = (uint32_t)GPIOB,
    PortC = (uint32_t)GPIOC,
    PortD = (uint32_t)GPIOD,
    PortE = (uint32_t)GPIOE,
    PortF = (uint32_t)GPIOF,
    PortG = (uint32_t)GPIOG,
}WHT_GPIO_Port_enum;

typedef enum
{
    Pin0  = GPIO_Pin_0,
    Pin1  = GPIO_Pin_1,
    Pin2  = GPIO_Pin_2,
    Pin3  = GPIO_Pin_3,
    Pin4  = GPIO_Pin_4,
    Pin5  = GPIO_Pin_5,
    Pin6  = GPIO_Pin_6,
    Pin7  = GPIO_Pin_7,
    Pin8  = GPIO_Pin_8,
    Pin9  = GPIO_Pin_9,
    Pin10 = GPIO_Pin_10,
    Pin11 = GPIO_Pin_11,
    Pin12 = GPIO_Pin_12,
    Pin13 = GPIO_Pin_13,
    Pin14 = GPIO_Pin_14,
    Pin15 = GPIO_Pin_15,
    PinAll =GPIO_Pin_All,
}WHT_GPIO_Pin_enum;

typedef enum
{
    Low = 0,
    Hig = !Low,
}WHT_GPIO_State_enum;

typedef enum
{
    Mode_Ain         = (uint8_t)GPIO_Mode_AIN,        //模拟输入
    Mode_IN_FLOATING = (uint8_t)GPIO_Mode_IN_FLOATING,//浮空输入
    Mode_IPD         = (uint8_t)GPIO_Mode_IPD,        //下拉输入
    Mode_IPU         = (uint8_t)GPIO_Mode_IPU,        //上拉输入
    Mode_Out_OD      = (uint8_t)GPIO_Mode_Out_OD,     //开漏输出
    Mode_Out_PP      = (uint8_t)GPIO_Mode_Out_PP,     //推挽输出
    Mode_AF_OD       = (uint8_t)GPIO_Mode_AF_OD,      //开漏复用输出
    Mode_AF_PP       = (uint8_t)GPIO_Mode_AF_PP,      //推挽复用输出
}WHT_GPIO_Mode_enum;

typedef struct
{
    void (*WHT_Set_GPIO_Mode)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_Mode_enum mode);
    void (*WHT_Set_GPIO_State)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_State_enum state);
    WHT_GPIO_State_enum(*WHT_Get_GPIO_State)(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx);
}WHT_GPIO_Device_t;

extern void WHT_GPIO_Init_Config(WHT_GPIO_Device_t* wht_gpio_device);

#endif /*__GPIO_BSP_H__*/

GPIO源文件:

#include "gpio_bsp.h"

static void wht_set_gpio_mode(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_Mode_enum mode)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    uint32_t RCC_APB2Periph;

    switch (portx)
    {
    case PortA:RCC_APB2Periph = RCC_APB2Periph_GPIOA; break;
    case PortB:RCC_APB2Periph = RCC_APB2Periph_GPIOB; break;
    case PortC:RCC_APB2Periph = RCC_APB2Periph_GPIOC; break;
    case PortD:RCC_APB2Periph = RCC_APB2Periph_GPIOD; break;
    case PortE:RCC_APB2Periph = RCC_APB2Periph_GPIOE; break;
    case PortF:RCC_APB2Periph = RCC_APB2Periph_GPIOF; break;
    case PortG:RCC_APB2Periph = RCC_APB2Periph_GPIOG; break;
    default:
        return;
    }
    RCC_APB2PeriphClockCmd(RCC_APB2Periph, ENABLE);
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode = (GPIOMode_TypeDef)mode;
    GPIO_InitStruct.GPIO_Pin = pinx;
    GPIO_Init((GPIO_TypeDef*)portx, &GPIO_InitStruct);
}
static void wht_set_gpio_state(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx, WHT_GPIO_State_enum state)
{
    if (state == Hig)
        GPIO_SetBits((GPIO_TypeDef*)portx, pinx);
    else
        GPIO_ResetBits((GPIO_TypeDef*)portx, pinx);
}
static WHT_GPIO_State_enum wht_get_gpio_state(WHT_GPIO_Port_enum portx, WHT_GPIO_Pin_enum pinx)
{
    if (GPIO_ReadInputDataBit((GPIO_TypeDef*)portx, pinx) == Bit_SET)
        return Hig;
    else
        return Low;
}

void WHT_GPIO_Init_Config(WHT_GPIO_Device_t* wht_gpio_device)
{
    wht_gpio_device->WHT_Set_GPIO_Mode = wht_set_gpio_mode;
    wht_gpio_device->WHT_Get_GPIO_State = wht_get_gpio_state;
    wht_gpio_device->WHT_Set_GPIO_State = wht_set_gpio_state;
}
  1. 最终的固件库就不贴了,基于官方固件库的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金丝草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值