对射式红外传感器计数复现
配置工程
我们直接复制oled的工程,但是要重命名。
将PB14设置为中断引脚
自定义命名为sensorcount
设置为上升沿触发
打开中断
配置NVCI
都为默认就可以了
修改代码
修改stm32f1xx_it.c 文件
找到中断函数并修改
void EXTI15_10_IRQHandler(void)
{/* USER CODE BEGIN EXTI15_10_IRQn 0 */if(__HAL_GPIO_EXTI_CLEAR_FLAG(sensorcount_Pin)){/* USER CODE END EXTI15_10_IRQn 0 */HAL_GPIO_EXTI_IRQHandler(sensorcount_Pin);/* USER CODE BEGIN EXTI15_10_IRQn 1 */count+=1;}/* USER CODE END EXTI15_10_IRQn 1 */}
定义一个count用于计数
添加计数函数
/* USER CODE BEGIN 1 */
uint16_t get_sensorcounter(void)
{return count;
}
/* USER CODE END 1 */
修改main函数文件
找到void main并修改
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();/* USER CODE BEGIN 2 */OLED_Init();OLED_Clear();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){OLED_ShowNum(1, 1, get_sensorcounter(), 5);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/