大家周末好,为什么STC8F单片机串口通讯发不出去啊?
以下是我的源码,哪位有空帮我分析分析:)
#include <STC8x.H>
#include <UART.H>
iui k,j;
iui txdbuf[13]={'H','E','L','L','O','\x20','W','O','R','L','D','!','\0'};
main()
{
uart_init(9600,3,1);
while(1)
{
uart_txd(txdbuf);
}
}
//-------------------- 子函数如下:
/*UART.H*/
#define uc unsigned char
#define ui unsigned int
#define ul unsigned long
#define iuc idata unsigned char
#define iui idata unsigned int
#define iul idata unsigned long
#define bt bit
#define swch switch
#define cs case
#define whl while
#define rtn return
#define brk break
#define itrpt interrupt
uart_init(ul baud,ui prty,bt xos)
{
if(xos==1)
{
P_SW2|=0x80; /*启动内部扩展区寄存器访问功能*/
XOSCCR|=0xc0; /*启动外部晶振*/
while(!(XOSCCR & 1)); /*待晶振频率稳定*/
CLKDIV=0x00; /*晶振不分频*/
CKSEL=0x01; /*选用外部晶振*/
}
SCON=0x50; /*设定串口工作方式*/
PCON=0x7f; /*波特率不倍速*/
TMOD|=0x20; /*设置T0T1寄存器*/
AUXR=0xfc; /*配置辅助寄存器*/
swch(baud)
{
cs 4800: TH1=TL1=0x70;
cs 9600: TH1=TL1=0xb8;
cs 19200: TH1=TL1=0xdc;
cs 28800: TH1=TL1=0xe8;
cs 38400: TH1=TL1=0xee;
cs 57600: TH1=TL1=0xf4;
cs 86400: TH1=TL1=0xf8;
cs 115200: TH1=TL1=0xfa;
cs 230400: TH1=TL1=0xfd;
cs 460800: TH1=TL1=0xfe;
cs 691200: TH1=TL1=0xff;
}
TR1=1; /*启动定时器1*/
swch(prty)
{
cs 0: {
PS=0;
IPH=0x00;
}
cs 1: {
PS=1;
IPH=0x00;
}
cs 2: {
PS=0;
IPH=0x10;
}
cs 3: {
PS=1; /*将串口1中断设置为高优先*/
IPH=0x10; /*将串口1中断设置为最高优先*/
}
}
REN=1; /*允许接收串口数据*/
EA=1; /*打开总中断*/
ES=1; /*打开串口中断*/
P3M1=0x01; /*00000001*/
P3M0=0x02; /*00000010,将串口接收口设为高阻输入模式,发送口设置为强推挽模式*/
}
uart_txd(uc udat[])
{
ui i;
ui j=sizeof(udat);
TI=RI=0;
for(i=0;i<j;i++)
{
SBUF=udat[i];
while(TI==0)
{
;
}
TI=0;
}
}