1、项目前期准备
接线如图所示:
2、这里主要用到07、08、09项目工程文件,复制09项目工程文件,重命名为10-电动车报警器项目
把08-继电器定时开闭项目中的alarm文件夹复制到10项目中
将alarm文件夹中的内容加载到项目文件中
3、代码:
exti.c
#include "exti.h"
#include "sys.h"
#include "delay.h"
#include "led.h"uint8_t buttonA_flag = FALSE; // 按键A按下的标志位
uint8_t buttonB_flag = FALSE; // 按键B按下的标志位
uint8_t vibrate_flag = FALSE; // 检测到震动标志位void exti_init(void)
{GPIO_InitTypeDef gpio_initstruct;//打开时钟__HAL_RCC_GPIOA_CLK_ENABLE(); // 使能GPIOA时钟__HAL_RCC_GPIOB_CLK_ENABLE();//按键B中断配置gpio_initstruct.Pin = GPIO_PIN_12; // 按键B对应的引脚gpio_initstruct.Mode = GPIO_MODE_IT_RISING; // 上升沿触发gpio_initstruct.Pull = GPIO_PULLDOWN; // 下拉HAL_GPIO_Init(GPIOA, &gpio_initstruct);HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0); // 设置EXTI15_10中断线的优先级HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); // 使能中断//按键A中断配置gpio_initstruct.Pin = GPIO_PIN_5; // 按键A对应的引脚gpio_initstruct.Mode = GPIO_MODE_IT_RISING; // 上升沿触发gpio_initstruct.Pull = GPIO_PULLDOWN; // 下拉HAL_GPIO_Init(GPIOB, &gpio_initstruct);HAL_NVIC_SetPriority(EXTI9_5_IRQn, 2, 0); // 设置EXTI9_5中断线的优先级HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); // 使能中断//震动传感器中断配置gpio_initstruct.Pin = GPIO_PIN_4; // 震动传感器对应的引脚gpio_initstruct.Mode = GPIO_MODE_IT_FALLING; // 下降沿触发gpio_initstruct.Pull = GPIO_PULLUP; // 上拉HAL_GPIO_Init(GPIOA, &gpio_initstruct);HAL_NVIC_SetPriority(EXTI4_IRQn, 2, 0); // 设置EXTI0中断线的优先级HAL_NVIC_EnableIRQ(EXTI4_IRQn); // 使能中断
}void EXTI15_10_IRQHandler(void)
{HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_12);
}void EXTI9_5_IRQHandler(void)
{HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
}void EXTI4_IRQHandler(void)
{HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
}void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{//delay_ms(20);if (GPIO_Pin == GPIO_PIN_12) //检测到B键按下{if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) == GPIO_PIN_SET)//led2_toggle();buttonB_flag = TRUE;}else if (GPIO_Pin == GPIO_PIN_5) //检测到A键按下{if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == GPIO_PIN_SET)//led1_toggle();buttonA_flag = TRUE;}else if (GPIO_Pin == GPIO_PIN_4) //检测到震动{if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == GPIO_PIN_RESET)//led1_toggle();vibrate_flag = TRUE;}
}uint8_t buttonA_flag_get(void)
{uint8_t temp = buttonA_flag;buttonA_flag = FALSE;return temp;
}void buttonA_flag_set(uint8_t value)
{buttonA_flag = value;
}uint8_t buttonB_flag_get(void)
{uint8_t temp = buttonB_flag;buttonB_flag = FALSE;return temp;
}void buttonB_flag_set(uint8_t value)
{buttonB_flag = value;
}uint8_t vibrate_flag_get(void)
{uint8_t temp = vibrate_flag;vibrate_flag = FALSE;return temp;}void vibrate_flag_set(uint8_t value)
{vibrate_flag = value;
}
exti.h
#ifndef __EXTI_H__
#define __EXTI_H__#include "stdint.h"#define TRUE 1
#define FALSE 0void exti_init(void);
uint8_t buttonA_flag_get(void);
void buttonA_flag_set(uint8_t value);
uint8_t buttonB_flag_get(void);
void buttonB_flag_set(uint8_t value);
uint8_t vibrate_flag_get(void);
void vibrate_flag_set(uint8_t value);#endif
main.c
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "exti.h"
#include "alarm.h"int main(void)
{HAL_Init(); /* 初始化HAL库 */stm32_clock_init(RCC_PLL_MUL9); /* 设置时钟, 72Mhz */led_init(); /* 初始化LED灯 */exti_init(); /* 初始化EXTI */alarm_init(); /* 初始化继电器 */uint8_t alert_mode = FALSE; //警戒模式标志while(1){ //A按键是否按下?if(buttonA_flag_get() == TRUE){alarm_on();delay_ms(2000);alarm_off();alert_mode = TRUE;}//B按键是否按下?if(buttonB_flag_get() == TRUE){if(alarm_status_get() == ALARM_STATUS_ON)alarm_off();else{alarm_on();delay_ms(1000);alarm_off();}alert_mode = FALSE;}//如果处于警戒模式if(alert_mode == TRUE){if(vibrate_flag_get() == TRUE)alarm_on();}elsevibrate_flag_set(FALSE);}
}