1. GPIO initialization corresponding to serial port Tx and Rx
Assign corresponding values to the corresponding bits of GPA0CON using C language bit operations.
void uart_init( void )
{
//Initialize the GPIO pins corresponding to Tx and Rx
rGPA0CON &= ~( 0xff << 0 ); // Clear all bits 0 to 7 of the register
rGPA0CON |= 0x00000022; //0b0010, Rx, Tx
}
2. Main control registers such as UCON, ULCON, UMCON, and UFCON
Just set it up according to the values analyzed in the previous section.
void uart_init( void )
{
//Initialize the GPIO pins corresponding to Tx and Rx
rGPA0CON &= ~( 0xff << 0 ); // Clear all bits 0 to 7 of the register
rGPA0CON |= 0x00000022; //0b0010, Rx, Tx
//Settings of several key registers
rULCON0 = 0x3;
rUCON0 = 0x5;
rUMCON0 = 0x0;
rUFCON0 = 0x0;
}
//Baud rate setting DIV_VAL = ( PCLK / (bps x 16) ) - 1 DVI_VAL = (66000000 / (115200 X 16 )) -1 = 34.8 remainder 0.8
//PCLK_PSYS uses 66MHz
//rUBRDIV0 = 34
//rUDIVSLOT0 = 0xdfdd;
//PCLK_PSYS uses 66.7MHz
//DVI_VAL = (66700000 / (115200 X 16 )) -1 = 35.18 remainder 0.18
rUBRDIV0 = 35
//(number of 1s in rUDIVSLOT) / 16 = remainder calculated in the previous step = 0.18
(Number of 1s in rUDIVSLOT) = 16 x 0.18 = 2.88 = 3 (3 1s to find the recommendation table)
rUDIVSLOT0 = 0x0888; //3 1s, look up the table to get this number
3. Calculation and setting of baud rate
(1) The first step is to use PCLK_PSYS and the target baud rate to calculate DIV_VAL = (PCLK / (bps x 16)) - 1
(2) In the second step, the integer part of DIV_VAL is written into the UBRDIV0 register.
(3) In the third step, multiply the decimal part by 16 to get the number of 1s, and then look up the table to get the setting value of the uBDIVSLOT0 register.
4. Writing of serial port sending and receiving functions
//Serial port sending program, send 1 character
void uart_putc( char c )
{
//The serial port sends a character, which actually throws a byte into the send buffer (that is, the UTXH0 register).
rUTXH0 = c;
rUTXH0 = a;
//If you send like the above, the CPU is too fast (because there is no interrupt) and the serial port cannot handle it. At this time, you need to read a status register (UTRSTAT0 register)
//Because the speed at which the serial port controller sends 1 byte is much lower than the speed of the CPU, the CPU must confirm that the current buffer of the serial port controller is empty before sending 1 byte (which means that the serial port has sent the previous byte)
//If the buffer is not empty (UTRSTAT0 register) bit 1 is 0, it should loop until (UTRSTAT0 register) bit 1 is 1.
while( !(rUTRSTAT0 & (1 << 1 ) ) ); //Judge whether the buffer is empty
rUTXH0 = c;
}
(1) Write a sending function. Before sending, use a while loop to wait until the sending buffer is empty.
(2) Serial port receiving program, polling mode, receives one byte
char uart_getc( void )
{
while( !(rUTRSTAT0 & ( 1 << 0 )));
return ( rURXH0 & 0x0f );
}
5. Comprehensive debugging
void main( void )
{
uart_init();
while( 1 ) //keep sending character a
{
uart_putc( 'a' );
delay(); //delay function
{
}
6. Extended Exercise - Debug after changing the baud rate
Check the clock diagram to find the clock source:
Baud rate calculation:
Send and receive status register:
Previous article:Introduction to the interrupt system of S5PPV210
Next article:S5PV210 Serial Communication Programming Practice - 1
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sandia Labs develops battery failure early warning technology to detect battery failures faster
- Application of STM32F03X serial port idle interrupt + DMA
- Bought a very good ESP32-S2 development board
- What is the IC model of infrared remote control transmitter?
- Please help me explain this circuit schematic, how it works, and where each module is.
- How to use printf for debugging
- Comparison between Internal PA and External FEM
- When studying (100 examples of single-chip C language programming practice), I encountered a problem with undefined identifiers. Please tell me how to solve it!
- NCP1252
- [GigaDevice GD32F310 Review] + Differences between GD32F310G and GD32F310K development boards
- Please tell me, what does this RTL_EQ mean? Is it equivalent to an inverter?