程序如下:想按下一个按键同时调用4个串口发送一个数据,但是只有串口1发送正常,串口234都不工作,波特率4800,都是用stc官方软件计算的波特率程序初始化,请问各位大神我的程序有什么问题??????
#include<STC15.H>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar good;
//*********************************************************************************************************************************************************
sbit key1=P2^0;
sbit key2=P2^1;
sbit key3=P2^2;
sbit key4=P2^3;
sbit key5=P2^4;
sbit key6=P2^5;
sbit key7=P2^6;
sbit key8=P2^7;
sbit key9=P1^0;
sbit key10=P1^1;
sbit key11=P1^2;
sbit key12=P3^5;
sbit key13=P1^4;
sbit key14=P1^5;
sbit key15=P1^6;
sbit key16=P1^7;
sbit key17=P3^6;
sbit key18=P3^7;
//*********************************************************************************************************************************************************
//*********************************************************************************************************************************************************
void Uart1Init(void) //4800bps@11.0592MHz//2¨ìØÂêéèÖÃ′®¿ú3õê¼»ˉ
{
SCON = 0x50; //8???,?????
AUXR |= 0x40; //???1???Fosc,?1T
AUXR &= 0xFE; //??1?????1???????
TMOD &= 0x0F; //?????1?16???????
TL1 = 0xC0; //??????
TH1 = 0xFD; //??????
ET1 = 0; //?????1??
TR1 = 1; //?????1
ES=1; //Æô¶ˉ′®¿úÖD¶Ï
}
void Uart2Init(void) //4800bps@11.0592MHz
{
S2CON = 0x50; //8???,?????
AUXR |= 0x04; //???2???Fosc,?1T
T2L = 0xC0; //??????
T2H = 0xFD; //??????
AUXR |= 0x10; //?????2
ES=1;
}
void Uart3Init(void) //9600bps@11.0592MHz
{
S3CON = 0x10; //8???,?????
S3CON |= 0x40; //??3?????3???????
T4T3M |= 0x02; //???3???Fosc,?1T
T3L = 0xC0; //??????
T3H = 0xFD; //??????
T4T3M |= 0x08; //?????3
ES=1;
}
void Uart4Init(void) //4800bps@11.0592MHz
{
S4CON = 0x10; //8???,?????
S4CON |= 0x40; //??4?????4???????
T4T3M |= 0x20; //???4???Fosc,?1T
T4L = 0xC0; //??????
T4H = 0xFD; //??????
T4T3M |= 0x80; //?????4
ES=1;
}
//*********************************************************************************************************************************************************
//*********************************************************************************************************************************************************
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
//*************************************************************°′¼ü1*****************************************************************************************
//*********************************************************************************************************************************************************
void trans1()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x01; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans2()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x01; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans3()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x01; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans4()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x01; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*************************************************************°′¼ü2********************************************************************************************
//*********************************************************************************************************************************************************
void trans12()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x02; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans22()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x02; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans32()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x02; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans42()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x02; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//***************************************************************°′¼ü3******************************************************************************************
//*********************************************************************************************************************************************************
void trans13()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x03; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans23()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x03; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans33()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x03; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans43()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x03; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*****************************************************************°′¼ü4****************************************************************************************
//*********************************************************************************************************************************************************
void trans14()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x04; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans24()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x04; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans34()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x04; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans44()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x04; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*****************************************************************°′¼ü5****************************************************************************************
//*********************************************************************************************************************************************************
void trans15()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x05; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans25()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x05; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans35()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x05; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans45()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x05; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//********************************************************************°′¼ü6*************************************************************************************
//*********************************************************************************************************************************************************
void trans16()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x06; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans26()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x06; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans36()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x06; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans46()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x06; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*********************************************************************°′¼ü7************************************************************************************
//*********************************************************************************************************************************************************
void trans17()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x07; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans27()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x07; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans37()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x07; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans47()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x07; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*********************************************************************°′¼ü8************************************************************************************
//*********************************************************************************************************************************************************
void trans18()//′®¿ú1·¢Ëíoˉêy
{
SBUF=0x08; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!TI); //μè′y·¢Ëííê3é
TI=0; //ÇåáãTI·¢Ëííê3é±ê¼Ç
}
void trans28()//′®¿ú¶t·¢Ëíoˉêy
{
Delay1000ms(); //@11.0592MHz
S2BUF=0x08; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S2CON&0x02)); //μè′y·¢Ëííê3é
S2CON=S2CON & 0xfd; //ÇåáãS2TI
}
void trans38()//′®¿úèy·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S3BUF=0x08; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S3CON&0x02)); //μè′y·¢Ëííê3é
S3CON=S3CON & 0xfd; //ÇåáãS3TI
}
void trans48()//′®¿úËÄ·¢Ëíoˉêy
{
Delay1000ms() ; //@11.0592MHz
S4BUF=0x08; //½«òa·¢ËíμÄêy¾Y·Åèë′®¿úêy¾Y»o3åÇø
while(!(S4CON&0x02)); //μè′y·¢Ëííê3é
S4CON=S4CON & 0xfd; //ÇåáãS4TI
}
//*********************************************************************************************************************************************************
//*********************************************************************************************************************************************************
void IO_Init(void)//éèÖÃÎa׼˫Ïò¿ú
{
//½«P30 P31 éèÖÃÎa׼˫Ïò¿ú£¬′®¿ú1
P3M1 &=~( (1<<0) | (1<<1) );
P3M0 &=~( (1<<0) | (1<<1) );
//½«P10 P11 éèÖÃÎa׼˫Ïò¿ú£¬′®¿ú2
P1M1 &=~( (1<<0) | (1<<1) );
P1M0 &=~( (1<<0) | (1<<1) );
//½«P00 P01 éèÖÃÎa׼˫Ïò¿ú£¬′®¿ú3
P0M1 &=~( (1<<0) | (1<<1) );
P0M0 &=~( (1<<0) | (1<<1) );
//½«P02 P03 éèÖÃÎa׼˫Ïò¿ú£¬′®¿ú4
P0M1 &=~( (1<<2) | (1<<3) );
P0M0 &=~( (1<<2) | (1<<3) );
P0M0=0x00;
P0M1=0x00;
P1M0=0x00;
P1M1=0x00;
P2M0=0x00;
P2M1=0x00;
P3M0=0x00;
P3M1=0x00;
P4M0=0x00;
P4M1=0x00;
P5M0=0x00;
P5M1=0x00;
P6M0=0x00;
P6M1=0x00;
P7M0=0x00;
P7M1=0x00;
}
//*********************************************************************************************************************************************************
//*********************************************************************************************************************************************************
void main() //Ö÷3ìDò
{
IO_Init();
while(1)
{
if(key12==0)
{
Uart1Init();
trans1();
Uart2Init();
trans2();
Uart3Init();
trans3();
Uart4Init();
trans4();
}
if(key2==0)
{
Uart1Init();
trans12();
Uart2Init();
trans22();
Uart3Init();
trans32();
Uart4Init();
trans42();
}
if(key3==0)
{
Uart1Init();
trans13();
Uart2Init();
trans23();
Uart3Init();
trans33();
Uart4Init();
trans43();
}
if(key4==0)
{
Uart1Init();
trans14();
Uart2Init();
trans24();
Uart3Init();
trans34();
Uart4Init();
trans44();
}
if(key5==0)
{
Uart1Init();
trans15();
Uart2Init();
trans25();
Uart3Init();
trans35();
Uart4Init();
trans45();
}
if(key6==0)
{
Uart1Init();
trans16();
Uart2Init();
trans26();
Uart3Init();
trans36();
Uart4Init();
trans46();
}
if(key7==0)
{
Uart1Init();
trans17();
Uart2Init();
trans27();
Uart3Init();
trans37();
Uart4Init();
trans47();
}
if(key8==0)
{
Uart1Init();
trans18();
Uart2Init();
trans28();
Uart3Init();
trans38();
Uart4Init();
trans48();
}
}
}
|