Stm32F103面向对象编程之GPIO
STM32F103 GPIO面向对象变成实例
创建一个GPIO对象
GpioClass gStateLed = NEW_GPIO(C, 0, GPIO_Mode_Out_PP);
初始化对象
GpioConfig(&gStateLed);
使用对象
GPIO_TOGGLE(gStateLed);
Gpio.c
/******************** (C) COPYRIGHT 2017 *********************************** File name: Description: Author: duking Version: 1.0.0 Date: 2017.2.4 History: *****************************************************************************/ #include "Gpio.h" /******************************************************************************* * Function Name : * Description : * Input : None * Output : None * Return : None *******************************************************************************/ void GpioConfig(GpioClass* gpio) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(gpio->rcc_mask, ENABLE); GPIO_InitStructure.GPIO_Pin = gpio->pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = gpio->mode; GPIO_Init(gpio->port, &GPIO_InitStructure); } /******************************************************************************* * Function Name : * Description : * Input : None * Output : None * Return : None *******************************************************************************/ void GpioListConfig(GpioClass* gpio_list, uint8_t len) { for(int i = 0; i < len; i ++){ GpioConfig(&(gpio_list[i])); } } /******************************************************************************* * Function Name : * Description : * Input : None * Output : None * Return : None *******************************************************************************/ void GpioConfigLowSpeed(GpioClass* gpio) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(gpio->rcc_mask, ENABLE); GPIO_InitStructure.GPIO_Pin = gpio->pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = gpio->mode; GPIO_Init(gpio->port, &GPIO_InitStructure); } /******************* (C) COPYRIGHT 2017 *******END OF FILE********************/
Gpio.h
/******************** (C) COPYRIGHT 2017 *********************************** File name: Description: Author: duking Version: 1.0.0 Date: 2017.2.4 History: *****************************************************************************/ #ifndef CONFIG_GPIO_H_ #define CONFIG_GPIO_H_ #include "stm32f10x.h" #ifdef __cplusplus extern "C" { #endif typedef struct { GPIO_TypeDef* port; uint16_t pin; uint32_t rcc_mask; GPIOMode_TypeDef mode; } GpioClass; #define NEW_GPIO(port_, pin_, mode_) { \ .port = GPIO##port_, \ .pin = GPIO_Pin_##pin_, \ .rcc_mask = RCC_APB2Periph_GPIO##port_, \ .mode = mode_ \ } #define NEW_GPIO_NOMODE(port_, pin_) { \ .port = GPIO##port_, \ .pin = GPIO_Pin_##pin_, \ .rcc_mask = RCC_APB2Periph_GPIO##port_ \ } void GpioConfig(GpioClass* gpio); void GpioListConfig(GpioClass* gpio_list, uint8_t len); void GpioConfigLowSpeed(GpioClass* gpio); #define GPIO_NO_REMAP (uint32_t)-1 #define GPIO_SET(GPIO) GPIO_WriteBit((GPIO).port, (GPIO).pin, Bit_SET) #define GPIO_RESET(GPIO) GPIO_WriteBit((GPIO).port, (GPIO).pin, Bit_RESET) #define GPIO_TOGGLE(GPIO) GPIO_WriteBit((GPIO).port, (GPIO).pin, (BitAction)!GPIO_ReadOutputDataBit((GPIO).port, (GPIO).pin)) #define GPIO_READ(GPIO) GPIO_ReadInputDataBit((GPIO).port, (GPIO).pin) #ifdef __cplusplus } #endif #endif /******************* (C) COPYRIGHT 2017 *******END OF FILE********************/