There are two UARTs in SAM3S4C. The corresponding relationship with peripheral pins is as follows:
The board uses UART0, and PA9 and PA10 pins.
SAM3S UART block diagram
The steps for serial port initialization and sending and receiving data are as follows:
1. Configure the pins of the corresponding IO port (set IO clock and pin working mode)
2. Turn on the UART clock
3. Reset and stop UART
4. Set UART function (parity check UART_MR, baud rate UART_BRGR, DMA UART_PTCR, transmit and receive enable UART_CR, etc.)
5. Send and receive data UART_THR UART_RHR
The serial port initialization procedure is as follows:
//Pin macro definition, serial port 0 transmit and receive pins PA9 and PA10 respectively
#define PINS_UART { PIO_PA9A_URXD0PIO_PA10A_UTXD0, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
#define CONSOLE_PINS {PINS_UART}
//Using the macro definition of the serial port, use UART0
#define CONSOLE_USART UART0
extern void UART_Configure( uint32_t baudrate, uint32_t masterClock)
{
const Pin pPins[] = CONSOLE_PINS;
Uart *pUart = CONSOLE_USART;
/* Configure PIO */
PIO_Configure(pPins, PIO_LISTSIZE(pPins)); //Configure corresponding pins
/* Configure PMC */
PMC->PMC_PCER0 = 1 << CONSOLE_ID; //Turn on UART clock
/* Reset and disable receiver & transmitter */
pUart->UART_CR = UART_CR_RSTRX UART_CR_RSTTX
UART_CR_RXDIS UART_CR_TXDIS; //Reset and stop UART
/* Configure mode */
pUart->UART_MR = UART_MR_PAR_NO; //Set parity check (no check)
/* Configure baudrate */
/* Asynchronous, no oversampling */
pUart->UART_BRGR = (masterClock / baudrate) / 16; //Set baud rate
/* Disable PDC channel */
pUart->UART_PTCR = UART_PTCR_RXTDIS UART_PTCR_TXTDIS; //Do not use DMA for both receiving and sending
/* Enable receiver and transmitter */
pUart->UART_CR = UART_CR_RXEN UART_CR_TXEN; //Enable transmission
_ucIsConsoleInitialized = 1; //Set the initialization status
}
The serial port sends a character:
extern void UART_PutChar( uint8_t c )
{
Uart *pUart=CONSOLE_USART;
if ( !_ucIsConsoleInitialized ) // Each time you send data, check whether the serial port has been initialized.
{
UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
}
/* Wait for the transmitter to be ready */
while ( (pUart->UART_SR & UART_SR_TXEMPTY) == 0 ) ; //Wait for the serial port status register to send the empty flag
/* Send character */
pUart->UART_THR=c ; //When the sending status is empty, the character can be thrown into the sending register.
}
The serial port receives a character:
extern uint32_t UART_GetChar( void )
{
Uart *pUart=CONSOLE_USART;
if ( !_ucIsConsoleInitialized ) //Each time you receive data, you must determine whether the serial port has been initialized
{
UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
}
while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 ) ; //Wait for the RXRDY bit in the serial port status register to be set, which indicates that the serial port has completely received a data
return pUart->UART_RHR ; //Read the received data and return. At the same time, the hardware automatically resets the RXRDY bit.
}
After that, you can use it. I only used one sentence in the main function to verify the status of the serial port:
UART_PutChar(UART_GetChar());
That is, send the data from the computer back. After verification, it is completely normal.
Of course, the query method is used here to receive data. As for the interrupt method to receive data, it will be supplemented after learning how to use interrupts.
Previous article:STM32 uses DMA to realize multi-channel ADC channel data acquisition
Next article:STM32F4 SPI2 initialization and data transmission and reception using library functions
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- What kind of feedback is introduced in the following circuit?
- What is the significance of connecting two capacitors in series?
- Storage time of different surface treatment processes for circuit boards
- Upgraded AMG8833 PyGamer thermal imager
- LED Dress
- Future unemployment issues facing electronic engineers
- The relationship between pulse+, pulse- and U2B output is logical AND.
- Southern dumplings and northern dumplings, what do you eat during the Winter Solstice?
- About using the emulator to adjust the STM32 program
- [Intuitive Explanation of Operational Amplifiers] Intuitive animation explains the working principle of operational amplifiers in detail, vivid and easy to understand!