Content originally posted in LPCWare by Ivan on Mon Jun 20 23:59:49 MST 2011
This is my code:
/*
===============================================================================
Name : main.c
Author : Ivan
Version : 1.0
Copyright : Copyright (C) 2010 noritan.org
Description : main definition
===============================================================================
*/
#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif
#define SystemFrequency SystemCoreClock
#include <NXP/crp.h>
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
// Flag mask for UART_LSR register.
const uint32_t LSR_THRE = 0x20;
// Initialize UART module with specified baud-rate
void uart_init(uint32_t baudrate) {
// Divisor ratio
uint32_t Fdiv;
uint32_t regVal;
// Configure P1[7] as TXD output.
LPC_IOCON->PIO1_7 &= ~0x07; // FUNC=000 (GPIO)
LPC_IOCON->PIO1_7 |= 0x01; // FUNC=001 (TXD)
// Enable UART clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); // UART=1
// Enable UART peripheral clock
LPC_SYSCON->UARTCLKDIV = 0x01; // DIV=1
// Calculate baud rate divisor
regVal = LPC_SYSCON->UARTCLKDIV;
Fdiv = (((SystemFrequency/LPC_SYSCON->SYSAHBCLKDIV)/regVal)/16)/baudrate ;
// Set the baud rate divisor value
LPC_UART->LCR |= 0x83;
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->LCR &= ~0x83;
// Configure UART module as
// 8 bit, 1 stop bit, no parity
// Enable and reset TX and RX FIFO.
LPC_UART->LCR = 0x03;
LPC_UART->FCR = 0x07;
}
// send a character via TXD
void uart_putc(const char c) {
// Wait for TX buffer empty
while (!(LPC_UART->LSR & LSR_THRE));
// Put a character
LPC_UART->THR = c;
}
// send a string via TXD
int uart_puts(const char *s) {
int n;
for (n = 0; *s; s++, n++) {
uart_putc(*s);
}
return n;
}
// Main loop
int main(void) {
uart_init(115200);
uart_puts("ciao");
// Enter an infinite loop, just incrementing a counter
volatile static int i = 0 ;
while(1) {
i++ ;
}
return 0 ;
}