TEMTOP 细颗粒物传感器调试
作者:互联网
细颗粒物调试
引脚定义
TEMTOP ------> STM32 USART2
1.VCC ------> 5V
2.GND ------> GND
4.RX ------> PD6
5.TX ------> PD7
通讯协议
串口配置及中断
void UART2_Init(u32 bound)
{
//UART 初始化设置
UART2_Handler.Instance=USART2; //USART2
UART2_Handler.Init.BaudRate=bound; //波特率
UART2_Handler.Init.WordLength=UART_WORDLENGTH_8B; //字长为8位数据格式
UART2_Handler.Init.StopBits=UART_STOPBITS_1; //一个停止位
UART2_Handler.Init.Parity=UART_PARITY_NONE; //无奇偶校验位
UART2_Handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //无硬件流控
UART2_Handler.Init.Mode=UART_MODE_TX_RX; //收发模式
UART2_Handler.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UART2_Handler.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&UART2_Handler); //HAL_UART_Init()会使能UART2
HAL_UART_Receive_IT(&UART2_Handler, (uint8_t *)uartBuffer, RXBUFFERSIZE);
}
/**USART2 GPIO Configuration
PD6 ------> USART2_TX
PD7 ------> USART2_RX
*/
if(huart->Instance==USART2)//如果是串口1,进行串口1 MSP初始化
{
__HAL_RCC_GPIOD_CLK_ENABLE(); //使能GPIOA时钟
__HAL_RCC_USART2_CLK_ENABLE(); //使能USART2时钟
GPIO_Initure.Pin=GPIO_PIN_6 | GPIO_PIN_7; //PD5\6
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速
GPIO_Initure.Alternate=GPIO_AF7_USART2; //复用为USART2
HAL_GPIO_Init(GPIOD,&GPIO_Initure); //初始化PD9
#if EN_USART2_RX
HAL_NVIC_EnableIRQ(USART2_IRQn); //使能USART1中断通道
HAL_NVIC_SetPriority(USART2_IRQn,3,3); //抢占优先级3,子优先级3
#endif
}
/*+-----------------细颗粒物中断函数-----------------+*/
uint8_t PM_RX_BUF[32];
uint8_t pmPoint = 0;
int PM1Data, PM25Data, PM10Data;
void USART2_IRQHandler(void)
{
uint32_t tmp1 = 0U, tmp2 = 0U;
tmp1 = __HAL_UART_GET_FLAG(&UART2_Handler, UART_FLAG_RXNE);
tmp2 = __HAL_UART_GET_IT_SOURCE(&UART2_Handler, UART_IT_RXNE);
if(tmp1 != RESET && tmp2 != RESET)
{
PM_RX_BUF[pmPoint] = (uint8_t)(UART2_Handler.Instance->DR & (uint8_t)0x00FFU);
pmPoint ++;
if(pmPoint == 2)
{
if(PM_RX_BUF[0] != 0x42 || PM_RX_BUF[1] != 0x4D)
{
pmPoint = 0;
}
}
if(pmPoint == 32)
{
if(PM_RX_BUF[0] == 0x42 && PM_RX_BUF[1] == 0x4D)
{
PM1Data = PM_RX_BUF[4] * 256 + PM_RX_BUF[5];
PM25Data = PM_RX_BUF[6] * 256 + PM_RX_BUF[7];
PM10Data = PM_RX_BUF[8] * 256 + PM_RX_BUF[9];
pmPoint = 0;
//调试语句
//HAL_UART_Transmit(&UART1_Handler,(uint8_t*)PM_RX_BUF,32,1000);
}
else
{
pmPoint = 0;
}
}
}
HAL_UART_IRQHandler(&UART2_Handler);
}
之前使用的是sensorion的颗粒物传感器,比此传感器更轻薄。
标签:TEMTOP,UART,RX,颗粒物,UART2,Handler,GPIO,USART2,调试 来源: https://blog.csdn.net/qq_39486659/article/details/121694574