当前位置:网站首页>STM32 - switch of relay control lamp
STM32 - switch of relay control lamp
2022-07-03 01:36:00 【Spade fish】
STM32—— The relay controls the switch of the light
List of articles
The principle of relay will not be introduced
The relay controls the switch of the light
Description of project :
Environmental Overview :
Windows10 pro
MDK—Lite 5.25
The beginning of the project :
In the firmware library USER Middle folder
add to XXX Folder Add... To this folder xx.c Files and xx.h file
Then import it in
First step :

The second step :

stay led.c Import in file led.h file , Write the include header file first , To compile , It is automatically imported

1、 To configure GPIOA The clock
To configure GPIOA The clock Want to go RCC Go looking for APB2 Configured clock
How to find it ???
open FWLB file
Find the file in the figure

Keep looking for rcc.h file

//APB2 The peripheral clock of , There are two parameters , The first parameter is IO mouth GPIOA, The second parameter open
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
How to find the first parameter ?
Continue to rcc.h Look for this parameter in the file

How to find the second parameter ?


2、GPIOA3 Structure configuration of
Let's find out GPIOA Configuration file for
Go find gpio.h file
Pictured :

Code :
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;

Code :
typedef enum
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;

Code :
typedef enum
{
GPIO_Mode_AIN = 0x0,// Analog input
GPIO_Mode_IN_FLOATING = 0x04,// Floating input
GPIO_Mode_IPD = 0x28,// Drop down input
GPIO_Mode_IPU = 0x48,// Pull up input
GPIO_Mode_Out_OD = 0x14,// Open drain output
GPIO_Mode_Out_PP = 0x10,// Push pull output
GPIO_Mode_AF_OD = 0x1C,// Push pull multiplexing
GPIO_Mode_AF_PP = 0x18// Open drain multiplexing
}GPIOMode_TypeDef;
GPIO Differences between various port modes
(1)GPIO_Mode_AIN Analog input
(2)GPIO_Mode_IN_FLOATING Floating input
(3)GPIO_Mode_IPD Drop down input
(4)GPIO_Mode_IPU Pull up input
(5)GPIO_Mode_Out_OD Open drain output
(6)GPIO_Mode_Out_PP Push pull output
(7)GPIO_Mode_AF_OD Reuse open drain output
(8)GPIO_Mode_AF_PP Multiplexing push pull output
Relevant configurations of pins

Code :
#define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */
#define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */
#define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */
#define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */
#define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */
#define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */
#define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */
#define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */
#define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */
#define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */
#define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */
#define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */
#define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */
#define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */
#define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */
#define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */
#define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */
//2、GPIOA3 Structure configuration of
Relay_GPIOInit.GPIO_Mode = GPIO_Mode_Out_PP;
Relay_GPIOInit.GPIO_Pin = GPIO_Pin_3;
Relay_GPIOInit.GPIO_Speed = GPIO_Speed_10MHz;
3、 initialization
stay GPIO.h Find the function in the file

Code after configuration :
#include "stm32f10x.h"
#include "Relay.h"
void Relay_Init(void)
{
/* Connect that IO Just find the corresponding clock and open it , We connect externally PA3, So open GPIOA The clock */
//1、 Can make GPIOA The clock
GPIO_InitTypeDef Relay_GPIOInit;
//APB2 The peripheral clock of , There are two parameters , The first parameter is IO mouth GPIOA, The second parameter open
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
//2、GPIOA3 Structure configuration of
Relay_GPIOInit.GPIO_Mode = GPIO_Mode_Out_PP;
Relay_GPIOInit.GPIO_Pin = GPIO_Pin_3;
Relay_GPIOInit.GPIO_Speed = GPIO_Speed_10MHz;
//3、 initialization
GPIO_Init(GPIOA,&Relay_GPIOInit );
}
After writing the document , stay main.c Add... To the file The header file And the path

stay main Set low level in function
GPIO_ResetBits(GPIOA, GPIO_3);
Code :
void GPIO_DeInit(GPIO_TypeDef* GPIOx);
void GPIO_AFIODeInit(void);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);// Pull high
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);// Pull low
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
void GPIO_EventOutputCmd(FunctionalState NewState);
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);
Project code :
main.c File code :
#include "stm32f10x.h"
#include "led.h"
#include "Relay.h"
int main(void)
{
LED_Init();
Relay_Init();
while(1){
GPIO_ResetBits(GPIOA, GPIO_Pin_3);
}
}
led.c Code :
#include "led.h"
#include "stm32f10x.h"
void LED_Init(void)
{
GPIO_InitTypeDef init_led;
//1、 Can make APB2 The clock of GPIOC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
//2、GPIO_OUT Set the output pin
init_led.GPIO_Mode=GPIO_Mode_Out_PP;
init_led.GPIO_Pin=GPIO_Pin_13;
init_led.GPIO_Speed=GPIO_Speed_10MHz;
//3、GPIO_LOW Set the low-level pin
GPIO_Init(GPIOC, &init_led);
}
void delayTime(uint16_t time)
{
uint16_t i=0;
while(time--)
{
i=12000;
while(i--);
}
}
/* void main_led() { LED_Init(); while(1) { GPIO_ResetBits(GPIOC, GPIO_Pin_13); delayTime(1000); GPIO_SetBits(GPIOC, GPIO_Pin_13); delayTime(1000); for(int i=0;i<=5;i++){ GPIO_ResetBits(GPIOC, GPIO_Pin_13); delayTime(100); GPIO_SetBits(GPIOC, GPIO_Pin_13); delayTime(100); } GPIO_ResetBits(GPIOC, GPIO_Pin_13); delayTime(1000); GPIO_SetBits(GPIOC, GPIO_Pin_13); delayTime(1000); } } */
led.h Code :
#include "stm32f10x.h"
void LED_Init(void);
void delayTime(uint16_t time);
Relay.c Code :
#include "stm32f10x.h"
#include "Relay.h"
void Relay_Init(void)
{
/* Connect that IO Just find the corresponding clock and open it , We connect externally PA3, So open GPIOA The clock */
//1、 Can make GPIOA The clock
GPIO_InitTypeDef Relay_GPIOInit;
//APB2 The peripheral clock of , There are two parameters , The first parameter is IO mouth GPIOA, The second parameter open
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA,ENABLE);
//2、GPIOA3 Structure configuration of
Relay_GPIOInit.GPIO_Mode = GPIO_Mode_Out_PP;
Relay_GPIOInit.GPIO_Pin = GPIO_Pin_3;
Relay_GPIOInit.GPIO_Speed = GPIO_Speed_10MHz;
//3、 initialization
GPIO_Init(GPIOA,&Relay_GPIOInit );
}
Relay.h Code :
#ifndef _RELAY_H_
#define _RELAY_H_
#include "stm32f10x.h"
void Relay_Init(void);
#endif
summary :
On the way to study , Don't learn too fast , Watching videos is very enjoyable , But pay attention to the actual combat and record the content in any video
And my own experience
边栏推荐
- Mathematical knowledge: step Nim game game game theory
- 2022 cable crane driver examination registration and cable crane driver certificate examination
- Database SQL language 01 where condition
- CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
- Wireshark data analysis and forensics a.pacapng
- Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
- [principles of multithreading and high concurrency: 2. Solutions to cache consistency]
- 强化学习 Q-learning 实例详解
- uniapp组件-uni-notice-bar通告栏
- MySQL basics 03 introduction to MySQL types
猜你喜欢

CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr

Give you an array numbers that may have duplicate element values. It was originally an array arranged in ascending order, and it was rotated once according to the above situation. Please return the sm
![[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge](/img/ed/23331d939c9338760e426d368bfd5f.png)
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge

Using tensorboard to visualize the model, data and training process

Tâche 6: regroupement DBSCAN

MySQL foundation 04 MySQL architecture

MySQL - database query - basic query

C#应用程序界面开发基础——窗体控制(2)——MDI窗体

简易分析fgui依赖关系工具

LeetCode 987. Vertical order transverse of a binary tree - Binary Tree Series Question 7
随机推荐
C#应用程序界面开发基础——窗体控制(4)——选择类控件
Tp6 fast installation uses mongodb to add, delete, modify and check
uniapp组件-uni-notice-bar通告栏
Database SQL language 02 connection query
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
MySQL basics 03 introduction to MySQL types
【面试题】1369- 什么时候不能使用箭头函数?
VIM 9.0 is officially released! The execution speed of the new script can be increased by up to 100 times
Installation and use of serial port packet capturing / cutting tool
Pytest learning notes (12) -allure feature · @allure Step () and allure attach
MySQL --- 数据库查询 - 基本查询
Top ten regular spot trading platforms 2022
数学知识:台阶-Nim游戏—博弈论
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
Common English Vocabulary
[data mining] task 6: DBSCAN clustering
简易分析fgui依赖关系工具
View of MySQL
[data mining] task 5: k-means/dbscan clustering: double square