<> Serial port practice

Content from B Station little bee teacher

Overview of serial communication

Calculation of baud rate

Receiving and sending serial port data

Serial port register

practise one

#include "reg52.h" /********************************************
Date:2022-1-13 Author: Xiao Yin Learn From:B Station little bee teacher
*********************************************/ sfr AUXR = 0x8e; // Serial port auxiliary register
unsigned char dat; // Receive data variable
//--------------------------74HC138 initialization --------------------------- void
Init_74HC138(unsigned char n) { switch(n) { case 4: P2 = P2 | 0x1f; P2 = 0x80;
break; case 5: P2 = P2 | 0x1f; P2 = 0xa0; break; case 6: P2 = P2 | 0x1f; P2 =
0xc0; break; case 7: P2 = P2 | 0x1f; P2 = 0xe0; break; } }
//--------------------------------------------------------------------
//----------------------------- System initialization ----------------------------- void
InitSystem(void) { Init_74HC138(5); //Y5 Gating P0 = 0x00; // Turn off the relay and buzzer Init_74HC138(4
); //Y4 P0 = 0xff; // close LED }
//----------------------------------------------------------------------
//--------------------------------- Serial port initialization ------------------------- void Init_Uart
(void) { TMOD = 0x20; // timer 1 Way of 2 Automatic overload 8 position Maximum 256 TH1 = 0xfd; //9600 Baud rate TL1 = 0xfd;
TR1= 1; // Start timer AUXR = 0x00; SCON = 0x50; //8 Bit reception ES = 1; // Serial port interrupt enable EA = 1;
// Interrupt master switch }
//---------------------------------------------------------------------
//----------------------------- Serial port sending function ----------------------------- void SendByte
(unsigned char dat) { SBUF = dat; while(TI == 0); // Wait for sending to complete TI = 0; } void main(
void) { InitSystem(); Init_Uart(); SendByte(0xa5); SendByte(0x5a); while(1) { }
} //----------------------------------- Serial port interrupt service function --------------------- void
ServerUart() interrupt 4 { if(RI == 1) // Reception complete { RI = 0; dat = SBUF; SendByte(dat
+1); } }

Here because of our orders and highlights 0 and 1 Just the opposite Therefore, reverse our orders first and keep consistent with the instructions of the highlights , The upper four digits remain unchanged and only the lower four digits are changed

#include "reg52.h" /********************************************
Date:2022-1-13 Author: Xiao Yin Learn From:B Station little bee teacher
*********************************************/ sfr AUXR = 0x8e; // Serial port auxiliary register
unsigned char command = 0x00; // Receive data variable
//--------------------------74HC138 initialization --------------------------- void
Init_74HC138(unsigned char n) { switch(n) { case 4: P2 = P2 | 0x1f; P2 = 0x80;
break; case 5: P2 = P2 | 0x1f; P2 = 0xa0; break; case 6: P2 = P2 | 0x1f; P2 =
0xc0; break; case 7: P2 = P2 | 0x1f; P2 = 0xe0; break; } }
//--------------------------------------------------------------------
//----------------------------- System initialization ----------------------------- void
InitSystem(void) { Init_74HC138(5); //Y5 Gating P0 = 0x00; // Turn off the relay and buzzer Init_74HC138(4
); //Y4 P0 = 0xff; // close LED }
//----------------------------------------------------------------------
//--------------------------------- Serial port initialization ------------------------- void Init_Uart
(void) { TMOD = 0x20; // timer 1 Way of 2 Automatic overload 8 position Maximum 256 TH1 = 0xfd; //9600 Baud rate TL1 = 0xfd;
TR1= 1; // Start timer AUXR = 0x00; SCON = 0x50; //8 Bit reception ES = 1; // Serial port interrupt enable EA = 1;
// Interrupt master switch }
//---------------------------------------------------------------------
//----------------------------- Serial port sending function ----------------------------- // Send a byte void
SendByte(unsigned char dat) { SBUF = dat; while(TI == 0); // Wait for sending to complete TI = 0;
// Clear flag bit } // Send a string void SendString(unsigned char *str) { // String with '\0' End flag while(
*str != '\0') { SendByte(*str++); } }
//-----------------------------------------------------------------------
//--------------------------------- Receive command processing --------------------------- void
Command_Tackle(void) { if(command != 0x00) { switch(command & 0xf0) { case 0xa0:
P0= (P0 | 0x0f) & (~command | 0xf0); command = 0x00; break; case 0xb0: P0 = (P0
| 0xf0) & ((~command << 4) | 0x0f); command = 0x00; break; case 0xc0: SendString
("xiao yin students\r\n"); command = 0x00; break; } } } void main(void) {
InitSystem(); Init_Uart(); while(1) { Init_74HC138(4); Command_Tackle(); } }
//----------------------------------- Serial port interrupt service function --------------------- void
ServerUart() interrupt 4 { if(RI == 1) // Reception complete { RI = 0; // Clear flag bit command = SBUF;
} }
Special attention should be paid to : Don't forget it AUXR Register configuration

Technology