A microcontroller UART LPC2138

Chapter VII UART

Features introduced

LPC2138 UART microcontroller has two controllers, each controller has UART Rx & Tx FIFO (16 bytes each), Rx FIFO trigger an interrupt may be provided when receiving 1/4/8/14 bytes.

UART controller is the reference clock PCLK.

UART controller principle

As can be seen from the schematic internal UART controller, UART interrupt controller is configured mainly by a portion (U0IER & U0IIR), frame control (U0LCR), baud rate configuration (U0FDR & U0DLL & U0DLM), FIFO configuration (U0FCR), sending and receiving (U0THR & U0RBR) of several parts.

They perform their duties, the UART initialization, the U0FDR, value U0DLL, U0DLM PCLK calculated by the first frequency and baud rate, then the configuration data U0LCR frame format (e.g., 8 data bits, one stop bit, no parity bit), then configure the FIFO mode of operation (including the number of bytes received trigger an interrupt), and finally enable interrupts by U0FCR. There is also the realization of read and write functions and interrupt service functions.

The baud rate is calculated as follows:

UART protocol

UART protocol please refer to my previous write a blog

Debug UART serial port instances

#include <lpc213x.h>
#include "serial.h"

#define CR 0x0D

void serial1_isr(void) __irq
{
    /*  The U1IIR must be read in order to
    **  clear the interrupt prior to exiting the
    **  Interrupt Service Routine.
    */
    if ((U1IIR & 0x0E) == 0x04)
    {
        /* Receive Interrupt */
        sendchar(U1RBR);
    }

    if (U1LSR & 0x20)
    {
        /* Transmit Interrupt */
    }
}

void serial_init(void)
{
    /* configure P0.8~9 as TXD1 & RXD1 */
    PINSEL0 &= 0xFFF0FFFF;
    PINSEL0 |= 0x00050000;

    /* 8bits, no Parity, 1 Stop bit */
    U1LCR = 0x83;

    /* 9600 bauds while PCLK is 30MHz */
    U1DLL = 0xC3;
    U1DLM = 0x00;
    U1FDR = 0x10;

    /* DLAB = 0 */
    U1LCR = 0x03;

    /* Reset & Enable FIFO, configure Rx FIFO trigger level 0 (1 character) */
    U1FCR = 0x07;

    /* Enable RDA & THRE Interrupts */
    U1IER = 0x03;

    /* UART1 ISR */
    VICVectCntl1 = 0x20 | 7;
    VICVectAddr1 = (unsigned int)serial1_isr;
    VICIntEnable = 0x80;

}

int sendchar(char ch)
{
    if ('\n' == ch)
    {
        while (!(U1LSR & 0x20));
        U1THR = CR;
    }

    while (!(U1LSR & 0x20));
    return (U1THR = ch);
}

void sendhex(char hex)
{
    int i = 0;
    char half[2] = {0};

    half[0] = hex >> 4;
    half[1] = hex & 0x0F;

    sendstr("0x");

    for (i = 0; i < 2; i++)
    {
        if (half[i] > 9)
            sendchar('A' + (half[i] - 10));
        else
            sendchar('0' + half[i]);
    }

}

void sendstr(char *p)
{
    while (*p)
        sendchar(*p++);
}

Guess you like

Origin www.cnblogs.com/justin-y-lin/p/12340711.html