标题:
求解STM32的串口中断接收数组出现接收失败(程序很简单,是否有逻辑问题)
[打印本页]
作者:
JUN_xo
时间:
2021-9-23 11:58
标题:
求解STM32的串口中断接收数组出现接收失败(程序很简单,是否有逻辑问题)
程序功能就是单片机接收上位机(串口助手)发送的
adbcd
;将接收的数据放入数组
Rec_array[]
;
通过
if(0 == memcmp(Rec_array, "abc", 3))
语句,翻转
LED
;
正常情况是数组存放了 {a,b,c,d,e}; 前三项 abc对应正确,led翻转
但是接收数据,在
if(0 == memcmp(Rec_array, "abc", 3))
识别时数组里只存放了
{b,0,0,0,0}
;
想做到 发送-
abcde
-五位,led翻转,再发送-
aaaaa
-不翻转,再发送-
abcee
-翻转
程序结构很简单:一个usart函数,一个mian函数
usart.c文件
#include "usart.h"
#include "string.h"
#include "led.h"
#define Code_Max 5//最大值 5
u8 Rec_array[Code_Max];//存放数组
unsigned int Code_Cnt = 0;//数组变量
/*中断初始化*/
static void USART1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*开启串口GPIO时钟*/
USART_APBxClkCmd(USART_GPIO_CLK,ENABLE);
GPIO_InitStructure.GPIO_Pin = USART_TX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(USART_TX_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART_RX_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART_RX_GPIO_PORT,&GPIO_InitStructure);
}
/*串口GPIO初始化*/
void USART1_Configuration(void)
{
USART1_NVIC_Configuration();
USART1_GPIO_Configuration();
USART_InitTypeDef USART_InitStructure;
/*开启串口时钟*/
USART_APBxClkCmd(USART_CLK,ENABLE);
USART_InitStructure.USART_BaudRate = USART_Baudrate;
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(USARTx,&USART_InitStructure);
USART_ITConfig(USARTx,USART_IT_RXNE,ENABLE);
USART_ITConfig(USARTx,USART_IT_TXE,DISABLE);
USART_Cmd(USARTx,ENABLE);
}
/*串口接收中断*/
void USART1_IRQHandler(void)
{
u8 data;
if(USART_GetITStatus(USARTx,USART_IT_RXNE) != RESET)
{
data = USART_ReceiveData(USARTx);
Rec_array[Code_Cnt] = data;//存放到数组
Code_Cnt++;
if(Code_Cnt >= Code_Max)
{ Code_Cnt = 0;
}
USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
}
}
/*串口接收数据函数*/
void USART1_Recive(void)
{
Code_Cnt = 0;
if(0 == memcmp(Rec_array, "abc", 3))
{
LED_BLUE;
}
if(0 == memcmp(Rec_array, "bca", 3))
{
LED_RED;
}
Rec_array[0] = Rec_array[1] = Rec_array[2] = Rec_array[3] = Rec_array[4] = 0;
}
复制代码
usart.h
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
#include <stdio.h>
#define USARTx USART1
#define USART_CLK RCC_APB2Periph_USART1
#define USART_Baudrate 115200
#define USART_GPIO_CLK RCC_APB2Periph_GPIOA
#define USART_APBxClkCmd RCC_APB2PeriphClockCmd
#define USART_TX_GPIO_PORT GPIOA
#define USART_TX_GPIO_PIN GPIO_Pin_9
#define USART_RX_GPIO_PORT GPIOA
#define USART_RX_GPIO_PIN GPIO_Pin_10
#define USART_IRQ USART1_IRQn
#define USART_IRQHandler USART1_IRQHandler
void USART1_Configuration(void);
void USART1_Recive(void);
#endif
复制代码
main.c
#include "stm32f10x.h"
#include "led.h"
#include "usart.h"
int main(void)
{
LED_GPIO_Config();
USART1_Configuration();
while (1)
{
USART1_Recive();
}
}
复制代码
作者:
a1093941419
时间:
2021-9-23 14:34
void USART1_Recive(void)
{
Code_Cnt = 0; //错误:该全局变量一直被清除;导致只有Rec_array[0]值
}
作者:
lkc8210
时间:
2021-9-23 15:32
若要比较串口接收的字串
加上结束符或定时器来确认传输结束比较好
结束符:
每次传输都在结尾加上特定字符如:';'分号(abcde;)
if(data == ';')Trans_Done = 1;
定时器:
建立一个1ms定时器,在串口中断中重载定时器并开始计时
在定时器中关闭定时器并Trans_Done = 1;
while(1)
{
if(Trans_Done)
{
Trans_Done = 0;
USART1_Recive();
}
}
作者:
yzwzfyz
时间:
2021-9-23 17:31
画个流程图,先走通流程,而后再写程序。
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1