对射式红外传感器计数器实现
This commit is contained in:
44
BSP/CountSensor.c
Normal file
44
BSP/CountSensor.c
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "stm32f10x.h"
|
||||||
|
|
||||||
|
uint16_t CountSensor_Count = 0;
|
||||||
|
|
||||||
|
void CountSensor_Init(void) {
|
||||||
|
// 初始化Gpio口
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||||||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
|
||||||
|
GPIO_InitTypeDef GPIO_InitStructure;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
|
||||||
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||||
|
|
||||||
|
// 初始化exit
|
||||||
|
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
|
||||||
|
EXTI_InitTypeDef EXTI_InitStructure;
|
||||||
|
EXTI_InitStructure.EXTI_Line = EXTI_Line14;
|
||||||
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||||||
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||||||
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||||||
|
EXTI_Init(&EXTI_InitStructure);
|
||||||
|
|
||||||
|
// 初始化nvic
|
||||||
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
||||||
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
|
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
|
||||||
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||||
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||||
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||||
|
NVIC_Init(&NVIC_InitStructure);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void EXTI15_10_IRQHandler(void) {
|
||||||
|
if (EXTI_GetITStatus(EXTI_Line14) == SET) {
|
||||||
|
CountSensor_Count++;
|
||||||
|
EXTI_ClearITPendingBit(EXTI_Line14);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t CountSensor_GetCount(void) {
|
||||||
|
return CountSensor_Count;
|
||||||
|
}
|
||||||
6
BSP/CountSensor.h
Normal file
6
BSP/CountSensor.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef __COUNTSENSOR_H
|
||||||
|
#define __COUNTSENSOR_H
|
||||||
|
|
||||||
|
void CountSensor_Init(void);
|
||||||
|
uint16_t CountSensor_GetCount(void);
|
||||||
|
#endif
|
||||||
@@ -39,6 +39,8 @@ add_executable(${CMAKE_PROJECT_NAME}
|
|||||||
BSP/OLED.c
|
BSP/OLED.c
|
||||||
BSP/OLED.h
|
BSP/OLED.h
|
||||||
BSP/OLED_Font.h
|
BSP/OLED_Font.h
|
||||||
|
BSP/CountSensor.c
|
||||||
|
BSP/CountSensor.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
||||||
|
|||||||
18
User/main.c
18
User/main.c
@@ -1,21 +1,13 @@
|
|||||||
#include "stm32f10x.h"
|
#include "stm32f10x.h"
|
||||||
#include "Delay.h"
|
#include "Delay.h"
|
||||||
#include "OLED.h"
|
#include "OLED.h"
|
||||||
|
#include "CountSensor.h"
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
||||||
OLED_Init();
|
OLED_Init();
|
||||||
OLED_ShowChar(1,1, 'A');
|
CountSensor_Init();
|
||||||
OLED_ShowString(1,3, "Hello World!12");
|
OLED_ShowString(1,1, "Count:");
|
||||||
OLED_ShowNum(2, 1, 12345, 5);
|
|
||||||
OLED_ShowSignedNum(2, 7, 12, 2);
|
|
||||||
OLED_ShowSignedNum(2, 11, -34, 2);
|
|
||||||
OLED_ShowHexNum(3,1,0xaa55,4);
|
|
||||||
OLED_ShowBinNum(4,1,0xaa55,16);
|
|
||||||
|
|
||||||
Delay_ms(1000);
|
|
||||||
OLED_Clear();
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
OLED_ShowNum(1,7, CountSensor_GetCount(), 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user