本帖最后由 辉常可乐 于 2017-8-28 21:37 编辑
最近买了一一块测PM2.5的传感器,传感器接通电源后直接输出PM2.5的值,输出格式如图。这是编写的串口2接收代码的主要部分,在主函数中执行时发现没有进入到中断,请问有谁能帮忙看看嘛?或者有相应类似的代码的,可以方便分享一下吗?
typedef struct
{
u8 head;
u8 data[5];
u8 tail;
u8 data_pt;
u8 lock_flag;
}ptr_stack;
ptr_stack rx_stack;
void rx_stack_init(void)
{
u8 i;
rx_stack.head=0;
for(i=0;i<=4;i++)
{rx_stack.data=0;}
rx_stack.tail=0;
rx_stack.data_pt=0;
rx_stack.lock_flag=UNLOCK;
uart2_config();
}
void uart2_config(void)
{
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure1;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO| RCC_APB2Periph_USART1, ENABLE);
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure1.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 0; //
NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure1); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷USART1
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 2400;//ò»°ãéèÖÃÎa9600;
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//¿aÆôÖD¶Ï
USART_Cmd(USART2, ENABLE); //ê1Äü′®¿ú
}
void USART2_IRQHandler(void) //′®¿ú2ÖD¶Ï·tÎñ3ìDò
{
u8 res;
int i;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
res=USART_ReceiveData(USART2);
if (rx_stack.lock_flag==UNLOCK);
{
if(res==0xaa)
{rx_stack.head=res;}
else if(res==0xff)
{rx_stack.tail=res;}
else{
rx_stack.data[rx_stack.data_pt]=res;
rx_stack.data_pt++;
if((rx_stack.data_pt>=5)&&(rx_stack.data[4]==rx_stack.data[0]+rx_stack.data[1]+rx_stack.data[2]+rx_stack.data[3])&&(rx_stack.tail==0xff))
rx_stack.lock_flag=LOCK;
}
}
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
有信号的,我用串口转usb模块直接接到电脑上,会按照图中格式打印出读数来
你两个波特频率一样,那你的配置有问题
一周热门 更多>