/*-----------------------------------------------
名称:STC12C5A60S2双串口通信(已经成功测试)
编写:WatsonBu
日期:2022.01
修改:无
内容:9600波特率 双串口收发
接线:串口1 发送P3.1,接收P3.0
串口2 发送P1.3,接收P1.2
------------------------------------------------*/
#include "stc12c5a60s2.h"
#include <stdio.h>
#include <stdarg.h>
#include <intrins.h>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
#define S2RI 0x01 //串口2接收中断中断请求标志位
#define S2TI 0x02 //串口2发送中断请求标志位
unsigned char flag1,flag2,temp2;
unsigned char uart2Receive[100];
unsigned char uart1Receive[100];
unsigned int point1=0,point2=0;
unsigned char i;
uchar RecDate[9]={0}; //串口接收 数据
uchar recnum2=0; //串口接收长度
bit recfinish=0; //串口接收完成
uchar flag=0;
sbit TEST_OK = P0^1;
sbit TEST_NG = P2^2;
sbit V5_off = P2^3;
/*------------------------------------------------
函数声明
------------------------------------------------*/
void UART_1SendOneByte(unsigned char c);
void UART_2SendOneByte(unsigned char c);
void uart1SendStr(unsigned char *s);
void uart2SendStr(unsigned char *s);
void send_char_com(uchar ch) //串口发送字节
{
SBUF=ch;
while(TI==0);
TI=0;
}
/*------------------------------------------------
双串口初始化
------------------------------------------------*/
void InitUART (void)
{
SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
TR1 = 1; // TR1: timer 1 打开
EA = 1; //打开总中断
ES = 1; //打开串口中断
S2CON = 0x50;
BRT = 0xFD;
AUXR = 0x10;
IE2 =0x01;
}
void DelayMs(uint n)
{
uchar i=112;
while(n--)
while(i--);
}
/*------------------------------------------------
主函数
------------------------------------------------*/
void main (void)
{
InitUART();
DelayMs(10);
uart1SendStr("COMTOPC");//用于发送给上位机
DelayMs(10);
uart2SendStr(">>GetVal");//去读取电流测试数据,“GetVal” 是电流测试板的请求协议。
DelayMs(10);
while(1)
{
if(flag1==1)
{
flag1=0;
{
for(i=0;i<9;i++)
send_char_com(RecDate[i]);
recnum2=0;// 接收数据长度清零
DelayMs(100);
}
{
uchar i=0;
uchar temp[9]={0};
sprintf((char*)temp,"%8s",RecDate);
if(strcmp(temp,">>TDTEST") == 0)//这是自己任意设定的约定字 "TDTEST",以下照此
{
V5_off=0;
DelayMs(100);
}
else if(strcmp(temp,">>TDT=OK") == 0)
{
TEST_NG=1;
DelayMs(10);
TEST_OK=0;
DelayMs(100);
}
else if(strcmp(temp,">>TDT=NG") == 0)
{
TEST_OK=1;
DelayMs(10);
TEST_NG=0;
DelayMs(100);
}
else
{
uart2SendStr(">>GetVal");
DelayMs(5);
}
}
{
for(i=0;i<9;i++)
RecDate[i]=0;
}
}
//如果串口2接收到数据,将此数据由串口1发送
if(flag2==1)
{
flag2=0;
UART_1SendOneByte(temp2);
}
}
}
/*------------------------------------------------
1发送字符串
------------------------------------------------*/
void uart1SendStr(unsigned char *s)
{
while(*s!='\0')// \0 表示字符串结束标志,
//通过检测是否字符串末尾
{
UART_1SendOneByte(*s);
s++;
}
}
/*------------------------------------------------
2发送字符串
------------------------------------------------*/
void uart2SendStr(unsigned char *s)
{
while(*s!='\0')// \0 表示字符串结束标志,
//通过检测是否字符串末尾
{
UART_2SendOneByte(*s);
s++;
}
}
void UART_1SendOneByte(unsigned char c)
{
SBUF = c;
while(!TI); //若TI=0,在此等待
TI = 0;
}
/****************串口2发送一个字符****************/
void UART_2SendOneByte(unsigned char c)
{
S2BUF = c;
while(!(S2CON&S2TI)); //若S2TI=0,在此等待
S2CON&=~S2TI; //S2TI=0
}
/************串行口1中断处理函数*************/
void UART_1Interrupt(void) interrupt 4
{
if(RI==1)
{
RI=0; //标志位清零
flag1=1;
if(recnum2<9)
{
RecDate[recnum2]=SBUF;
recnum2++;
}
}
}
/************串行口2中断处理函数*************/
void UART_2Interrupt(void) interrupt 8
{
if(S2CON&S2RI)
{
S2CON&=~S2RI;
flag2=1;
temp2=S2BUF;
}
}
|