LPC2368 UART init

void uartInit(unsigned long baudrate)
{
    /*------------------------------------------------
    Setup serial port registers.
    ------------------------------------------------*/
    PCONP |= (1<<3); //  PCUART0 UART0 power/clock control bit.

    PINSEL0 |= (1<<(2*2)) | (1<<(2*3)); /* Enable TXD0, RXD0 */

    U0LCR = 0x03;            /* 8 bits, no Parity, 1 Stop bit */
    U0IER = 0;               /* Disable UART1 Interrupts */   

    U0FCR = 0x07;

    unsigned long DLreload;

    DLreload = ((Fpclk / 16UL) + (baudrate-1)) / baudrate;

    U0LCR |= 0x80;           /* Set DLAB */
    U0DLL = DLreload;
    U0DLM = (DLreload >> 8);
    U0LCR &= ~0x80;          /* Clear DLAB */
}

char uartGetc(void)
{
    while (!(U0LSR & 0x01)); // Receiver Data Ready (RDR)
    return U0RBR;
}

void uartPutc(char ch)
{
    while (!(U0LSR & 0x20)); // Transmitter Holding Register Empty (THRE)
    U0THR = ch;
}

void uartPuts(char* str)
{
    char ch;
    while((ch = *str++) != 0)
    {
        while (!(U0LSR & 0x20)); // Transmitter Holding Register Empty (THRE)
        U0THR = ch;
    }
}

void uartPutHex(char hex)
{
    if((hex>>4) < 10)
        uartPutc((hex>>4) + '0');
    else
        uartPutc((hex>>4) + 'A'-10);

    if((hex&15) < 10)
        uartPutc((hex&15) + '0');
    else
        uartPutc((hex&15) + 'A'-10);

}

void uartPutHexLong(unsigned long hex)
{
    uartPutHex((hex>>24) & 0xFF);
    uartPutHex((hex>>16) & 0xFF);
    uartPutHex((hex>>8) & 0xFF);
    uartPutHex((hex>>0) & 0xFF);
}
 
comments powered by Disqus