- 打卡等级:初来乍到
- 打卡总天数:5
- 最近打卡:2026-04-28 09:47:34
已绑定手机
新手上路
- 积分
- 37
|
使用串口1,接收状态会出现这种情况
代码:
#include "usart.h"
// 全局变量
uint8_t usart1_recv_len=0;
uint8_t usart1_recv_buff[USART1_RECV_BUFF_SIZE]={0};
void usart1_init(void) //9600bps
{
usart1_recv_len = 0;//初始化接收长度为0,避免首次接收数据异常
P_SW1 = 0x40; //UART1 switch to, 0x00: P3.0 P3.1, 0x40: P3.6 P3.7, 0x80: P1.6 P1.7, 0xC0: P4.3 P4.4
P3M1 &= ~(1<<6);
P3M0 &= ~(1<<6);
P3M1 &= ~(1<<7); // P3.7 (TX) 推挽输出
P3M0 |= (1<<7);
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x20; //设置定时器模式
TL1 = 0xDC; //设置定时初始值
TH1 = 0xDC; //设置定时重载值
ET1 = 0; //禁止定时器中断
TR1 = 1; //定时器1开始计时
ES = 1; //使能串口1中断
}
void usart1_isr(void) interrupt 4
{
if (TI) //检测串口1发送中断
{
TI = 0; //清除串口1发送中断请求位
}
if (RI) //检测串口1接收中断
{
RI = 0; //清除串口1接收中断请求位
// 防缓冲区溢出(比单纯复位更安全)
if(usart1_recv_len < USART1_RECV_BUFF_SIZE - 1)
{
usart1_recv_buff[usart1_recv_len] = SBUF;
usart1_recv_len++;
}
// usart1_send_byte(SBUF);
P03 ^=1;
}
}
// 发送单个字节
void usart1_send_byte(uint8_t dat)
{
SBUF = dat;
while(!TI); // 等待发送完成
TI = 0; // 清标志
}
// 发送字符串
void usart1_send_str(char *str)
{
while(*str)
{
usart1_send_byte(*str++);
}
}
// 发送数组
void usart1_send_array(uint8_t* buff, uint8_t len)
{
uint8_t i;
for(i = 0; i < len; i++)
{
usart1_send_byte(buff[i]);
}
}
// 状态请求包
const uint8_t get_status_packet[] = {
0x68, // 包头
0x02, // 长度L
0x00, // 长度H
0x68, // 重复包头
0x12, // 地址
0x0A, // 类型码
0x1C, // CS校验
0x16 // 包尾
};
/**
* @brief 发送状态包
*/
void send_get_status_packet(void)
{
// 串口发送数组
usart1_send_array((uint8_t *)get_status_packet, sizeof(get_status_packet));
}
void main(void)
{
uint16_t loop_count16;
uint8_t i;
P_SW2 |= 0x80; //B7位写1,使能访问XFR
P0M1 = 0x00; P0M0 = 0x00;
P1M1 = 0x00; P1M0 = 0xFF; // P1.7推挽
P2M1 = 0x00; P2M0 = 0x00;
P3M1 = 0x00; P3M0 = 0x00;
P3PU=0x40;
P4M1 = 0x00; P4M0 = 0x00;
P5M1 = 0x00; P5M0 = 0x00;
P6M1 = 0x00; P6M0 = 0x00;
P7M1 = 0x00; P7M0 = 0x00;
P1SR = 0X00; // i/o切换速度
usb_init(); //USB CDC 接口配置
IE2 |= 0x80; //使能USB中断
EA = 1; //IE |= 0X80;
tim2_init();
usart1_init(); // usart1串口初始化
P03 = 0; // 直接拉低P02
while(1)
{
// if (bUsbOutReady)
// {
// USB_SendData(UsbOutBuffer,OutNumber); //发送数据缓冲区,长度(接收数据原样返回, 用于测试)
//
// usb_OUT_done();
// }
delay_10ms(); // 10ms基准延时(按键防抖)
loop_count16++; // 每10ms累加1
// usart1发送请求
if((loop_count16&0x7F) ==0x01) //1.28秒发起一次询问
{
// send_get_status_packet();
// start_tim2(); // 启动超时检测
// tim2_ov_flag = 0;
usart1_send_byte(0x01);
}
if(usart1_recv_len !=0 || tim2_ov_flag)
{
printf("长度=%d\n",usart1_recv_len);
printf("内容:");
for(i=0; i<8; i++)
{
printf("%02X ", usart1_recv_buff[i]);
}
printf("\n");
memset((unsigned char*)usart1_recv_buff,0,USART1_RECV_BUFF_SIZE); // 清零
usart1_recv_len = 0;
tim2_ov_flag = 0;
}
}
}
以上是问题部分摘出来了,原始现象[09:42:54.120]接收←长度=8
[09:42:56.403]接收←长度=7
内容:6807 207 07 6807 1207 A07 1C07 1607
[09:42:57.503]接收←长度=7
内容:6807 207 07 6807 1207 A07 1C07 1607
[09:42:58.151]接收←长度=5
内容:6805 205 05 6805 1205 A05 1C05 1605
[09:42:58.938]接收←长度=1
内容:6801 201 01 01 01 01 01 01
长度=4
内容:1204 A04 1C04 1604 04 04 04 04
长度莫名其妙,而且数据夹杂着乱码 正确接收应为68 02 00 68 12 0A 1C 16
|
-
接收长度大的离谱吧、接收的数据莫名有很多零
|