stm32 learning record - relay

STM32 control relay

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);
}
	

Among them, relay_on and relay_off are the functions for us to control the relay switch, as long as these two functions are called where needed, the principle is to control the output level of the IO port.

jdq. h

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

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

What can relays be used for?

 

The first time I used a relay was to control an electronic lock.

Attach the source code I used for debugging at that time for transplantation.

Link: https://pan.baidu.com/s/1X25h8GX0_2V0qczXfvMF4w 
Extraction code: 9438

Guess you like

Origin blog.csdn.net/sujiaxin12/article/details/123715049