STM32F7xx —— 输出

STM32F7xx —— 输出

 

目录

STM32F7xx —— 输出

一、几个重要的IO口操作函数

二、几个重要的结构

三、封装两个GPIO初始化函数(普通GPIO,复用GPIO)

四、输出接口设计


 

 

一、几个重要的IO口操作函数

HAL_GPIO_Init(GPIO_TypeDef  *GPIOx, GPIO_InitTypeDef *GPIO_Init);  // GPIO初始化HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState); // 输出高低电平GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); // 读IO口电平void HAL_GPIO_TogglePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); // 电平取反

 

二、几个重要的结构

// GPIO口  GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG,GPIOH,GPIOI
typedef struct
{__IO uint32_t MODER;    /*!< GPIO port mode register,               Address offset: 0x00      */__IO uint32_t OTYPER;   /*!< GPIO port output type register,        Address offset: 0x04      */__IO uint32_t OSPEEDR;  /*!< GPIO port output speed register,       Address offset: 0x08      */__IO uint32_t PUPDR;    /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */__IO uint32_t IDR;      /*!< GPIO port input data register,         Address offset: 0x10      */__IO uint32_t ODR;      /*!< GPIO port output data register,        Address offset: 0x14      */__IO uint32_t BSRR;     /*!< GPIO port bit set/reset register,      Address offset: 0x18      */__IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address offset: 0x1C      */__IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
} GPIO_TypeDef; 
// GPIO引脚,模式,上下拉,速度,复用等
typedef struct
{uint32_t Pin;       /*!< Specifies the GPIO pins to be configured.This parameter can be any value of @ref GPIO_pins_define */uint32_t Mode;      /*!< Specifies the operating mode for the selected pins.This parameter can be a value of @ref GPIO_mode_define */uint32_t Pull;      /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.This parameter can be a value of @ref GPIO_pull_define */uint32_t Speed;     /*!< Specifies the speed for the selected pins.This parameter can be a value of @ref GPIO_speed_define */uint32_t Alternate;  /*!< Peripheral to be connected to the selected pins. This parameter can be a value of @ref GPIO_Alternate_function_selection */
}GPIO_InitTypeDef;
// GPIO引脚
#define GPIO_PIN_0                 ((uint16_t)0x0001U)  /* Pin 0 selected    */
#define GPIO_PIN_1                 ((uint16_t)0x0002U)  /* Pin 1 selected    */
#define GPIO_PIN_2                 ((uint16_t)0x0004U)  /* Pin 2 selected    */
#define GPIO_PIN_3                 ((uint16_t)0x0008U)  /* Pin 3 selected    */
#define GPIO_PIN_4                 ((uint16_t)0x0010U)  /* Pin 4 selected    */
#define GPIO_PIN_5                 ((uint16_t)0x0020U)  /* Pin 5 selected    */
#define GPIO_PIN_6                 ((uint16_t)0x0040U)  /* Pin 6 selected    */
#define GPIO_PIN_7                 ((uint16_t)0x0080U)  /* Pin 7 selected    */
#define GPIO_PIN_8                 ((uint16_t)0x0100U)  /* Pin 8 selected    */
#define GPIO_PIN_9                 ((uint16_t)0x0200U)  /* Pin 9 selected    */
#define GPIO_PIN_10                ((uint16_t)0x0400U)  /* Pin 10 selected   */
#define GPIO_PIN_11                ((uint16_t)0x0800U)  /* Pin 11 selected   */
#define GPIO_PIN_12                ((uint16_t)0x1000U)  /* Pin 12 selected   */
#define GPIO_PIN_13                ((uint16_t)0x2000U)  /* Pin 13 selected   */
#define GPIO_PIN_14                ((uint16_t)0x4000U)  /* Pin 14 selected   */
#define GPIO_PIN_15                ((uint16_t)0x8000U)  /* Pin 15 selected   */
// GPIO模式  输入 推挽输出 开漏输出 推挽复用 开漏复用
#define  GPIO_MODE_INPUT                        ((uint32_t)0x00000000U)   /*!< Input Floating Mode                   */
#define  GPIO_MODE_OUTPUT_PP                    ((uint32_t)0x00000001U)   /*!< Output Push Pull Mode                 */
#define  GPIO_MODE_OUTPUT_OD                    ((uint32_t)0x00000011U)   /*!< Output Open Drain Mode                */
#define  GPIO_MODE_AF_PP                        ((uint32_t)0x00000002U)   /*!< Alternate Function Push Pull Mode     */
#define  GPIO_MODE_AF_OD                        ((uint32_t)0x00000012U)   /*!< Alternate Function Open Drain Mode    */
// GPIO速度 低速  中速  高速  极速
#define  GPIO_SPEED_FREQ_LOW         ((uint32_t)0x00000000U)  /*!< Low speed     */
#define  GPIO_SPEED_FREQ_MEDIUM      ((uint32_t)0x00000001U)  /*!< Medium speed  */
#define  GPIO_SPEED_FREQ_HIGH        ((uint32_t)0x00000002U)  /*!< Fast speed    */
#define  GPIO_SPEED_FREQ_VERY_HIGH   ((uint32_t)0x00000003U)  /*!< High speed    */
// GPIO上下拉 无上下拉  上拉  下拉
#define  GPIO_NOPULL        ((uint32_t)0x00000000U)   /*!< No Pull-up or Pull-down activation  */
#define  GPIO_PULLUP        ((uint32_t)0x00000001U)   /*!< Pull-up activation                  */
#define  GPIO_PULLDOWN      ((uint32_t)0x00000002U)   /*!< Pull-down activation                */
// GPIO引脚高低电平
typedef enum
{GPIO_PIN_RESET = 0,GPIO_PIN_SET
}GPIO_PinState;

三、封装两个GPIO初始化函数(普通GPIO,复用GPIO)

// 这里只贴出代码片段,封装两个接口,一个普通GPIO初始化,一个带复用功能。
#define GPIO_CLK_ENABLE() do{ \__HAL_RCC_GPIOA_CLK_ENABLE(); \__HAL_RCC_GPIOB_CLK_ENABLE(); \__HAL_RCC_GPIOC_CLK_ENABLE(); \__HAL_RCC_GPIOD_CLK_ENABLE(); \__HAL_RCC_GPIOE_CLK_ENABLE(); \__HAL_RCC_GPIOF_CLK_ENABLE(); \__HAL_RCC_GPIOG_CLK_ENABLE(); \__HAL_RCC_GPIOH_CLK_ENABLE(); \__HAL_RCC_GPIOI_CLK_ENABLE(); \
} while(0);void GPIOConfig(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_CLK_ENABLE();GPIO_InitStructure.Pin = pin;GPIO_InitStructure.Mode = mode;GPIO_InitStructure.Pull = pull;GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;HAL_GPIO_Init(gpio, &GPIO_InitStructure);
}void GPIOConfigExt(GPIO_TypeDef *gpio, uint32_t pin, uint32_t mode, uint32_t pull, uint32_t alternate)
{GPIO_InitTypeDef GPIO_InitStructure;GPIO_CLK_ENABLE();GPIO_InitStructure.Pin = pin;GPIO_InitStructure.Mode = mode;GPIO_InitStructure.Pull = pull;GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;GPIO_InitStructure.Alternate = alternate;HAL_GPIO_Init(gpio, &GPIO_InitStructure);
}

四、输出接口设计

为了灵活使用,我们将输出的有效电平设置成可配置。

// 配置的有效电平 初始  低电平有效  高电平有效
typedef enum
{OUTPUT_INIT_IS_ACTIVE = 0,OUTPUT_LOW_IS_ACTIVE  = 1,OUTPUT_HIGH_IS_ACTIVE = 2,
} output_active_t;#define OUTX_CONFIG(gpio, pin)    GPIOConfig(gpio, pin, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL)
#define OUTX_READ(gpio, pin)      HAL_GPIO_ReadPin(gpio, pin)
#define OUTX_ACTIVE(gpio, pin, tag) do{ \if(OUTPUT_LOW_IS_ACTIVE == tag) \{ \HAL_GPIO_WritePin(gpio, pin, GPIO_PIN_RESET); \} \else \{ \HAL_GPIO_WritePin(gpio, pin, GPIO_PIN_SET); \} \
} while(0);
#define OUTX_NEGATIVE(gpio, pin, tag) do{ \if(OUTPUT_LOW_IS_ACTIVE == tag) \{ \HAL_GPIO_WritePin(gpio, pin, GPIO_PIN_SET); \} \else \{ \HAL_GPIO_WritePin(gpio, pin, GPIO_PIN_RESET); \} \
} while(0);#define OUT1_PORT                 GPIOA
#define OUT1_PIN                  GPIO_PIN_0#define OUT2_PORT                 GPIOA
#define OUT2_PIN                  GPIO_PIN_1#define OUT3_PORT                 GPIOA
#define OUT3_PIN                  GPIO_PIN_2
// demo代码 只传递思想 
// 封装出对外的四个接口 初始化 输出有效 输出无效 输出取反 
// 以列表的形式封装 方便增加和删除static void output_config(GPIO_TypeDef *gpio, uint16_t pin)
{OUTX_CONFIG(gpio, pin);
}static uint8_t output_is_enable(uint8_t index)
{// 输出通道可用 这里也可以变成可配置 可以配置本IO口有效和无效return 1;
}static void output_active(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag)
{OUTX_ACTIVE(gpio, pin, tag);
}static void output_negative(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag)
{OUTX_NEGATIVE(gpio, pin, tag);
}static void output_toggle(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag)
{// 这里可以不用这么麻烦,使用HAL_GPIO_TogglePinif(OUTX_READ(gpio, pin) == 0){OUTX_ACTIVE(gpio, pin, tag);}else{OUTX_NEGATIVE(gpio, pin, tag);}
}typedef struct
{GPIO_TypeDef *gpio;uint16_t pin;void (* output_config_cb)(GPIO_TypeDef *gpio, uint16_t pin);uint8_t (* output_is_enable_cb)(uint8_t index);void (* output_active_cb)(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag);void (* output_negative_cb)(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag);void (* output_toggle_cb)(GPIO_TypeDef *gpio, uint16_t pin, uint8_t tag);
} output_port_t;static output_port_t output_items[] =
{{OUT1_PORT, OUT1_PIN, output_config, output_is_enable, output_active, output_negative, output_toggle},{OUT2_PORT, OUT2_PIN, output_config, output_is_enable, output_active, output_negative, output_toggle},{OUT3_PORT, OUT3_PIN, output_config, output_is_enable, output_active, output_negative, output_toggle},
};static void output_init(uint32_t value)
{uint32_t i, mask = 1;for(i = 0; i < ARRAY_SIZE(output_items); ++i){if(value & mask){
#if(CONFIG_OUTPUT_TEST == 1)config.output.total_switch = OUTPUT_MODE_OPEN;config.output.sub_switch[i] = OUTPUT_MODE_OPEN;config.output.active_tag[i] = OUTPUT_HIGH_IS_ACTIVE;
#endifoutput_items[i].output_config_cb(output_items[i].gpio, output_items[i].pin);output_items[i].output_negative_cb(output_items[i].gpio, output_items[i].pin, config.output.active_tag[i]);}mask <<= 1;}
}void OutputHigh(uint32_t value)
{uint32_t i, mask = 1;for(i = 0; i < ARRAY_SIZE(output_items); ++i){if(value & mask){if(output_items[i].output_is_enable_cb(i)){output_items[i].output_active_cb(output_items[i].gpio, output_items[i].pin, config.output.active_tag[i]);}}mask <<= 1;}
}void OutputLow(uint32_t value)
{uint32_t i, mask = 1;for(i = 0; i < ARRAY_SIZE(output_items); ++i){if(value & mask){if(output_items[i].output_is_enable_cb(i)){output_items[i].output_negative_cb(output_items[i].gpio, output_items[i].pin, config.output.active_tag[i]);}}mask <<= 1;}
}void OutputToggle(uint32_t value)
{uint32_t i, mask = 1;for(i = 0; i < ARRAY_SIZE(output_items); ++i){if(value & mask){if(output_items[i].output_is_enable_cb(i)){output_items[i].output_toggle_cb(output_items[i].gpio, output_items[i].pin, config.output.active_tag[i]);}}mask <<= 1;}
}void OutputInit(void)
{output_init(0xFFFFFFFF);OutputLow(0xFFFFFFFF);
}

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/465648.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VS2008水晶报表发布部署总结

如果你安装了VS2008&#xff0c;那么可以找到如下目录&#xff1a; C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CrystalReports10_5 此目录下有如下文件&#xff1a; COPY到服务器上根据服务器CPU情况选择性地安装一下。 二、 将WEB项目打包成安装程…

VS2015配置环境支持opencv3库(网络方法总结)

今天安装了opencv3.4.1的版本&#xff0c;之前一直是在ubuntu上做的&#xff0c;本次在windows10上使用VS2015来开发。 VS2015是之前安装的&#xff0c;能正常的编译程序。 1. 安装opencv&#xff0c;下载opencv的exe文件&#xff0c;其他它就是一个压缩包&#xff0c;双击exe文…

话里话外: 信息化与高层参与度的关系

上周我去了一家装备制造业企业&#xff0c;客户找我们是想做梳理流程。为什么要在这个时间点去梳理流程&#xff0c;带着这个疑问&#xff0c;我去客户企业进行调研。调研得知集团已经决定&#xff0c;要在今年上一个管理系统&#xff0c;这个管理系统刚好他们的兄弟单位已经用…

STM32F7xx —— 输入

STM32F7xx —— 输入 目录 STM32F7xx —— 输入 一、输入配置 二、输入扫描 三、输入处理 一、输入配置 为了灵活使用&#xff0c;我们将输入的有效电平设置成可配置。同样是列表表示所有IO口。 // 配置有效电平 typedef enum {KEY_INIT_IS_ACTIVE 0,KEY_LOW_IS_ACTIVE …

Kernel中如何操作CPU及外设寄存器

01ARM Coretex-A9寄存器对于ARM Coretex-A9处理器而言其寄存器主要包括两大部分&#xff0c;分别是通用寄存器以及系统控制寄存器。上图所示的通用寄存器&#xff0c;主要是在代码运行过程中使用到&#xff0c;CPU通过该部分寄存器执行代码并完成相关的运算操作。对于调试过程中…

初识设计模式(装饰者模式)

前言&#xff1a;总结这两天学到的装饰者模式&#xff0c;并用java小小的实现一下。书中有写到&#xff1a;给爱用继承的人一个全新的设计眼界。&#xff08;ps&#xff0c;本文最后有个小问题待解决&#xff09; 什么是装饰者模式&#xff08;Decorator Pattern&#xff09;&a…

帮一个读者解锁手机

昨天晚上&#xff0c;深圳下了好大好大的雨&#xff0c;我还在加班的时候&#xff0c;小云就发消息说家里打雷很大&#xff0c;到了十点多&#xff0c;我打开手机准备打车&#xff0c;发现打车排队有800多人&#xff0c;然后我赶紧冲下楼&#xff0c;拿起两个雨衣&#xff0c;消…

STM32F7xx —— 96位唯一ID

STM32F7xx —— 96位唯一ID // 配置单片机型号 对外只有这个宏CONFIG_SYSTEM_HARDWARE_TYPE #define CONFIG_SYSTEM_HARDWARE_TYPE SOC_TYPE_STM32F7// SOC类型 typedef enum {SOC_TYPE_STM32F0,SOC_TYPE_STM32F1,SOC_TYPE_STM32F2,SOC_TYPE_STM32F3,SOC_TYPE_STM32F4,SOC_TY…

我喜欢这样的老大[10-24]

据说原图是奥巴马和一个小朋友打雪仗&#xff0c;下面的图片是众多恶搞作品中的一个。也是我最喜欢的一个&#xff0c;因为总统先生可爱&#xff0c;猫更可爱。。。。我喜欢这样的老大&#xff0c;一把年纪了还保有童真非常难得&#xff1b;作为一位总统&#xff0c;愿意把童真…

知识点:Mysql 数据库索引优化实战(4)

知识点&#xff1a;Mysql 索引原理完全手册(1) 知识点&#xff1a;Mysql 索引原理完全手册(2) 知识点&#xff1a;Mysql 索引优化实战(3) 知识点&#xff1a;Mysql 数据库索引优化实战(4) 一&#xff1a;插入订单 业务逻辑&#xff1a;插入订单数据&#xff0c;为了避免重复导单…

看漫画学电子,非常精彩!有些概念以前模糊现在真的懂了

来源&#xff1a;gadgetronicx.com&#xff0c;排版&#xff1a;晓宇微信公众号&#xff1a;芯片之家&#xff08;ID&#xff1a;chiphome-dy&#xff09;1、按键消抖&#xff0c;在机械按键断开与闭合时&#xff0c;按键的触电是有一点弹性的&#xff0c;按下去的时候不会马上…

STM32F7xx —— 串口通信

STM32F7xx —— 串口通信 目录 STM32F7xx —— 串口通信 一、串口初始化过程 二、几个重要的串口函数 三、几个重要的结构 四、基本接口设计 一、串口初始化过程 1、时钟使能&#xff1b; 2、GPIO初始化&#xff1b; 3、串口波特率设置&#xff1b; 4、串口控制&#…

一个跳楼博士生的遗书:这个世界是一沟绝望的死水

o(︶︿︶)o 唉北邮跳楼博士生给母亲堵塞遗书&#xff1a;这个世界是一沟绝望的死水 这个世界是一沟绝望的死水&#xff0c;我在这里再怎么折腾也激不起半点涟漪。所有的努力都会被既得利益集团踩在脚下&#xff0c;所有的奋斗都面临着举步维艰。冷漠的人&#xff0c;谢谢你…

jquery插件课程1 幻灯片、城市选择、日期时间选择、拖放、方向拖动插件

jquery插件课程1 幻灯片、城市选择、日期时间选择、拖放、方向拖动插件 一、总结 一句话总结&#xff1a;都是jquery插件&#xff0c;都还比较小&#xff0c;参数&#xff08;配置参数、数据&#xff09;一般都是通过json传递。 1、插件配置数据或者参数的时候用的是什么数据结…

由c语言转向c++,我们需要做什么?

点击上方蓝字添加关注在此送大家一份小礼物&#xff0c;公众号内回复linux0001即可获得一本Linux电子教程“c语言和c到底有什么不同和联系呢&#xff1f;”毫无疑问&#xff0c;c语言和c是两种不同的语言&#xff0c;但是又有着千丝万缕的联系。语法上c语言与c一脉相承&#xf…

黄聪:【强烈推荐】搜索引擎排名决定一切吗!

在点石看到一篇《搜索引擎排名决定一切么》&#xff0c;作者cqqc626。写的太赞的&#xff0c;Kyw看后都有点激动&#xff0c;希望天下所有要需要SEO服务的客户们&#xff0c;都能明白其中道理&#xff0c;明白排名不是一切。正文如下&#xff1a; 前段时间经常看到很多和自己差…

STM32F7xx —— CAN通信

STM32F7xx —— CAN通信 目录 STM32F7xx —— CAN通信 一、CAN基础 二、几个重要的CAN函数 三、几个重要的结构 四、接口设计 一、CAN基础 差分信号&#xff1a;显性电平对应逻辑0&#xff0c;CAN_H和CAN_L差为2.5V&#xff1b;隐形电平对应逻辑1&#xff0c;CAN_H和CAN_…

[leetcode] 14. 最长公共前缀

14. 最长公共前缀 超级简单。。。 class Solution { public:string longestCommonPrefix(vector<string> &strs) {if (strs.size() 0) {return "";}string common strs[0];for (int i 1; i < strs.size(); i) {common findCommon(strs[i], common);…

Cmake确实应该用到的时候再学

最近在做项目用到Cmake相关的知识&#xff0c;之前做的是BSP驱动开发&#xff0c;基本用不到Cmake&#xff0c;唯一和Cmake有交集的应该是我们移植网关项目&#xff0c;不过也只是修修改改&#xff0c;直到最近用到Cmake开发项目&#xff0c;才真正是接触了这个东西。前天加载一…

手机吞吃蛇游戏的设计与开发

为什么80%的码农都做不了架构师&#xff1f;>>> J2ME(Java 2 Micro Edition) 是近年来随着各种不同设备&#xff0c;尤其是移动通信设备的飞速发展而诞生的一项新的开发技术。它定位在消费性电子产品的应用上&#xff0c;对设备的智能化、www.21cnlunwen.com Write…