当前位置:网站首页>STM32 learning note 1 - the simplest GPIO
STM32 learning note 1 - the simplest GPIO
2022-04-22 06:05:00 【Lithium salt block】
STM32 Learning notes 1—— The simplest GPIO
The previous study refers to 《STM32 Beginner's Guide 》
One 、 To configure GPIO
LED The partial schematic diagram is shown in the figure below :

Now configure LED0(PA8) This pin is illustrated as an example .
1. Define initialization structure variables
GPIO_InitTypeDef GPIO_InitStrcture;
Go to the definition of structure :
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
2. Can make IO Port clock
// Can make PA The peripheral clock of
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
Go to function definition to learn , The first parameter of this function is used to specify which group to open IO The clock of the port , The second parameter is set to enable or disable .
3. To configure IO Port pin
// To configure PA The pin of the port
GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_8;
4. To configure IO Port input / output mode
// Configured to output push-pull mode
GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP;
5. To configure IO Port output speed
// Can choose 10Mhz、20Mhz、50Mhz
GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz;
6. Initialize the corresponding register
// initialization A Port corresponding register
GPIO_Init(GPIOA,&GPIO_InitStrcture);
LED0 and LED1 Two pin configuration
led.h
#ifndef __LED_H
#define __LED_H
#include "stm32f10x.h"
#define LED0_OFF GPIO_SetBits(GPIOA,GPIO_Pin_8)
#define LED0_ON GPIO_ResetBits(GPIOA,GPIO_Pin_8)
#define LED0_REV GPIO_WriteBit(GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)))
#define LED1_OFF GPIO_SetBits(GPIOB,GPIO_Pin_11)
#define LED1_ON GPIO_ResetBits(GPIOB,GPIO_Pin_11)
#define LED1_REV GPIO_WriteBit(GPIOB,GPIO_Pin_11,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_11)))
void LED_GPIO_Config(void);
#endif /*__LED_H */
led.c
#include "led.h"
#include "stm32f10x.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStrcture;
//set PA8
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//clk
GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_8; //Pin
GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz; //speed
GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP; //mode
GPIO_Init(GPIOA,&GPIO_InitStrcture); //init
//set PB11
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//clk
GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_11; //Pin
GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz; //speed
GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP; //mode
GPIO_Init(GPIOB,&GPIO_InitStrcture); //init
}
Two 、 Key input control LED The light of the sun

1. Key input configuration
Key.h
#ifndef __KEY_H
#define __KEY_H
#define KEY1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)
#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)
#define KEY3 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
void Key_GPIO_Config(void);
unsigned char KEY_Scan(void);
unsigned char KEY1_Scan(void);
unsigned char KEY2_Scan(void);
unsigned char KEY3_Scan(void);
#endif /* __KEY_H */
Key.c
#include "stm32f10x.h"
#include "Key.h"
#include "delay.h"
void Key_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//PC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
unsigned char KEY_Scan(void)
{
unsigned char key_code;
if(KEY1_Scan()==1) key_code=1;
else if(KEY2_Scan()==1) key_code=2;
else if(KEY3_Scan()==1) key_code=3;
else key_code=0;
return key_code;
}
unsigned char KEY1_Scan(void)
{
static char key_up1=0;
if(KEY1==0)
{
Delay(10000);
if(KEY1==0)
{
key_up1=1;
}
}
if(KEY1==1&&key_up1==1)
{
key_up1=0;
return 1;
}
return 0;
}
unsigned char KEY2_Scan(void)
{
static char key_up2=0;
if(KEY2==0)
{
Delay(10000);
if(KEY2==0)
{
key_up2=1;
}
}
if(KEY2==1&&key_up2==1)
{
key_up2=0;
return 1;
}
return 0;
}
unsigned char KEY3_Scan(void)
{
static char key_up3=0;
if(KEY3==0)
{
Delay(10000);
if(KEY3==0)
{
key_up3=1;
}
}
if(KEY3==1&&key_up3==1)
{
key_up3=0;
return 1;
}
return 0;
}
2. Realization function
When WK UP Press down ,LED0、LED1 At the same time, reverse ;
When KEY0 Press down ,LED0 Take the opposite ;
When KEY1 Press down ,LED1 Take the opposite .
The main function :
#include "stm32f10x.h"
#include "led.h"
#include "Key.h"
int main(void)
{
unsigned char value=0;
LED_GPIO_Config();
Key_GPIO_Config();
while(1)
{
value =KEY_Scan();
if(value==1)
{
LED0_REV;
}
else if(value==2)
{
LED1_REV;
}
else if(value==3)
{
LED0_REV;
LED1_REV;
}
}
}
The learning
Mainly through training STM32GPIO Simplest configuration . Understand what the code is doing , And according to their own learning board to modify the code is the biggest progress in the near future , Continue to work hard .
版权声明
本文为[Lithium salt block]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220539557165.html
边栏推荐
- Blue Bridge Cup Sprint - binary enumeration
- flask 【GET/POST】数据处理
- dlopen调用动态库
- TXT文本内容逐个删除
- Chapter 89 leetcode refers to offer dynamic programming (6) translating numbers into strings
- Blue Bridge Cup 31 day sprint Day8
- After compiling the official program of punctual atom stm32f429, it cannot be downloaded with j-link
- oracle使用c语言编写自定义函数
- c语言开发postgres自定义函数
- Leetcode: Sword finger offer 29 Print the matrix clockwise
猜你喜欢

LeetCode: 剑指 Offer 29. 顺时针打印矩阵.

stm32单片机与LD3320语音模块交互法二

Blue Bridge Cup embedded expansion board learning nixie tube

Part 90 leetcode refers to the longest substring of offer dynamic programming (VII) that does not contain duplicate characters

QT信号与槽的特点和用法

Blue Bridge Cup 31 day sprint Day17

Pytorch deep learning practice_ 10 basis of convolutional neural network CNN

14 - container - tuple

Software test classification

03-pycharm
随机推荐
记录AD软件学习之坑
Part 84 leetcode sword refers to offer dynamic programming (I) Fibonacci sequence
第73篇 LeetCode题目练习(六) 6.Z字形变换
wgs84坐标转换,地图拾取wgs84坐标工具推荐
Blue Bridge Cup 31 day sprint day18
PyGame simple aircraft war
The postgraduate entrance examination is over
蓝桥杯嵌入式省赛第七届:模拟液位检测告警系统”
Subsets and problems (backtracking & branch and bound)
Apple CMS sets the local player ckplayer (version: ckplayerx)
Record the pit of ad software learning
13 - container - List
DS18B20 of Blue Bridge Cup embedded expansion board learning
stm32学习笔记5——RGB屏相对位置计算
oracle使用c语言编写自定义函数
时钟
Blue Bridge Cup 31 day sprint day4
第89篇 LeetCode剑指Offer动态规划(六)把数字翻译成字符串
编译openssl-0.9.8e报错out range of signed 32bit displacement
B / S architecture