1.硬件
2.软件
继电器模块alarm驱动文件添加 GPIO常用函数 main.c程序
# include "sys.h"
# include "delay.h"
# include "led.h"
# include "alarm.h"
int main ( void )
{
HAL_Init ( ) ;
stm32_clock_init ( RCC_PLL_MUL9) ;
led_init ( ) ;
alarm_init ( ) ;
while ( 1 )
{
alarm_on ( ) ;
delay_ms ( 500 ) ;
alarm_off ( ) ;
delay_ms ( 500 ) ;
}
}
# include "alarm.h"
# include "sys.h"
void alarm_init ( void )
{
GPIO_InitTypeDef gpio_initstruct;
__HAL_RCC_GPIOB_CLK_ENABLE ( ) ;
gpio_initstruct. Pin = GPIO_PIN_7;
gpio_initstruct. Mode = GPIO_MODE_OUTPUT_PP;
gpio_initstruct. Pull = GPIO_PULLUP;
gpio_initstruct. Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init ( GPIOB, & gpio_initstruct) ;
alarm_off ( ) ;
}
void alarm_on ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_7, GPIO_PIN_RESET) ;
}
void alarm_off ( void )
{
HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_7, GPIO_PIN_SET) ;
}
uint8_t alarm_status_get ( void )
{
return ( uint8_t ) HAL_GPIO_ReadPin ( GPIOB, GPIO_PIN_7) ;
}
# ifndef __ALARM_H__
# define __ALARM_H__
# include "stdint.h"
# define ALARM_STATUS_ON 0
# define ALARM_STATUS_OFF 1
void alarm_init ( void ) ;
void alarm_on ( void ) ;
void alarm_off ( void ) ;
uint8_t alarm_status_get ( void ) ;
# endif
3.实物效果
硬件模块接线 继电器I/O—>PB7 继电器VCC—>3.3V 继电器GND—>GND ST-Link下载方式实验现象 继电器循环500ms断开,500ms闭合。