本傳感器我分為使用外部中斷與不使用外部中斷
因為和按鍵原理差不多,所以移植修改按鍵即可
原理圖
main.c
/**
* @brief 主函數(shù)
* @param 無
* @retval 無
*/
int main(void)
{
/* LED 端口初始化 */
LED_GPIO_Config();
/* 初始化EXTI中斷,按下按鍵會觸發(fā)中斷,
* 觸發(fā)中斷會進入stm32f4xx_it.c文件中的函數(shù)
* KEY1_IRQHandler和KEY2_IRQHandler,處理中斷,反轉(zhuǎn)LED燈。
*/
EXTI_Key_Config();
/* 等待中斷,由于使用中斷方式,CPU不用輪詢按鍵 */
while(1)
{
}
}
stm32f103x_it.c
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTI
AL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "bsp_led.h"
#include "bsp_exti.h"
/** @addtogroup STM32F10x_StdPeriph_Template
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Hard Fault exception.
* @param None
* @retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles PendSVC exception.
* @param None
* @retval None
*/
void PendSV_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
}
void KEY1_IRQHandler(void)
{
//確保是否產(chǎn)生了EXTI Line中斷
if(EXTI_GetITStatus(KEY1_INT_EXTI_LINE) != RESET)
{
// LED1 取反
LED1_TOGGLE;
//清除中斷標(biāo)志位
EXTI_ClearITPendingBit(KEY1_INT_EXTI_LINE);
}
}
//void KEY2_IRQHandler(void)
//{
// //確保是否產(chǎn)生了EXTI Line中斷
// if(EXTI_GetITStatus(KEY2_INT_EXTI_LINE) != RESET)
// {
// // LED2 取反
// LED2_TOGGLE;
// //清除中斷標(biāo)志位
// EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE);
// }
//}
/******************************************************************************/
/* STM32F10x Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f10x_xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
SW_18015P.h
#ifndef __EXTI_H
#define __EXTI_H
#include "stm32f10x.h"
//引腳定義
#define KEY1_INT_GPIO_PORT GPIOA
#define KEY1_INT_GPIO_CLK (RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO)
#define KEY1_INT_GPIO_PIN GPIO_Pin_0
#define KEY1_INT_EXTI_PORTSOURCE GPIO_PortSourceGPIOA
#define KEY1_INT_EXTI_PINSOURCE GPIO_PinSource0
#define KEY1_INT_EXTI_LINE EXTI_Line0
#define KEY1_INT_EXTI_IRQ EXTI0_IRQn
#define KEY1_IRQHandler EXTI0_IRQHandler
//#define KEY2_INT_GPIO_PORT GPIOC
//#define KEY2_INT_GPIO_CLK (RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO)
//#define KEY2_INT_GPIO_PIN GPIO_Pin_13
//#define KEY2_INT_EXTI_PORTSOURCE GPIO_PortSourceGPIOC
//#define KEY2_INT_EXTI_PINSOURCE GPIO_PinSource13
//#define KEY2_INT_EXTI_LINE EXTI_Line13
//#define KEY2_INT_EXTI_IRQ EXTI15_10_IRQn
//#define KEY2_IRQHandler EXTI15_10_IRQHandler
void EXTI_Key_Config(void);
#endif /* __EXTI_H */
SW_18015P.c
#include "bsp_exti.h"
/**
* @brief 配置嵌套向量中斷控制器NVIC
* @param 無
* @retval 無
*/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 配置NVIC為優(yōu)先級組1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 配置中斷源:按鍵1 */
NVIC_InitStructure.NVIC_IRQChannel = KEY1_INT_EXTI_IRQ;
/* 配置搶占優(yōu)先級 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
/* 配置子優(yōu)先級 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
/* 使能中斷通道 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* 配置中斷源:按鍵2,其他使用上面相關(guān)配置 */
//NVIC_InitStructure.NVIC_IRQChannel = KEY2_INT_EXTI_IRQ;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief 配置 IO為EXTI中斷口,并設(shè)置中斷優(yōu)先級
* @param 無
* @retval 無
*/
void EXTI_Key_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/*開啟按鍵GPIO口的時鐘*/
RCC_APB2PeriphClockCmd(KEY1_INT_GPIO_CLK,ENABLE);
//RCC_APB2PeriphClockCmd(KEY2_INT_GPIO_CLK,ENABLE);
/* 配置 NVIC 中斷*/
NVIC_Configuration();
/*--------------------------KEY1配置-----------------------------*/
/* 選擇按鍵用到的GPIO */
GPIO_InitStructure.GPIO_Pin = KEY1_INT_GPIO_PIN;
/* 配置為浮空輸入 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY1_INT_GPIO_PORT, &GPIO_InitStructure);
/* 選擇EXTI的信號源 */
GPIO_EXTILineConfig(KEY1_INT_EXTI_PORTSOURCE, KEY1_INT_EXTI_PINSOURCE);
EXTI_InitStructure.EXTI_Line = KEY1_INT_EXTI_LINE;
/* EXTI為中斷模式 */
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
/* 上升沿中斷 */
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
/* 使能中斷 */
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// /*--------------------------KEY2配置-----------------------------*/
// /* 選擇按鍵用到的GPIO */
// GPIO_InitStructure.GPIO_Pin = KEY2_INT_GPIO_PIN;
// /* 配置為浮空輸入 */
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_Init(KEY2_INT_GPIO_PORT, &GPIO_InitStructure);
// /* 選擇EXTI的信號源 */
// GPIO_EXTILineConfig(KEY2_INT_EXTI_PORTSOURCE, KEY2_INT_EXTI_PINSOURCE);
// EXTI_InitStructure.EXTI_Line = KEY2_INT_EXTI_LINE;
//
// /* EXTI為中斷模式 */
// EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
// /* 下降沿中斷 */
// EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
// /* 使能中斷 */
// EXTI_InitStructure.EXTI_LineCmd = ENABLE;
// EXTI_Init(&EXTI_InitStructure);
}
/*********************************************END OF FILE**********************/
參考
STM32-外部中斷詳解https://blog.csdn.net/qq_44016222/article/details/123539693?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170212726916777224466739%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170212726916777224466739&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-123539693-null-null.142%5Ev96%5Epc_search_result_base7&utm_term=stm32%E7%9A%84%E5%A4%96%E9%83%A8%E4%B8%AD%E6%96%AD%E9%80%9A%E9%81%93&spm=1018.2226.3001.4187
聯(lián)系方式 微信號:13648103287