First, here is the full code:
C
#include
#define uchar unsigned char
#define uint unsigned int
void send(uchar state)
{
SBUF = state;
while (TI == 0);
TI = 0;
}
void SCON_init(void)
{
TMOD = 0x20; // 6T mode, 11.05992@115200
SCON = 0x50;
PCON = 0x80;
TL1 = 0xFF;
TH1 = 0xFF;
TR1 = 1;
/*SCON = 0x50; // Timer 2 mode, 11.0592@115200
TH2 = 0xFF;
TL2 = 0xFD;
RCAP2H = 0xFF;
RCAP2L = 0xFD;
TCLK = 1;
RCLK = 1;
C_T2 = 0;
EXEN2 = 0;
TR2 = 1;*/
}
void main()
{
SCON_init();
while (1)
{
send('A');
}
}
Second, some notes:
The SCON_init
function is used to configure and initialize the serial port baud rate. It should be called once at the beginning of the main program. In this code, two different methods are shown for setting up the serial communication. The part without comments represents the 6T mode configuration, while the commented section demonstrates the 12T mode using Timer 2.
The choice between 6T and 12T mode is made by the programmer during development, and no special code is needed to switch between them. Note that Timer 2 is only available on certain enhanced 51 microcontrollers, such as the STC89C52RC. This setup ensures reliable serial communication at a specific baud rate, typically 115200 bps in this example.
This code can be modified to send other characters or data by changing the argument passed to the send()
function. It's also common to add error checking or loop control to prevent infinite loops in real-world applications.
Non-slip Heat Shrink Tube,Heat Shrink Tube,Silicone Heat Shrinkable Tube
Dongguan Liansi Electronics Co.,Ltd , https://www.liansisleeve.com