#include }; While(1); } waka Hongkong Onice Limited , https://www.ousibangvape.com
#define BUFFERLEGTH 10
//------------------------------------------------ -----------------
Void UART_init(); //Serial port initialization function
Void COM_send(void); //Serial port send function
Char str[20];
Char j;
//------------------------------------------------ -------------------
Void main(void)
{
Unsigned char i;
UART_init();
j=0; //Initialize the serial port
For(i = 0;i < 10 ;i++)
{
COM_send(); //Send data once for testing
}
//------------------------------------------------ -------------
//------------------------------------------------ --------------------------------------------------
// Function name: UART_init() serial port initialization function
// Function: When the system clock is 11.059MHZ, set the serial port baud rate to 9600bit/s.
// Serial port receive interrupt is allowed, send interrupt is forbidden
//------------------------------------------------ --------------------------------------------------
Void UART_init()
{
/ / Initialize the serial port and baud rate generator
SCON =0x50; //Select the serial port working mode 1, open the receiving permission
TMOD =0x20; //Timer 1 works in mode 2, timer 0 works in mode 1
TH1 =0xfA; // achieve baud rate 9600 (system clock 11.0592MHZ)
PCON = 0x80;
TR1 =1; //Start timer T1
ET1 =0;
ES=1; //Allow serial port interrupt
PS=1; //Design serial port interrupt priority
EA =1; //MCU interrupt allowed
}
//------------------------------------------------ ------------
Void COM_send(void)
{
Unsigned char point = 0;
For(point=0;str[point]!="\0";point++) //Continuously send 20 bits of data // Send the data in the buffer to the serial port
{
SBUF=str[point];
While(!TI);
TI=0;
//str[point]="\0";
}
//------------------------------------------------ --------------
//------------------------------------------------ --------------------------------------------------
// Function name: com_interrup() serial port receive interrupt handler
/ / Function: Receive ten bits of data including the start bit "S" to the data buffer
//------------------------------------------------ --------------------------------------------------
Void com_interrupt(void) interrupt 4 using 3
{
Unsigned char RECEIVR_buffer;
Bit flag=1;
If(RI) //Processing receive interrupts
{
RI=0; //Clear interrupt flag
RECEIVR_buffer=SBUF; //Receive serial port data
Str[j]=SBUF;
If (RECEIVR_buffer == "$")
{ ES=0;
Str[j]="\0";
SCON =0x40; //Receiving is not allowed
COM_send(); //Send data
ES=1;
j=0;
Flag=0;
SCON=0x50; //Receive permission
}
If(flag)
j++;
}
}