本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
各位大神,我写了一个双串口的开发板输出数据的程序。
程序的设计思路是:开发板接收接收信息,如果收到长的数据,直接输出,然后接收短的才能输出,输出之后等待下一次信号输入;
下次仍先接收长的数据。
如果先收到短的,保存起来,等长的输出之后,再输出短的。然后同样还是继续等待长数据的输出。
/********************************/
/* 串口1 SBUF接收条码信息 */
/* 串口2 S2BUF发送条码信息 */
/********************************/
#include "stc12c5a.h"
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define FOSC 11059200L
#define BAUD 9600
//定义全局变量存储接收到的字符串
uchar ReceivedString[22];
uchar index=0;
//uchar Pck_Ipt_Finish=0;
bit Rcv_Dlv_Finish=0;
bit Rcv_Pck_Finish=0;
//定义全局变量存储运单码
uchar DeliveryCode[20];
//定义全局变量存储包材码
uchar PackingCode[12];
void UART_Initial();
void SendBarcode(uchar *p);
void delay(uint i);
void main(void)
{
bit Dlv_Opt_Finish=0;
UART_Initial();
while(1)
{
ES=1;
if((Rcv_Dlv_Finish==1)&&(Rcv_Pck_Finish==0))
{
Rcv_Dlv_Finish=0;
Dlv_Opt_Finish=1;
SendBarcode(DeliveryCode);
memset(DeliveryCode,'\0',20);
delay(3500);
}
if((Rcv_Pck_Finish==1)&&(Rcv_Dlv_Finish==0))
{
Rcv_Pck_Finish=0;
if(Dlv_Opt_Finish==1)
{
Dlv_Opt_Finish=0;
SendBarcode(PackingCode);
memset(PackingCode,'\0',12);
}
else
{
ES=1;
while(Rcv_Dlv_Finish==0);
Rcv_Dlv_Finish=0;
Dlv_Opt_Finish=0;
SendBarcode(DeliveryCode);
memset(DeliveryCode,'\0',20);
delay(3500);
SendBarcode(PackingCode);
memset(PackingCode,'\0',12);
break;
}
}
}
}
void UART_Initial()
{
//Init Uart2:9600,8,0,1.
S2CON = 0x50;
AUXR |= 0x10;
BRT =256 -(FOSC/32/12/BAUD);
IE2 = 0x01;
//Init Uart1:9600,8,0,1.
SCON = 0x50;
TMOD = 0X20;
TL1 = 0xFD;
TH1 = 0xFD;
TR1 = 1;
// ES = 1;
EA = 1;
}
//接收条码字符串
void UART_1_Rcv() interrupt 4
{
uchar i;
if(RI)
{
RI=0;
ReceivedString[index++]=SBUF;
if(SBUF=='\n')
{
ES=0;
index=0;
if(strlen(ReceivedString)>10)
{
for(i=0;i<strlen(ReceivedString);i++)
{
DeliveryCode[i]=ReceivedString[i];
}
Rcv_Dlv_Finish=1;
}
if(strlen(ReceivedString)<10)
{
for(i=0;i<strlen(ReceivedString);i++)
{
PackingCode[i]=ReceivedString[i];
}
Rcv_Pck_Finish=1;
}
}
}
}
//发送条码字符串
void SendBarcode(uchar *p)
{
while(*p!='\n')
{
S2CON = S2CON & 0xfd; //S2TI清零
S2BUF=*p;
while((S2CON & 0x02)==0x0);
S2CON = S2CON & 0xfd; //S2TI清零
p++;
}
}
void delay(uint i)
{
uint j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--) ;
}
|