Then the pin selection is set to serial port mode.
Then set the serial port transmission data format.
Baud rate setting, (note that the baud rate is different when there are decimal places and when there are no decimal places. If there are decimal places, DIVADDL and MULVAL need to be set).
The baud rate is then latched.
Settings control the operation of UARTn RX and TX FIFO.
Finally, turn on the interrupt of UART0 in the system and enable the receive interrupt.
See the code for details, which is commented in detail.
-
-
- #include LPC17XX.h
-
-
- #define
FOSC 12000000 - #define
FCCLK (FOSC * 8) -
- #define
FCCO (FCCLK * 3) -
- #define
FPCLK (FCCLK / 4) -
-
- int
Uart0RecvBuf; // UART0 RX DATA -
- void
Uart0Init(uint32_t bps) - {
-
LPC_SC->PCONP |= (1<<3); //Turn on UART0 power and clock control bits -
-
LPC_PINCON->PINSEL0 = 0X00000050; //P0.2 P0.3 are set to serial port -
-
LPC_UART0->LCR = 0x83; //Set the serial port data format, 8-bit character length, 1 stop bit, no checksum, -
// Enable access to the divisor latch and set the baud rate -
LPC_UART0->DLM = ((FPCLK/16)/bps) / 256; //The divisor has eight high bits , no decimals -
LPC_UART0->DLL = ((FPCLK/16)/bps) % 256; //The eighth digit of the divisor -
-
LPC_UART0->LCR = 0x03; //Disable access to the divisor latch and lock the baud rate -
-
LPC_UART0->FCR = 0x07; //Control the operation of UARTn RX and TX FIFO. Receive one byte of data to trigger an interrupt -
-
NVIC_EnableIRQ(UART0_IRQn); -
LPC_UART0->IER = 0x01; //Enable receive interrupt - }
-
- int
Uart0RecvByte(void) //Query method - {
-
//When UnRBR contains unread characters, UnLSR[0] will be set; when UARTn RBR FIFO is empty, UnLSR[0] will be cleared -
//0 - UnRBR is empty -
//1 - UnRBR contains valid data -
while(!((LPC_UART0->LSR) & 0x01)); //Wait to determine whether LSR[0] is 1. If it is 1, it means that data has been received in RBR -
-
return(LPC_UART0->RBR); //Read received data - }
-
- int
Uart0SendByte(int buf) - {
-
//When UARTn THR is detected to be empty, THRE will be set immediately. Writing UnTHR will clear THRE -
//0 - UnTHR contains valid characters -
//1 - UnTHR is empty -
while(!((LPC_UART0->LSR) & 0x01)); //Wait to determine whether LSR[5] (THRE) is 1. If it is 1, it means THR is empty. -
-
LPC_UART0->THR = buf; //Send data -
-
return 0; - }
-
- void
UART0_IRQHandler(void) - {
-
Uart0RecvBuf = LPC_UART0->RBR; //Read received data -
-
Uart0SendByte(Uart0RecvBuf); //Send received data - }
-
-
- int
main(void) - {
-
int temp; -
-
SystemInit(); -
Uart0Init(115200); -
-
while(1) -
{ -
-
// temp = Uart0RecvByte(); //Query received data -
// Uart0SendByte(temp); //Send data -
-
} -
- }
Previous article:LPC1768 CPU clock configuration
Next article:ARM boot process (Cortex-M3 NXP LPC1768 as an example)
Recommended ReadingLatest update time:2024-11-16 23:48
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- 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
- Everspin MRAM optimizes system energy consumption
- The impact of 5g wireless network on the development of e-sports market
- Anlu SparkRoad Development Board Review (9) Use of on-chip SDRAM, TD debugging function
- Introduction to the method of board-level online compilation and downloading of C6000 DSP code
- Disassembly and analysis of GoodWe three-phase 15kW grid-connected inverter
- [ATmega4809 Curiosity Nano Review] Buttons
- Have you fallen into these customer support "pitfalls"? Read the story, write a comment, grab a post and win a gift!
- About rail-to-rail op amps
- 【TGF4042 signal generator】+ Load capacity test
- EEWORLD University - In-depth understanding of PCI Express 5.0 testing