STM32单片机入门学习(七)-外部中断-光敏计数
2023-12-25 22:51:27
?光敏传感器模块:
光敏传感模块一DO端接B14,GND接GND,VCC接VCC,AO不接。
OLED:
OLED:SCL接B12,SDA接B13
如图:
main.c?
#include "stm32f10x.h"
#include "Delay.h" //delay函数所在头文件
#include "OLED.h"
#include "LightSensor.h"
int main(void)
{
OLED_Init();
LightSensor_Init();
while(1)
{
OLED_ShowString(1,3,"count:");
OLED_ShowNum(2,3,get_LightSensorCount(),6);
OLED_ShowString(3,3,"state:");
OLED_ShowNum(4,3,get_LightSensorNum(),6);
}
}
?LightSensor.c
#include "stm32f10x.h"
#include "Delay.h"
uint16_t LightSensorCount;
uint16_t flag;
//初始化LightSensor
void LightSensor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //设置时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //配置AFIO中断引脚
GPIO_InitTypeDef GPIOInitStruct;
GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU; //上拉模式
GPIOInitStruct.GPIO_Pin = GPIO_Pin_14; //B14
GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIOInitStruct);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14); //配置AFIO中断引脚
//配置EXTI
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);
flag=0;
LightSensorCount=0;
}
//获取传感器结果
uint16_t get_LightSensorCount(void)
{
return LightSensorCount;
}
uint16_t get_flag(void)
{
return flag;
}
//获取传感器结果
uint8_t get_LightSensorNum(void)
{
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
}
void EXTI15_10_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line14) == SET)
{
if(get_LightSensorNum()==1)
{
Delay_ms(50);
if(get_LightSensorNum()==1)
{
LightSensorCount++;
flag=1;
}
else
{
flag=0;
}
}
EXTI_ClearITPendingBit(EXTI_Line14);
}
}
?LightSensor.h
#ifndef __LIGHTSENSOR_H
#define __LIGHTSENSOR_H
void LightSensor_Init(void);
unsigned char get_LightSensorNum(void);
uint16_t get_LightSensorCount(void);
uint16_t get_flag(void);
#endif
运行效果:
文章来源:https://blog.csdn.net/veray/article/details/135093126
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!