先贴出最终效果
STM32_Pin PC7 (GPIOC,7 );
//LED灯闪烁效果
PC7.Init(IO_Mode_Output);
while(1)
{
PC7.Set();//引脚置高电平
delay(500);
PC7.Clr();//引脚置低电平
delay(500);
}
接下来是引脚类的定义
#include <stdint.h>
#include "STM32头文件"
class STM32_Pin
{
GPIO_TypeDef* const GPIOx;
uint16_t const pos;
uint16_t const mask;
public:
STM32_Pin(GPIO_TypeDef* GPIOx,uint16_t pos);
//IO控制
void Init(IO_Mode mode); //初始化
void Set(); //置高电平
void Clr(); //置低电平
bool Input(); //读输入电平
void SetMode(IO_Mode mode); //设置输入输出模式
};
这个类的定义适用于STM32多种系列,不管是F1,F4还是F0系列都可以用,只是具体的实现方式略有不同
GPIOx对应的是STM32的GPIO组也就是GPIOA,GPIOB...
pos对应的是该组的第n个引脚
这两个参数用来构造引脚类对象,构造方式如下
STM32_Pin PC7 (GPIOC,7 );