1. Basic knowledge
The serial port is one of the most basic functions of various chips. After the serial port is configured, the program can print various information through the serial port, which is convenient for code debugging. The main parameters of the serial port are 4: baud rate, data width, check bit, stop bit
Common serial port configurations are: baud rate 115200, 8-bit data width, no parity bit, 1-bit stop bit, usually abbreviated as 115200 8N1
2440 has 3 sets of UARTs, each set of UARTs has 2 64Bytes FIFOs
jz2240 uses serial port 0 to communicate with the computer, and PL2303 is used to convert the logic level between the computer and the development board.
2. Main registers
2.1 GPHCON & GPHUP
The serial port needs to use three pins: Tx, RX, GND, so first you need to set the corresponding pins to TX, RD mode, and in addition, you need to turn on the pull-up function because the UART is high level when idle.
2.2 ULCON0
ULCON0 is used to control the basic properties of the serial port. Here, 8N1 in 115200 8N1 is set.
2.3 UCON0
ULCON mainly sets the clock source and various working modes
If FLCK is selected as the clock, there are many restrictions on the configuration, so it is more convenient to select PCLK here.
Select polling mode for both sending and receiving, so you need to check the status register manually.
2.4 UTRSTAT0
Status register. In polling mode, you can check the value of this register to determine the status of data transmission and reception.
When sending data, you need to first check whether UTRSTAT0[2] is empty. The logic code is:
{
/* Wait for the trasnmitter to be empty, otherwise it means there is still data left to be sent*/
while (!(UTRSTAT0 & 0x4))
send data
}
When receiving data, you need to first check whether UTRSTAT0[0] is not empty. The logic code is:
{
/*You need to wait until the Receive buffer is not empty, which means that data has been obtained, otherwise keep waiting*/
while (!(UTRSTAT0 & 0x1));
Receive data
}
2.5 UTXH0
When sending data, fill the data to be sent into this register
2.6 URXH0
When receiving data, this register is used to obtain the data.
2.7 OFFENSIVE0
Baud rate setting, the formula is: UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
UART clock is set to PLCK=50M
Expected buad rate = 115200
Substituting into the formula (50M / (115200 x 16)) - 1 = 27.12 - 1 = 26
Because division may have decimals, the actual baud rate will have errors, but 2440 provides the error calculation formula and the maximum error range.
The error is calculated as:
tUPCLK = (UBRDIVn + 1) x 16 x 1Frame / PCLK
tUEXACT = 1Frame / baud-rate
UART error = (tUPCLK – tUEXACT) / tUEXACT x 100%
UBRDIVn = 26,
1Frame = start bit + data bit + parity bit + stop bit. Because each time 1 Byte is transmitted, there will be 1 start bit.
1Frame = 1 + 8 + 0 + 1 = 10,
tUPCLK = (26 + 1)* 16 * 10 / 50M = 432 / 5M
tUEXACT = 10 / 115200 = 1 / 11520
UART error = (432 / 5M - 1 / 11520) / (1 / 11520) * 100% = -0.446%
3. Source code
uart.c
int uart0_init(void)
{
/* 1 set gpio to uart mode
Tx GPH2 [5:4] b10
Rx GPH3 [7:6] b10
*/
GPHCON &= ~((0x3 << 4) | (0x3 << 6));
GPHCON |= (0x2 << 4) | (0x2 << 6);
/* 2 set pull up */
GPHUP |= (0x1 << 2) | (0x1 << 3);
/* 3 set form 8N1 */
ULCON0 |= 0x3;
/* 4 set Soruce clock PCLK and poling mode*/
UCON0 = 0x5;
/* 5 set baud rate 115200
PCLK = 50M
UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
UBRDIVn = (50M / (115200 x 16))-1 = 26.1267 -> 26
*/
OFFENSIVE0 = 26;
return 0;
}
int getchar(void)
{
int chr = 0;
while (!(UTRSTAT0 & 0x1))
{
//nothing
}
chr = URXH0;
return chr;
}
int putchar(int c)
{
while (!(UTRSTAT0 & 0x4))
{
//nothing
}
UTXH0 = (unsigned char )c;
}
The implementation of uart0_init, getchar, putchar is very simple, so I won't introduce it in detail.
The only thing to note is that when sending data, it is best to convert the data into unsigned char to avoid abnormal data transmission when the ascii of the character exceeds 127 and becomes a negative number.
Previous article:s3c2440 learning path-008 uart implements printf function
Next article:s3c2440 learning path-006 clock settings
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Design of automotive LIN communication simulator based on Renesas MCU
- When will solid-state batteries become popular?
- Adding solid-state batteries, CATL wants to continue to be the "King of Ning"
- The agency predicts that my country's public electric vehicle charging piles will reach 3.6 million this year, accounting for nearly 70% of the world
- U.S. senators urge NHTSA to issue new vehicle safety rules
- Giants step up investment, accelerating the application of solid-state batteries
- Guangzhou Auto Show: End-to-end competition accelerates, autonomous driving fully impacts luxury...
- Weifu debugging software E2000w version 3.2
- Precision control techniques and methods for PCB board milling
- 51 MCU SPI communication reads SD card data, SD card content cannot be read
- EEWORLD大学堂----Modern Robotics: Mechanics, Planning, and Control
- Before the competition, I took some time to post some information about power supplies. I hope you can share it with me.
- 3G construction, beware of duplication
- Microcontroller Basics
- HP launches five new inkjet all-in-one printers
- Analysis of the top ten popular microprocessor and memory new products in 2006
- Teach you how to learn DSP: Based on TMS320F28335