当前位置:网站首页>STM32驱动继电器 STM32F103RCT6基于寄存器和库函数驱动IO口
STM32驱动继电器 STM32F103RCT6基于寄存器和库函数驱动IO口
2022-06-09 22:04:00 【优信电子】
STM32驱动继电器 STM32F103RCT6基于寄存器和库函数驱动IO口
继电器的描述

继电器是一种小电压控制大电压器件,输入端是一个开关,不会有输出电压,具体的接线方式是要看自己如何利用这个开关;
继电器具体的工作原理可以参考之前的文章“51单片机驱动继电器模块点灯”
继电器的参数测试
以我们自己店铺的继电器模块为例,继电器触发电压高于某个值时,继电器就会被触发,触发灯亮起
这里实测时1.3V左右,实际触发电压最好2V以上,5.5V以下;
当触发信号低于某个值的时候,继电器就会复原,不会被触发,触发灯熄灭,这里实测时1V,不过实际触发信号建议低于0.5V;
STM32F103寄存器版本控制IO口
1:程序现象
信号控制引脚是STM32F103RCT6的PA11引脚;
STM32控制继电器-寄存器版本
1.由STM32F10系列参考手册可知,GPIO的时钟使能是由APB2外设所控制的;
从上面的图可以看出,只需要把IOPA置为1,其他置为零,就可以打开GPIOA的使能,
2.然后是配置端口这里选为通用推挽输出,速度50MHZ;
由图可以看出CNF11配置为00,MODE11配置为11,其他置为0即可;
3.控制GPIOA11口输出;
ODR11置为1就是输出高电平;
4.代码演示,
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC->APB2ENR = 0x00000004; //打开GPIOA使能
GPIOA->CRH = 0x00003000; //配置PA11引脚为通用推挽输出模式
while(1)
{
GPIOA->ODR = 0x00000800; //PA11输出高电平
Delay_ms(1000);
GPIOA->ODR = 0x00000000; //PA11输出低电平
Delay_ms(1000);
}
}
STM32控制继电器-库函数版本
库函数是已经把寄存器都包装好了,我们直接使用即可;
主要代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //配置GPIOA外设时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //配置GPIOA为通用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //配置对应的端口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //配置好速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //传递结构体的位置
while(1)
{
GPIO_SetBits(GPIOA,GPIO_Pin_11); //端口设置为高电平
Delay_ms(1000); //延时1s
GPIO_ResetBits(GPIOA,GPIO_Pin_11); //端口设置为低电平
Delay_ms(1000); //延时1s
}
}
总结
1.寄存器版本和库函数版本代码现象是一样的;
2.IO口模式要配置为推挽输出模式,开漏输出模式无法驱动继电器;
3.本次使用的是5V继电器版本,供电需要用到5V,如果开发板没有5V 输入引脚,可以接额外电源供电,但是要把供电电源的地和单片机的地连起来;
边栏推荐
- C语言试题166之整数逆序输出
- The original tree array can be so simple?
- How to use sqlplus to remotely connect ASM instances
- JS 用正则表达式,验证密码包含数字和字母的方法
- 【Jmeter】Jmeter从入门到精通
- Explanation of leetcode UHF questions (II)
- C language 0 length array (variable array / flexible array) explanation
- 房贷利率下调 现在是买房的时机吗?
- 目前28岁,从20年开始北漂闯荡到入职软件测试,我成功捧起15K薪资
- 在 4GB 物理內存的機器上,申請 8G 內存會怎麼樣?
猜你喜欢

How to use sqlplus to remotely connect ASM instances

元宇宙并非法外之地,该如何保护个人权益?

从零开始实现lmax-Disruptor队列(二)多消费者、消费者组间消费依赖原理解析

知乎关注热度25K的问题,自学软件测试,要学到什么程度才能找到工作?
![[image reconstruction] regularization based image super-resolution reconstruction with matlab code](/img/30/9ed42f3099ddb741352e26272e9c3d.png)
[image reconstruction] regularization based image super-resolution reconstruction with matlab code

GUI 用户登录

Lua learning notes (4) -- building mobdebug remote development environment

Huawei device configuration hub and spoke

这本书押中了2022北京高考作文题

从内核代码了解SQL如何解析
随机推荐
mkdir 创建目录命令
Explication de la boucle de force (1)
Gamefi's new departure, aquanee will log in to gate and bitmart on June 9
C语言试题169之谁家孩子跑得最慢
Re summary of mongodb installation, configuration, error handling, crud operation, etc
Leetcode(力扣)超高频题讲解(二)
【轴承故障分解】基于 ITD实现轴承故障信号分解含Matlab源码
Who is the slowest child in C language test 169
Aquanee will land in gate and bitmart in the near future, providing a good opportunity for low-level layout
Understand the execution principle of show create table
Find my product | the new TWS headset supports the find my function, and apple find my is more widely used in third-party manufacturers
【蓝桥杯集训100题】scratch眩晕苹果 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第12题
At present, I am 28 years old, and I have successfully won the 15K salary since I started to wander to the north in 20 years and started to test the entry software
定义requestAnimationFrame一秒执行一次
This book has won the 2022 Beijing college entrance examination composition
veracrypt 创建文件型加密卷
SDN特有的网络安全问题
Expérience de l'optimisation des bases de données
Web3 in the distant future? No, it has come!
在 4GB 物理内存的机器上,申请 8G 内存会怎么样?