/*************************************
* Serial Communication Experiment
* Oscillator 11.0592 MHz
* Baud rate 9600bps
* Interrupt mode implementation: the microcontroller receives computer data and sends it back to the computer after adding 1
*
**************************************/
# Include <reg52.h>
Unsigned char dat;
*******************/
Void uart_config (unsigned long Baud, CY) // baud rate, Cy Crystal Oscillator
{
Tmod = 0x20; // The timer 1 working mode. The default value is.
Th1 = 256-(CY/(12*32 * baud ));
TL1 = 256-(CY/(12*32 * baud ));
Tr1 = 1;
Sm0 = 0; // The sm0 and SM1 values determine the serial port working mode.
SM1 = 1;
Ren = 1; // allows receiving data
Ea = 1; // total disconnection
Es = 1; // enable serial port interruption
}
*****************/
Int main ()
{
Uart_config (9600,11059200 );
While (1)
{
}
Return 0;
}
Void interrupt_uart () interrupt 4
{
If (RI) // Detection If Ri = 1, the data is received,
{
Ri = 0; // set the software to 0
Dat = sbuf; // read the received data to dat;
Dat ++; // Add 1 to the data
Sbuf = dat; // data is automatically sent to sbuf.
Ren = 0; // prohibited from receiving (prohibited from receiving data when sending data)
}
If (Ti) // Detection If Ti = 1, the data is sent completely.
{
Ti = 0; // Ti 0
Ren = 1; // enable accept
}
}
Daddy's amateur MCU Study: UART Serial Communication Study Notes (2)