LPC54100 LPC54101 LPC54102 LPC541xx series serial port usage

Publisher:温柔阳光Latest update time:2019-07-08 Source: eefocusKeywords:LPC54100 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

LPC54100 LPC54101 LPC54102 LPC541xx series serial port usage

(Using library, interrupt mode, no FIFO)

// Initialization function

void uart_init(baudRate)

{

    UART_BAUD_T baud;

    UART_CFG_T cfg;


    // Enable the clock

    Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_FRG);


    //Configure baud rate

    //Get the clock frequency

    baud.clk = Chip_Clock_GetAsyncSyscon_ClockRate();

    //Desired baud rate

    baud.baud = baudRate;

    baud.ovr = 0;

    baud.mul = 0;

    baud.div = 0;

    if (ROM_UART_CalBaud(&baud) != LPC_OK) {

        // Failed to calculate the frequency division coefficient

        printf("Error calculating baud ratern");

    }

    /* Set the fractional divider */

    Chip_SYSCON_SetUSARTFRGCtrl(baud.mul, 0xFF);


    // Enable serial port 0 clock

    Chip_Clock_EnablePeriphClock(SYSCON_CLOCK_USART0);

    //Configure IO ports P0.0 and P0.1 as serial port functions

    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 0, (IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_DIGITAL_EN));

    Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 1, (IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_DIGITAL_EN));

    //Get the space required by the serial port

    size = ROM_UART_GetMemSize();

    //Apply for the space required by the serial port

    drv_mem = (uint32_t *)malloc(size);

    //Call the initialization function to initialize serial port 0

    UART0 = ROM_UART_Init(drv_mem, LPC_USART0_BASE, NULL);


    //Configure the serial port

    //8-bit data enable receive interrupt

    cfg.cfg = UART_CFG_8BIT | UART_CFG_BRKRX;

    //Set the frequency division value, directly use the result calculated by ROM_UART_CalBaud

    cfg.div = baud.div;

    cfg.ovr = baud.ovr;

    cfg.res = UART_BIT_DLY(baudRate);

    //Configure serial port 0

    ROM_UART_Configure(UART0, &cfg);


    // Enable interrupt

    NVIC_ClearPendingIRQ(UART0_IRQn);

    NVIC_EnableIRQ(UART0_IRQn);

    LPC_USART0->INTENSET |= (1<<0);

}


//Interrupt service function

void UART0_IRQHandler(void)

{   

    uint8_t that;


    //Send interrupt

    if (LPC_USART0->INTSTAT & (1<<2))

    {

        //Wait for sending to complete

        ROM_UART_WaitTx(UART0);

    }

    //Receive interrupt

    if (LPC_USART0->INTSTAT & (1<<0))

    {

        //Read data

        dat = LPC_USART0->RXDAT;

        printf("%c", dat);

    }

}



Keywords:LPC54100 Reference address:LPC54100 LPC54101 LPC54102 LPC541xx series serial port usage

Previous article:Bare metal program 2: GPIO lights up the LED lamp assembly
Next article:Can both Cortex-M3 and Cortex-M0 operate with bit-banding?

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号