stm32学习记录——继电器

STM32控制继电器

jdq.c

#include "jdq.h"

void relay_init(void)
{
  GPIO_InitTypeDef     GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}          

void relay_on(void)
{
  GPIO_SetBits(GPIOB,GPIO_Pin_7);
}

void relay_off(void)
{ 
  GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
	

其中relay_on和relay_off就是我们控制继电器开关的函数,只要在需要的地方调用这两个函数即可,其原理就是控制IO口输出高低电平。

jdq.h

#ifndef __JDQ_H
#define __JDQ_H
#include "sys.h"

void relay_init(void);
void relay_on(void);
void relay_off(void);
#endif

继电器可以用来做什么呢?

我第一次用继电器是用来控制电子锁。

附上我当时调试使用的源码供移植。

链接:https://pan.baidu.com/s/1X25h8GX0_2V0qczXfvMF4w 
提取码:9438

猜你喜欢

转载自blog.csdn.net/sujiaxin12/article/details/123715049