LPCOpen LPC13xx UART

With the UART we have an easy to use interface to other devices in example our computer.

In this example I used the lpc1313 and a ft232 cable to communicate between chip and computer. The lpc1313 sends each second a „Hello“.

The connection setup is:

LPC1313 -> FT232

RxD -> TxD

TxD -> RxD

GND -> GND

// Tobias Markus
#include "chip.h"
#include <cr_section_macros.h>
 
STATIC const PINMUX_GRP_T pinmuxing[] = {
    {(uint32_t)IOCON_PIO1_6,  
    (IOCON_FUNC1 | IOCON_RESERVED_BIT_6 | IOCON_RESERVED_BIT_7 
        | IOCON_MODE_INACT)}, /*RXD*/
    {(uint32_t)IOCON_PIO1_7,  
    (IOCON_FUNC1 | IOCON_RESERVED_BIT_6 | IOCON_RESERVED_BIT_7 
        | IOCON_MODE_INACT)}, /*TXD*/
};
 
const uint32_t OscRateIn = 12000000;
volatile uint32_t msTicks=0;
 
void SysTick_Handler(void)
{
    msTicks++;
}
 
__INLINE static void Delay (uint32_t dlyTicks) {
    uint32_t curTicks;
 
    curTicks = msTicks;
    while ((msTicks - curTicks) < dlyTicks);
}
 
static void Init_UART_PinMux(void)
{
    Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON);
    Chip_IOCON_SetPinMuxing(LPC_IOCON,pinmuxing,
        sizeof(pinmuxing)/sizeof(PINMUX_GRP_T));
}
 
int main(void) {
 
    SysTick_Config(72000000 / 1000);
 
    /* Setup UART */
    Init_UART_PinMux();
    Chip_UART_Init(LPC_USART);
    Chip_UART_SetBaud(LPC_USART, 115200);
    Chip_UART_ConfigData(LPC_USART, 
        (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
    Chip_UART_TXEnable(LPC_USART);
 
    NVIC_SetPriority(UART0_IRQn, 1);
    NVIC_EnableIRQ(UART0_IRQn);
 
    while(1)
    {
    	Chip_UART_SendBlocking(LPC_USART,(uint8_t*) "Hello\n",7);
        Delay(1000);
    }
    return 0 ;
}

In the above example we have again the Systick handler and the delay function for time reference.

In the beginning we set with the OscRateIn the frequency for the lpcopen in our case 12 MHz.

The Init_UART_PinMux(); Function sets the IOCON Register and configures the UART Pins to TxD and RxD.

In the main function Chip_UART_Init(LPC_USART_T *pUART); is called. This function initialise the UART peripheral. For this the function needs the UART base address then it enables the clock for the UART and sets up a default configuration.

In the next two function we set our own configuration. With Chip_UART_SetBaud(LPC_USART_T *pUART, uint32_t baudrate); we set the baudrate we want. In our case 115200, This function needs the base address too. With the Chip_UART_ConfigData(LPC_USART_T *pUART, uint32_t config); function we configure the UART. In our configuration we set the UART to 8 databits , no parity and  one stop bit.

Now we enable the transmit line with Chip_UART_TXEnable(LPC_USART);.

In the next step we enable the UART interrupts. And then enter the main loop here we wait 1s and then send with Chip_UART_SendBlocking(LPC_USART_T *pUART, const void *data, int numBytes); our „Hello“ via the uart. This function blocks until the data is transmitted. The function needs the UART base address, the data pointer and the number of Bytes which should be transmitted.

The UART is an important peripherial of the device with it you have a communication interface and a simple debug interface to your controller. For more detailed description of the UART peripherial see the user manual on page 182.

 

Dieser Beitrag wurde unter ARM lpc1xxx, Mikrocontroller abgelegt und mit , , , , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.