STM32:使用外部中断控制对射式红外传感器并计次

简介: STM32:使用外部中断控制对射式红外传感器并计次

1.主函数(main.c)代码:


890d5af079d44278ba6d7c3fdc188b3f.png

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"
int main(void)
{
    OLED_Init();    
    OLED_ShowString(1,1,"Count:");
    CountSensor_Init();
    while(1)
    {
        OLED_ShowNum(2,1,CountSensor_Get(),4);
    }
}

注:该处警告是C99协议,无视即可

2.CountSensor.c代码部分:


b746df7b2f2b44dca34fd05e44540ada.png


0b2ca225559443f4911e327df73e5ca9.png

f1980671dd41485297adb60db36173c2.png

#include "stm32f10x.h"                  // Device header
uint16_t CountSensor_Count;
void CountSensor_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
    GPIO_InitStruct.GPIO_Pin=GPIO_Pin_14;
    GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&GPIO_InitStruct);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);
    EXTI_InitTypeDef EXTI_InitStruct;
    EXTI_InitStruct.EXTI_Line=EXTI_Line14;
    EXTI_InitStruct.EXTI_LineCmd=ENABLE;
    EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
    EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
    EXTI_Init(&EXTI_InitStruct);
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_InitStruct;
    NVIC_InitStruct.NVIC_IRQChannel=EXTI15_10_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
    NVIC_Init(&NVIC_InitStruct);
}
void EXTI15_10_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line14)==SET)
    {
        if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
        {
            CountSensor_Count ++;
        }
        EXTI_ClearITPendingBit(EXTI_Line14);
    }
}
uint16_t CountSensor_Get(void)
{
    return CountSensor_Count;
}


3.CountSensor.h代码部分:

e40aef705597434e8cec12ae27a1908a.png


#ifndef  __COUNT_SENSOR_H
#define  __COUNT_SENSOR_H
uint16_t CountSensor_Get(void);
void EXTI15_10_IRQHandler(void);
void CountSensor_Init(void);
#endif


目录
打赏
0
0
0
0
11
分享
相关文章
STM32外设系列—红外遥控
本文详细介绍了红外通信的应用,原理。介绍了一种常用的二进制脉冲码形式。最后,给出了红外遥控的实现思路和程序设计。
501 2
STM32外设系列—红外遥控
【STM32】I2C练习,SHT3X温度传感器的数据读取
【STM32】I2C练习,SHT3X温度传感器的数据读取
135 0
基于STM32的光敏传感器数据采集系统-嵌入式系统与设计课程设计2
基于STM32的光敏传感器数据采集系统-嵌入式系统与设计课程设计
879 0
【STM32基础 CubeMX】外部中断
【STM32基础 CubeMX】外部中断
276 44
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等