标题:
ST15F2K60S2单片机同时使用两个串口,单此程序只能实现串口一的数据发送到串口二
[打印本页]
作者:
李是是是
时间:
2018-12-20 19:11
标题:
ST15F2K60S2单片机同时使用两个串口,单此程序只能实现串口一的数据发送到串口二
单片机代码:
#include <stc15f2k60s2.h>
#include <STDIO.H>
#include <INTRINS.H>
#define U8 unsigned char
#define U16 unsigned int
#define U32 unsigned long
#define FOSC 11059200L
#define BAUD 9600
#define TM (65536 - (FOSC/4/BAUD))
sbit DIR = P3^2;
sbit DIR2 = P3^3;
#define S2RI 0x01
#define S2TI 0x02
bit flagTxd = 0;
bit flagTxd2 = 0;
U8 cntRxd = 0;
U8 cntRxd2 = 0;
U8 flagRxd = 0;
U8 flagRxd2 = 0;
U8 pdata bufRxd[64];
U8 pdata bufRxd2[64];
void Delay1ms()
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delay(unsigned int i)
{
do
{
Delay1ms();
} while (--i);
}
void UartInit(void)
{
SCON = 0x50;
S2CON =0X50;
AUXR |= 0x01;
AUXR |= 0x04;
T2L = 0xE0;
T2H = 0xFe;
AUXR |= 0x10;
ES = 1;
IE2 = 0x01;
EA = 1;
}
void UartWrite(U8 *buf, U8 len)
{
DIR = 1;
while (len--)
{
flagTxd = 0;
SBUF = *buf++;
while (!flagTxd);
}
Delay(1);
DIR = 0;
}
void UartWrite2(U8 *buf, U8 len)
{
DIR2 = 1;
while (len--)
{
flagTxd2 = 0;
S2BUF = *buf++;
while (!flagTxd2);
}
Delay(1);
DIR2 = 0;
}
void main (void)
{
U8 pdata Data[64];
U8 pdata Data2[64];
U8 i;
UartInit();
while(1)
{
DIR = 0;
if (flagRxd==1)
{
Delay(7);
for (i=0;i<cntRxd;i++)
{
Data[i]=bufRxd[i];
}
UartWrite2(Data,cntRxd);
cntRxd=0;
flagRxd=0;
}
DIR2 = 0;
if (flagRxd2==1)
{
Delay(7);
for (i=0;i<cntRxd2;i++)
{
Data2[i]=bufRxd2[i];
}
UartWrite(Data2,cntRxd2);
cntRxd2=0;
flagRxd2=0;
}
}
}
void InterruptUART() interrupt 4
{
if (RI)
{
RI = 0;
flagRxd=1;
if (cntRxd < sizeof(bufRxd))
{
bufRxd[cntRxd++] = SBUF;
}
}
if (TI)
{
TI = 0;
flagTxd = 1;
}
}
void InterruptUART2() interrupt 8
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI;
flagRxd2=1;
if (cntRxd2 < sizeof(bufRxd2))
{
bufRxd2[cntRxd2++] = S2BUF;
}
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI;
flagTxd2 = 1;
}
}
复制代码
作者:
李是是是
时间:
2018-12-20 19:12
来个大佬帮忙看看啊,串口一的数据可以发到串口二,但是串口2的数据无法发到串口一
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1