基于stm32用代码写出两个红外对管检测人的进出数量,并通过LCD屏显示出来
时间: 2024-01-24 20:04:58 浏览: 124
好的,以下是一个基于STM32的代码示例,可以用两个红外对管检测人的进出数量,并通过LCD屏幕显示出来。请注意,此示例代码是基于STM32CubeIDE和HAL库编写的,您需要将其适应您的硬件和开发环境。
首先,您需要在STM32CubeIDE中创建一个新的工程,并添加以下HAL库和设备驱动程序:
- GPIO库
- EXTI库
- TIM库
- LCD库
然后,您可以使用以下代码示例来实现该应用程序:
```c
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#define IR1_GPIO_Port GPIOA
#define IR1_Pin GPIO_PIN_0
#define IR2_GPIO_Port GPIOA
#define IR2_Pin GPIO_PIN_1
#define LCD_RS_Pin GPIO_PIN_0
#define LCD_RS_GPIO_Port GPIOB
#define LCD_EN_Pin GPIO_PIN_1
#define LCD_EN_GPIO_Port GPIOB
#define LCD_D4_Pin GPIO_PIN_5
#define LCD_D4_GPIO_Port GPIOB
#define LCD_D5_Pin GPIO_PIN_6
#define LCD_D5_GPIO_Port GPIOB
#define LCD_D6_Pin GPIO_PIN_7
#define LCD_D6_GPIO_Port GPIOB
#define LCD_D7_Pin GPIO_PIN_8
#define LCD_D7_GPIO_Port GPIOB
uint8_t IR1_Status = 0;
uint8_t IR2_Status = 0;
uint8_t People_In = 0;
uint8_t People_Out = 0;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_LCD_Init(void);
void IR1_Callback(void)
{
if(HAL_GPIO_ReadPin(IR1_GPIO_Port, IR1_Pin) == GPIO_PIN_SET)
{
IR1_Status = 1;
}
else
{
IR1_Status = 0;
}
}
void IR2_Callback(void)
{
if(HAL_GPIO_ReadPin(IR2_GPIO_Port, IR2_Pin) == GPIO_PIN_SET)
{
IR2_Status = 1;
}
else
{
IR2_Status = 0;
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
MX_LCD_Init();
HAL_TIM_Base_Start_IT(&htim2);
while (1)
{
if(IR1_Status == 1 && IR2_Status == 0)
{
People_In++;
IR1_Status = 0;
}
else if(IR1_Status == 0 && IR2_Status == 1)
{
People_Out++;
IR2_Status = 0;
}
char buffer[16];
sprintf(buffer, "In:%d Out:%d", People_In, People_Out);
LCD_SendCommand(0x80);
LCD_SendString(buffer);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
HAL_GPIO_EXTI_Callback(IR1_Pin);
HAL_GPIO_EXTI_Callback(IR2_Pin);
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = IR1_Pin|IR2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
GPIO_InitStruct.Pin = LCD_RS_Pin|LCD_EN_Pin|LCD_D4_Pin|LCD_D5_Pin
|LCD_D6_Pin|LCD_D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
static void MX_TIM2_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 7999;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 99;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
}
static void MX_LCD_Init(void)
{
LCD_Init();
LCD_SendCommand(0x01);
HAL_Delay(10);
LCD_SendCommand(0x02);
HAL_Delay(10);
LCD_SendCommand(0x28);
HAL_Delay(10);
LCD_SendCommand(0x0C);
HAL_Delay(10);
LCD_SendCommand(0x06);
HAL_Delay(10);
}
void EXTI0_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(IR1_Pin);
}
void EXTI1_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(IR2_Pin);
}
void Error_Handler(void)
{
while(1)
{
}
}
void SysTick_Handler(void)
{
HAL_IncTick();
}
void LCD_SendHalfByte(uint8_t HalfByte)
{
HAL_GPIO_WritePin(LCD_D4_GPIO_Port, LCD_D4_Pin, (HalfByte & 0x01));
HAL_GPIO_WritePin(LCD_D5_GPIO_Port, LCD_D5_Pin, (HalfByte & 0x02));
HAL_GPIO_WritePin(LCD_D6_GPIO_Port, LCD_D6_Pin, (HalfByte & 0x04));
HAL_GPIO_WritePin(LCD_D7_GPIO_Port, LCD_D7_Pin, (HalfByte & 0x08));
HAL_GPIO_WritePin(LCD_EN_GPIO_Port, LCD_EN_Pin, GPIO_PIN_SET);
HAL_Delay(1);
HAL_GPIO_WritePin(LCD_EN_GPIO_Port, LCD_EN_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
}
void LCD_SendCommand(uint8_t Command)
{
HAL_GPIO_WritePin(LCD_RS_GPIO_Port, LCD_RS_Pin, GPIO_PIN_RESET);
LCD_SendHalfByte(Command >> 4);
LCD_SendHalfByte(Command & 0x0F);
}
void LCD_SendData(uint8_t Data)
{
HAL_GPIO_WritePin(LCD_RS_GPIO_Port, LCD_RS_Pin, GPIO_PIN_SET);
LCD_SendHalfByte(Data >> 4);
LCD_SendHalfByte(Data & 0x0F);
}
void LCD_SendString(char* String)
{
while(*String)
{
LCD_SendData(*String++);
}
}
```
在此代码中,我们使用了两个GPIO引脚来连接红外对管,并使用了一个TIM定时器来定期检测红外对管的状态。当红外对管的状态发生变化时,我们使用中断回调函数来更新状态变量。
此外,我们还使用了一个LCD库来控制LCD屏幕,并编写了一些简单的函数来发送命令和数据到LCD屏幕。
最后,在主循环中,我们使用状态变量来计算进出人数,并使用LCD屏幕来显示它们。
希望这个示例代码对您有所帮助!
阅读全文