Home > other >  Online help!!!!!! Stc16c5a60s wrote a dual serial communication program, it is a serial port after r
Online help!!!!!! Stc16c5a60s wrote a dual serial communication program, it is a serial port after r

Time:12-05

Procedure is as follows: serial port receive send 1 can be used, also can use a serial port 2 to send test, now the problem is the card on the reception, there is something wrong with the receiving
Trouble BBS great god help see serial port 2 reception where there is a problem, thank you very much!!!!!!


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Time: 2020.04.04
Chip: STC12C5A60S2
Crystals: 11.0592 MHz baud rate: 115200 BPS
Pin definition: 1: serial port to send - TxD/P3.1; Receiving, RxD/P3.0
Serial port 2: send - TxD2/P1.3; Receiving - RxD2/P1.2
Function description: STC12 double serial communication (interrupt)
After the serial port 1 receiving data, the data sent by the serial port 2
When the serial port 2 after receiving data, the data sent by the serial port 1
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include
# include "intrins. H"
# include "string. H"
# include "stdio.h"
# define S2RI 0 x01//serial port receiving interrupt request 2 marks a
# define S2TI 0 x02//serial port 2 send interrupt request marks a
# define uchar unsigned char
# define uint unsigned int
# define S2RI 0 x01
# define S2TI 0 x02
# define Code_head1 0 xaa
# define Code_head2 0 x55
# define len 25
Bit busy2;//this bit is used for a serial port to judge whether send busy
Char xdata Uart2_buff [40].//the buffer data
Char xdata Uart1_buff [40].
Uchar count2=0;//serial counter
Uchar count1=0;//serial counter
Char xdata echo [40].
Unsigned char, flag1, flag2, temp1, temp2;

Void Clear_Buff ();//remove an array by the values from the serial port 1, 2, to prepare for the next
Void read_code2 (uchar _char);//save a serial port receives the characters
Void Uart_1_SendString (char * P);//serial port 1 send string function
Float TDM_Receive_Check ();
Void set_brrate ();
Void serial_put_uint (unsigned int number);
Void dispplay (int _data while forming);
Void read_ac ();
Void read_once ();
Void delay_ms (uint ms);
/* * * * * * * * * * * * * * * * serial port initialization function * * * * * * * * * * * * * * * */
Void InitUART (void)
{
PCON & amp;=0 x7f;//baud rate not 2 x
SCON=0 x50;//8 bits of data, variable baud rate
AUXR |=0 x40; A clock for Fosc//timer, namely 1 t
AUXR & amp;=0 xfe;//serial port 1 select timer 1 for baud rate generator
TMOD & amp;=0 x0f;//remove the timer 1 model a
TMOD |=0 x20;//set the timer 1 to 8 automatic reinstall way
TL1=0 XFD;//set the timing of initial
TH1=0 XFD;//set the timer reshipment value
ET1=0;//prohibited timer interrupt 1
TR1=1;//start the timer 1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * 115200 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
AUXR & amp;=0 xf7;//baud rate not 2 x
S2CON=0 x50;//8 bits of data, variable baud rate
AUXR |=0 x04;//clock for Fosc independent baud rate generator, which is 1 t
BRT=0 XFD;//set up independent baud rate generator reshipment value
AUXR |=0 x10.//start independent baud rate generator

IE2=0 x01;//allow the serial port interrupt, 2
EA=1;//always interrupt must open, or you can send, can't receive
}
/* * * * * * * * * * * * * * * * serial port to send 1 * * * * * * * * * * * * * * * */
Void UART_1SendOneByte (unsigned char c)
{
SBUF=c;
while(! TI);//if TI=0, in this waiting for
TI=0;
}
/* * * * * * * * * * * * * * * * serial port to send 2 * * * * * * * * * * * * * * * */
Void UART_2SendOneByte (unsigned char c)
{
S2BUF=c;
while(! (S2CON & amp; S2TI));//if S2TI=0, in this waiting for
S2CON & amp;=~ S2TI;//S2TI=0
}
/* * * * * * * * * * * * * * * * * the main function * * * * * * * * * * * * * * * * * */
Void main (void)
{
InitUART ();//serial port initialization
Uart_1_SendString (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ r \ n ");
Uart_1_SendString (" * * * * * * * * * * * * 00000 * * * * * * * * * * * \ r \ n ");
While (1)
{
int i;
//if the serial port 1 to receive data, the data sent by a serial port 2
If (flag1==1)
{
Flag1=0;
for(i=0; i{
UART_1SendOneByte (Uart1_buff [I]);
}
Read_ac ();
Uart_1_SendString (" \ r \ n ");
Clear_Buff ();
}

//if the serial port 2 to receive data, the data is sent by the serial port 1
If (flag2==1)
{
Flag2=0;
UART_1SendOneByte (temp2);
}

}
}
/* * * * * * * * * * * * 1 serial port interrupt handling function * * * * * * * * * * * * */
Void UART_1Interrupt (void) interrupt 4 using 3
{
If (RI)
{
RI=0;
ES=0;//turn off the serial port interrupt,
Uart1_buff [count1]=SBUF;

The switch (Uart1_buff [count1])
{
Case '\ r:
Flag1=1;
Count1=0;
ES=0;
REN=0;
break;
Case '\ n' :
Flag1=1;
Count1=0;
ES=0;
REN=0;
break;
Default:
Count1 + +;
}
ES=1;//to open a serial port interrupt,
}
}
/* * * * * * * * * * * * 2 serial port interrupt handling function * * * * * * * * * * * * */
Void UART_2Interrupt (void) interrupt 8 using 2
{
If (S2CON & amp; S2RI)//determine whether have receive interrupt,
{
S2CON & amp;=0 xfe;//remove serial port receiving interrupt, 2
ES=0;//turn off the serial port interrupt,
Uart2_buff [count2]=S2BUF;
Count2 + +;
ES=1;//to open a serial port interrupt,
}
}

/* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Send a serial port 1 function:
Parameters: the string
The return value: None
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - */
Void Uart_1_SendString (char * P)
{
ES=0;
REN=0;
While (* P!='\ 0')//P is not empty, as long as the * has been sending
{
SBUF=* P;
P++;
while(! TI);
TI=0;
}
REN=1;
ES=1;
}

/* * * * * * * * * * * * * * * * read a result * * * * * * * * * * * * * * * * * * * */
Void read_ac ()
{
Int current=0;
int i;
Clear_Buff ();//remove a serial port 1, 2, buffer
Read_once ();//read a single data
delay_ms(10);
//for (I=0; i here I put a serial port receives the data sent via a serial port 1, 2 but no data
//{
//UART_1SendOneByte (Uart2_buff [I]);
//UART_1SendOneByte (' \ n ');
//UART_1SendOneByte (' * ');
//}
Sprintf (echo, DC: % 1 f "mA", TDM_Receive_Check ());
Uart_1_SendString (echo);
Clear_Buff ();//remove the serial buffer
}
/* * * * * * * * * * * * * * * * reading ammeter function * * * * * * * * * * * * * * * * * * * */
Void read_once ()
{
Uint Sum_check=0;
Sum_check=0 xfe + 0 x02;
UART_2SendOneByte (Code_head1);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related