遇到串口4奇怪的问题
想同时使用4个串口,串口1,2都很好调试,串口3,4没调通。所以单独做了一个串口3,4的程序。
串口3可以正常收发数据;
串口4可以正常发送数据,接收到字符后再发出就出现错误。使用芯片STC8H4K64TLR。
具体代码如文件,具体是什么问题呢?
<div class="blockcode"><blockquote>#include <STC8H.h>
#include <stdio.h>
#include "intrins.h"
//========================================================================
// 数据类型定义
//========================================================================
typedef unsigned char u8; //8 bits
typedef unsigned int u16; // 16 bits
typedef unsigned long u32; // 32 bits
typedef signed char int8; //8 bits
typedef signed int int16;// 16 bits
typedef signed long int32;// 32 bits
typedef unsigned char uint8;//8 bits
typedef unsigned int uint16; // 16 bits
typedef unsigned long uint32; // 32 bits
#define MAIN_Fosc 22118400L //定义主时钟 // 11059200UL
#define UART3_BRT (65536 - MAIN_Fosc / 115200 / 4)
#define UART4_BRT (65536 - MAIN_Fosc/ 115200 / 4)
#define COM3_BUF_LENGTH 64
#define COM4_BUF_LENGTH 128
bit B_COM3_Tx_Busy;
u16 COM3_Rx_Cnt;
u16 COM3_Tx_Cnt;
u8 xdata COM3_Rx_Buffer;
bit B_COM4_Tx_Busy;
u16 COM4_Rx_Cnt;
u16 COM4_Tx_Cnt;
u8 xdata COM4_Rx_Buffer;
void UART3_Isr() interrupt 17
{
if (S3CON & 0x02)
{
S3CON &= ~0x02;
B_COM3_Tx_Busy = 0;
}
if (S3CON & 0x01)
{
S3CON &= ~0x01;
COM3_Rx_Buffer = S3BUF;
if(++COM3_Rx_Cnt >= COM3_BUF_LENGTH - 1) COM3_Rx_Cnt = 0; //防溢出
COM3_Rx_Buffer = 0;
}
}
void Uart3_Init()
{
S3CON = 0x50;
T3L = UART3_BRT;
T3H = UART3_BRT >> 8;
T4T3M |= 0x0A;
IE2 |= 0x08;
COM3_Rx_Cnt = 0;
COM3_Tx_Cnt= 0;
B_COM3_Tx_Busy = 0;
}
// -------- 串口3 发送字符串 -------------------------------------------
void COM3_PrintString(unsigned char *puts) //发送一个字符串
{
for (; *puts != 0;puts++) //遇到停止符0结束
{
S3BUF = *puts;
B_COM3_Tx_Busy = 1;
while(B_COM3_Tx_Busy);
}
}
void Uart3_SendChar(char dat)
{
while (B_COM3_Tx_Busy);
B_COM3_Tx_Busy = 1;
S3BUF = dat;
}
void Uart3_SendStr(char *p)
{
while (*p)
{
Uart3_SendChar(*p++);
}
}
void Uart4_Isr() interrupt 18
{
if (S4CON & 0x02) // ((S4CON & 0x02) != 0)
{
S4CON &= ~0x02;
B_COM4_Tx_Busy = 0;
}
if ((S4CON & 0x01) != 0) // (S4CON & 0x01)
{
S4CON &= ~0x01;
COM4_Rx_Buffer = S4BUF;
if(++COM4_Rx_Cnt >= COM4_BUF_LENGTH - 1) COM4_Rx_Cnt = 0; //防溢出
COM4_Rx_Buffer = 0;
}
}
void Uart4_Init()
{
S4CON = 0x50;
T4L = UART4_BRT;
T4H = UART4_BRT >> 8;
T4T3M |= 0xA0;
IE2 |= 0x10;
COM4_Rx_Cnt = 0;
COM4_Tx_Cnt= 0;
B_COM4_Tx_Busy = 0;
}
void COM4_PrintString(unsigned char *puts) //发送一个字符串
{
for (; *puts != 0;puts++) //遇到停止符0结束
{
S4BUF = *puts;
B_COM4_Tx_Busy = 1;
while(B_COM4_Tx_Busy);
}
}
void Uart4_SendChar(char dat)
{
while (B_COM4_Tx_Busy);
B_COM4_Tx_Busy = 1;
S4BUF = dat;
}
void Uart4_SendStr(char *p)
{
while (*p)
{
Uart4_SendChar(*p++);
}
}
void GPIO_Init(void) // 管脚状态初始化
{
// I/O口工作模式PnM1和PnM0组合,00-准双向口,01-推挽输出,10-高阻输入,11-开漏输出
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
}
void Pin_Switch(void) // 功能管脚切换
{
P_SW2 &= 0xFD; // B1=0 设置串口3的功能脚为P00和P01
P_SW2 &= ~0x04; //UART4: RxD4(P0.2), TxD4(P0.3)
}
void main()
{
u16 i,j,n;
u8 temp;
u8 tt[] = "temp data \r\n";
GPIO_Init(); // 管脚状态初始化
Pin_Switch(); // 功能管脚切换
Uart3_Init();
Uart4_Init();
EA = 1;
i=0;
j=0;
while (1)
{
if(i>1000)
{
i=0;
j++;
if(j>1000)
{
j=0;
P52 = ~P52;
Uart3_SendStr("Uart 3 Test !\r\n");
// COM3_PrintString("COM3_PrintString ------ \r\n");
Uart4_SendStr("Uart 4 Test !\r\n");
// Uart4_SendStr(tt);// UART1发送一个字符串
sprintf(temp,"COM4_Rx_Cnt = %d \n",COM4_Rx_Cnt);
Uart4_SendStr(temp);// UART1发送一个字符串
}
}
i++;
if(COM3_Rx_Cnt > 2)
{
// Uart3_SendStr(COM3_Rx_Buffer);
COM3_PrintString(COM3_Rx_Buffer);
COM3_Rx_Cnt = 0;
}
if(COM4_Rx_Cnt > 2)
{
for(n = 0; n < COM4_Rx_Cnt; n++)
{
Uart4_SendChar(COM4_Rx_Buffer);
}
// Uart4_SendStr(COM4_Rx_Buffer);
// Uart4_SendStr("Uart 4 Get data ---- !\r\n");
//// COM4_PrintString(COM4_Rx_Buffer);
COM4_Rx_Cnt = 0;
}
}
}
本帖最后由 giveyou 于 2024-2-29 10:39 编辑
202-204行,正常发送。接收数据的计数器COM4_Rx_Cnt计数正常。
219-230行,用于返回接收到的数据,返回的数据就是有问题的。
用我们 官方经典串口程序
https://www.stcaimcu.com/forum.p ... t&ptid=375&pid=2070
用我们强大的 仿真器 来调试发现你错在哪
【新提醒】仿真 STC8 系列 MCU,用 STC-USB Link1D 全自动仿真/脱机下载 工具 ! 有视频讲解 - 仿真/编译器/头文件 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
用我们 STC-ISP 强大的原生串口助手 来讨论
【新提醒】视频讲解:串口绘图功能、串口助手高级应用、STC8库函数|串口库函数功能介绍 - 串行口,DMA支持的4组串口,RS232,RS485 - 国芯论坛-STC全球32位8051爱好者互助交流社区 - STC全球32位8051爱好者互助交流社区 (stcaimcu.com)
深圳国芯人工智能有限公司-工具软件 (stcai.com)
神农鼎 发表于 2024-2-29 10:56
深圳国芯人工智能有限公司-工具软件 (stcai.com)
谢谢,我先学习一下,然后找我的错误。 神农鼎 发表于 2024-2-29 10:48
用我们 官方经典串口程序
程序不做改动的情况下,例05的4个串口的程序都正常,
例04的单独串口4的程序不正常。芯片为STC8H4K64TLR
所以我们程序完善的情况下,芯片正常
另外:
芯片为STC8H4K64TLR====现在没这个命名的
只有 STC8H4K64TL-LQFP48/32, STC8H4K32TL-LQFP48/32
只有 STC8H4K64TLCD-LQFP64/48, STC8H4K32TLCD-LQFP64/48
页:
[1]
2