当前位置:网站首页>【浅析STM32之GPIO寄存器(CRL/CRH)配置 】
【浅析STM32之GPIO寄存器(CRL/CRH)配置 】
2022-07-25 04:14:00 【@slow_walker】
浅析STM32之GPIO寄存器(CRL/CRH)配置
一、前言
GPIO的配置可以说是STM单片机的一大特色,通过GPIO的配置我们可以进行多种不同模式的输入和输出,并且进行配置的方式也有多种,一般我们在使用的过程中可能大多数都是通过库函数来进行配置的,这样确实也比较方便,但有时候我们也不难免会使用寄存器进行配置,代码可能会更加简洁,所以掌握寄存器的配置对我们来说也很重要。
特别是在进行IIC的数据引脚输入输出配置时寄存器的作用十分重要
二、寄存器配置分析
#define IIC_INPUT_MODE_SET() {
GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=8<<28;}//改变PB7对应位为1000(CNF[1:0]MODE[1:0]),设置成上拉或者下拉输入
#define IIC_OUTPUT_MODE_SET() {
GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=3<<28;}//改变PB7对应位为0011,设置成通用推挽输出
上述代码是我在另一篇博文进行IIC的数据引脚进行输入输出配置的代码,下面我会进行相关寄存器的讲解,然后进行此代码的分析,相信大家会一目了然了。
在stm32单片机中我们可以发现,单片机的引脚分成了很多组GPIOA~GPIOE,并且每一组中又有0-15共16个引脚,每个IO口由4bit位来控制,那么一组IO就需要16*4 = 64bit,但我们使用的是32位的单片机,所以又将64Bit分成了两部分,高32bit又GPIOX_CRH寄存器来配置,低32bit由GPIOX_CRLj寄存器来进行配置.对应的引脚CRL为0到7CRH为8到15。
下面是官方数据手册的内容
GPIOX_CRL

GPIOX_CRH

从上面我们可以看出不管是GPIOX_CRL还是GPIOX_CRH每个IO都是由CNF[1:0]和MODE[1:0]两部分进行配置选择的,其中MODE[1:0]是进行配置输入还是输出一般00为输入 11为输出。CNF[1:0]则配合MODE[1:0]进行具体的模式配置。
譬如GPIOA中的GPIO_Pin_0配置上拉输入模式
CNF[1:0] 则为10 MODE[1:0] 则为00
GPIOA->CRL &= 0xfffffff0;GPIOA->CRL |= 8;
0xfffffff0先进行配置IO清零其余的保持不变,8为1000 相应的IO配置成上拉输入模式
三、代码解析
GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=8<<28;
首先我们从0x0FFFFFFF这句代码中可以看出我们是进行GPIO_Pin_7配置,先将7引脚Io的控制位清零其余的位保持不变,8 ----->1000 为CNF[1:0] 为10 上拉MODE[1:0] 为00输入则将引脚配置成上拉输入模式 8 << 28 可以看成 1111 1111 1111 1111 1111 1111 1111 1000 左移28位则变成了1000 1111 1111 1111 1111 1111 1111 1111,可以看出就是GPIO_Pin_7引脚进行配置了上拉输入模式。
同理
GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=3<<28;
首先从0X0FFFFFFF这句代码我们是进行GPIO_Pin_7配置,先将7引脚Io控制位清零其余位保持不变,3------>0011为CNF[1:0] 为00推挽,MODE[1:0]为11输出则将相应的引脚配置成推挽输出,3<<28可以看出1111 1111 1111 1111 1111 1111 1111 0011 左移28位后变成了0011 1111 1111 1111 1111 1111 1111 1111,正好是将GPIO_Pin_7设置成了推挽输出模式
上述内容为个人观点若有误,望大佬指正。

边栏推荐
- Properties of trees
- Day008 select structure (switch statement)
- Understand Huawei's 2018 financial report with one picture
- Infinite connection · infinite collaboration | the first global enterprise communication cloud conference WECC is coming
- Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
- 一般在进行数仓迁移过程中,是如何进行数据测试的?
- LVGL Switch & Table
- Maker concept design to adapt to popular education
- [daily question] 731. My schedule II
- RGB and SATA function switching module based on Quanzhi rk3568j
猜你喜欢

Simple understanding of RPC

Digital collections can go further without hype

2019 telecast retest test questions

Creativity: presentation of AI oil paintings with high imitation mineral pigments

Docker install MySQL 5.7

ThreadLocal Kills 11 consecutive questions

# 1. Excel的IF函数

Jenkins continues to integrate entry to mastery

Grafana visual configuration diagram histogram

Infinite connection · infinite collaboration | the first global enterprise communication cloud conference WECC is coming
随机推荐
Sudden! Britain accuses Huawei of major defects in its equipment (with report)
二、MySQL数据库基础
[ CTF 学习 ] CTF 中的隐写集合 —— 图片隐写术
After the failure of listing in Hong Kong, tuba rabbit terminated its gem IPO, and the founder responded
How to use ide tool hhdbcs to create a data table containing 1000 simulated data in Oracle database, and
Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
Analytic hierarchy process of MATLAB
Has baozi ever played in the multi merchant system?
@ResponseBody注解的总结
GDT,LDT,GDTR,LDTR
What are the models of asemi from the manufacturer of rectifier bridge and how about the electroplating process of the manufacturer of rectifier bridge?
LVGL 8.2 Roller
01 create project warehouse
数据中台建设(一):数据中台出现的背景
自然的状态最好
Source code analysis of ArrayList
Open source summer interview | "after 00" PMC member Bai Zeping
Interview required: how to design the seckill system?
Apache Doris series: in depth understanding of real-time analytical database Apache Doris