本帖最后由 jrcsh 于 2021-12-14 03:09 编辑
从 8H1Kxx 升级到 8A8K64D4 代码复制过去发现串口乱码, 花了一个晚上排查出
是配置了 P1 口 引起 P3 口 的串口乱码, 有其他人也遇到这样吗?
以下代码主要引用至厂家例程:
//#include "reg51.h"
#include "STC8HX.h"
#include "intrins.h"
#define FOSC 11059200UL
#define BRT (65536 - FOSC / 115200 / 4)
//s/fr AUXR = 0x8e;
//sfr T2H = 0xd6;
//sfr T2L = 0xd7;
bit busy;
char wptr;
char rptr;
char buffer[16];
char aaa;
void Delay100ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 5;
j = 52;
k = 195;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void UartIsr() interrupt 4
{
if (TI)
{
TI = 0;
busy = 0;
}
if (RI)
{
RI = 0;
buffer[wptr++] = SBUF;
wptr &= 0x0f;
}
}
void UartInit()
{
SCON = 0x50;
T2L = BRT;
T2H = BRT >> 8;
AUXR = 0x15;
wptr = 0x00;
rptr = 0x00;
busy = 0;
}
void UartSend(char dat)
{
while (busy);
busy = 1;
SBUF = dat;
}
void UartSendStr(char *p)
{
while (*p)
{
UartSend(*p++);
}
}
void main()
{
P1M1 = 0x00; //设置P1强推
P1M0 = 0xf0;// 8H1k f0
UartInit();
ES = 1;
EA = 1;
UartSendStr("Uart Test !\r\n");
while (1)
{
aaa++; if(aaa>10){aaa=0; UartSendStr("Uart Test ! #\r\n"); }
Delay100ms();
UartSendStr("Uart Test !\r\n");
if (rptr != wptr)
{
UartSend(buffer[rptr++]);
rptr &= 0x0f;
}
}
}
下载视频
|