当前位置: 代码迷 >> 综合 >> STM32F407使用串口3获取攀腾G7的PM2.5数据
  详细解决方案

STM32F407使用串口3获取攀腾G7的PM2.5数据

热度:75   发布时间:2024-03-09 08:37:55.0

STM32F407使用该程序主要是复用串口3用来接收PM2.5所发出的数据,然后获取所需要的pm2.5的值,引脚接口为pb11和pb10,5v电压。

void uart3_init(u32 bound){GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;NVIC_InitTypeDef NVIC_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB,&GPIO_InitStructure);USART_InitStructure.USART_BaudRate = bound;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;    USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE);  //????????1 //USART_ClearFlag(USART1, USART_FLAG_TC);#if EN_USART3_RX    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;         NVIC_Init(&NVIC_InitStructure);   #endif}
u8 Res;
void USART3_IRQHandler(void)                
{u16 t=0;if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  {Res =USART_ReceiveData(USART3);  USART3_RX_BUF[USART3_RX_STA++]=Res ;if(USART3_RX_STA == 32){flag3 = 1;USART3_RX_STA = 0;} 

在main函数中:

int main(void)
{NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);delay_init(168);	uart3_init(9600);	 uart_init(115200);while(1){if(flag3){printf("pm2.5:%d\n",USART3_RX_BUF[5]);while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);delay_ms(1000);flag3 = 0;}}
}

在此模块中传出的数据一共有32位,第5位为pm2.5的数据。