AT91SAM3S serial port UART initialization and data transmission and reception

Publisher:婉如ChanelLatest update time:2016-06-03 Source: eefocusKeywords:AT91SAM3S Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The UART serial port in the SAM3S is a two-wire asynchronous receiver and transmitter. This serial port can be used for communication or tracing. There are two DMA channels associated with the UART serial port, which can save CPU time by using DMA to handle serial port transfers.

There are two UARTs in SAM3S4C. The corresponding relationship with peripheral pins is as follows:

AT91SAM3S serial port UART initialization and data transmission and reception
 

The board uses UART0, and PA9 and PA10 pins.

SAM3S UART block diagram

AT91SAM3S serial port UART initialization and data transmission and reception
 

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.

 

Keywords:AT91SAM3S Reference address:AT91SAM3S serial port UART initialization and data transmission and reception

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

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号