当前位置:网站首页>[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
2022-06-13 01:50:00 【It's Beichen bupiacra】
Use library functions and registers to light up the lights respectively
One 、 Use register to light up
Hardware analysis : First of all, we have to look at the schematic diagram to find the corresponding GPIO mouth 

Then go to the register used
First of all, let's take a look at GPIO Port is attached to AHB1 above , First enable GPIOB The clock of 
Here is RCC AHB1 The register of 
Then take a look at the configuration GPIO Port register 






Let's use the above schematic diagram and register to write our lighting LED Code for , If the establishment of the project is not possible, you can take a look at the previous blog
Build one first led.h and led.c Don't put the papers in user The folder inc and src
led.h The code in
#ifndef _LED_H
#define _LED_H
#include "stm32f4xx.h"
void Led_Config(void);
#endif
led.c The code in
Mainly the configuration register
#include "led.h"
/* The functionality :LED initialization Shape parameter :void Return value :void explain : 1、 open GPIOB The clock 2、PB8 ----- Universal push-pull mode PB9 ----- Universal push-pull mode */
void Led_Config(void)
{
RCC->AHB1ENR |= 1 << 1; // open GPIOB The clock of
GPIOB->MODER &=~ (3 << 16);
GPIOB->MODER |= (1 << 16);
GPIOB->OTYPER &=~ (1 << 8);
GPIOB->OSPEEDR |= 2 << 16;
GPIOB->PUPDR &=~ (3 << 16);
GPIOB->ODR |= 1 << 8;// Default off
GPIOB->MODER &=~ (3 << 18);
GPIOB->MODER |= (1 << 18);
GPIOB->OTYPER &=~ (1 << 9);
GPIOB->OSPEEDR |= 2 << 18;
GPIOB->PUPDR &=~ (3 << 18);
GPIOB->ODR |= 1 << 9;// Default off
}
main.c The code in
#include "main.h"
int main()
{
Led_Config();//LED initialization
while(1)
{
/* * The register is lit LED(PB8、PB9) */
GPIOB->ODR &=~ (1 << 8);//LED1 bright
GPIOB->ODR &=~ (1 << 9);//LED2 bright
GPIOB->ODR &=(1 << 8);//LED1 destroy
GPIOB->ODR &=(1 << 9);//LED2 destroy
}
}
main.h The code in
#ifndef _MAIN_H
#define _MAIN_H
#include "stm32f4xx.h"
#include "led.h"
#endif
The two blue ones are the lights on 
Two 、 Use library functions to light up
The project is the same as the above except that the code has changed , Instead, call the library function
led.h The code in
#ifndef _LED_H
#define _LED_H
#include "stm32f4xx.h"
void Led_Config(void);
#endif
led.c The code in
#include "led.h"
/* The functionality :LED initialization Shape parameter :void Return value :void explain : 1、 open GPIOB The clock 2、PB8 ----- Universal push-pull mode PB9 ----- Universal push-pull mode */
void Led_Config(void)
{
/* * Library function version */
GPIO_InitTypeDef GPIO_InitStruct;// Declare struct variables
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);// open GPIOB The clock of
// initialization PB8 and PB9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;// General output
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;// Push pull output
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;// Pin 8
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
GPIO_InitStruct.GPIO_Speed = GPIO_Fast_Speed;// Speed 50Mhz
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_SetBits(GPIOB, GPIO_Pin_8);// Default off
GPIO_SetBits(GPIOB, GPIO_Pin_9);// Default off
}
main.h The code in
#ifndef _MAIN_H
#define _MAIN_H
#include "stm32f4xx.h"
#include "led.h"
#endif
main.c The code in
#include "main.h"
int main()
{
Led_Config();//LED initialization
while(1)
{
/* * Library functions are lit LED(PB8、PB9) */
// Method 1 does not use macro definitions
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
GPIO_SetBits(GPIOB, GPIO_Pin_8);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
}
}
The two blue ones are the lights on 
The blog will be written here first , Next, let's write about the operation of keys , If this blog post is helpful to you, please pay attention to it 、 give the thumbs-up 、 Collect it , Thank you for your support !
边栏推荐
- Establishment of microservice development environment
- 开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
- Devaxpress Chinese description --tdximageslider (picture rotation control)
- csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
- 水管工游戏
- cin,cin. get(),cin. Summary of the use of getline() and getline()
- Detailed understanding of white noise
- Vs how to enter chromium subprocess debugging
- Tweets movement description and chart display
- This of phaser3 add. sprite
猜你喜欢

Uuid/guid introduction, generation rules and generation codes

万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
![[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx](/img/e9/4e08e07c2de2f99c2938e79f7f1c44.png)
[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx

Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)

Magics 23.0如何激活和使用视图工具页的切片预览功能

leetcode743. Network latency (medium, Dijkstra)

LabVIEW大型项目开发提高质量的工具

Tweets movement description and chart display

Record the VMware installation process of VMware Tools and some problems encountered

Alertwindowmanager pop up prompt window help (Part 1)
随机推荐
Implementation of pointer linked list
Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
How does Google's audience work?
About inquirerjs
水管工遊戲
Devaxpress Chinese description --tdximageslider (picture rotation control)
Audiences with similar interests
6、 Implementation of warehouse out management function
受众群体应该选择观察模式还是定位模式?
移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式
谷歌的受众群体是如何发挥作用的?
Qt实现思维导图功能(二)
五、库存查询功能的完善
Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
Cmake has no obvious error after compilation, but prompts that pthread cannot be found
关于tkinter.Canvas 不显示图片的问题
Delphi 10.4.2 release instructions and installation methods of three patches
Restful interface specification annotation of pringboot (2)
详细受众特征详细解释
leetcode743. 网络延迟时间(中等, dijkstra)