#ifdef __USE_CMSIS #include "LPC11xx.h" #endif #include <cr_section_macros.h> #include <NXP/crp.h> __CRP const unsigned int CRP_WORD = CRP_NO_CRP ; #define SYSTEM_CLOCK_IN_MHZ 36 //main clock in MHz. #define FALSE 0 #define LSR_THRE 0x20 #define LSR_TEMT 0x40 #define LSR_RDR 0x01 #define TEMT (1<<6) #define LINE_FEED 0xA #define CARRIAGE_RET 0xD void setup_power(void) { LPC_SYSCON->PDRUNCFG = 0b1110110101000011; //Power-up: Flash // BOD // ADC // System oscillator // System PLL //Power down: RC oscillator // RC oscillator output // Watchdog oscillator } void setup_clocks(void) { LPC_SYSCON->SYSAHBCLKDIV = 0b1; //System clock divide to the peripherals (timers etc.) => System clock = main system clock. LPC_SYSCON->SYSAHBCLKCTRL = 0b00010011111011111; //enable clock to: ROM // RAM // Flash register interface // Flash array access // GPIO // 16-bit timer 0 // 16-bit timer 1 // 32-bit timer 0 // 32-bit timer 1 // ADC // I/O configuration block //Disable clock to: I2C // SPI0 // UART // WDT // CAN // SPI1 } void setup_main_clock(void) { //set up for 12MHz Crystal (XTAL) and PLL for 24MHz, 36MHz or 48MHz system clock: LPC_SYSCON->MAINCLKSEL = 0b0; //select main system clock = IRC output for not to remove system clock from system: LPC_SYSCON->MAINCLKUEN = 0b0; // LPC_SYSCON->MAINCLKUEN = 0b1; // while((LPC_SYSCON->MAINCLKUEN & 0b1) == FALSE); //wait until updated. LPC_SYSCON->PDRUNCFG |= 1 << 7; //power-down SYSPLL. #if SYSTEM_CLOCK_IN_MHZ == 24 LPC_SYSCON->SYSPLLCTRL = 0b1000001; //Set PLL to Fout = 24MHz. #elif SYSTEM_CLOCK_IN_MHZ == 36 LPC_SYSCON->SYSPLLCTRL = 0b1000010; //Set PLL to Fout = 36MHz. #else //SYSTEM_CLOCK_IN_MHZ = 48: LPC_SYSCON->SYSPLLCTRL = 0b11; //Set PLL to Fout = 48MHz. #endif LPC_SYSCON->SYSPLLCLKSEL = 0b1; //select 12MHz crystal (XTAL) for the PLL input: LPC_SYSCON->SYSPLLCLKUEN = 0b0; // LPC_SYSCON->SYSPLLCLKUEN = 0b1; // while((LPC_SYSCON->SYSPLLCLKUEN & 0b1) == FALSE); //wait until updated. LPC_SYSCON->PDRUNCFG &= ~(1 << 7); //power-up SYSPLL. while((LPC_SYSCON->SYSPLLSTAT & 0b1) == FALSE); //wait for PLL to lock on system (XTAL) oscillator. LPC_SYSCON->MAINCLKSEL = 0b11; //select main system clock = PLL output: LPC_SYSCON->MAINCLKUEN = 0b0; // LPC_SYSCON->MAINCLKUEN = 0b1; // while((LPC_SYSCON->MAINCLKUEN & 0b1) == FALSE); //wait until updated. #if DEBUG_MODE LPC_SYSCON->CLKOUTDIV = 0b11000; //enable CLKOUT output and divide selected clock with 24. LPC_SYSCON->CLKOUTCLKSEL = 0b10; //WDT clock to CLKOUT. LPC_SYSCON->CLKOUTUEN = 0b0; LPC_SYSCON->CLKOUTUEN = 0b1; #endif } void us_delay(uint32_t usec) { if(usec < 3) LPC_TMR32B0->MR0 = SYSTEM_CLOCK_IN_MHZ; else LPC_TMR32B0->MR0 = (usec * SYSTEM_CLOCK_IN_MHZ) - ((SYSTEM_CLOCK_IN_MHZ * 17)/10); LPC_TMR32B0->TCR = 0b1; //start timer32-0. while(LPC_TMR32B0->TCR & 0b1); //wait for time to expire. LPC_TMR32B0->TCR = 0b10; //clear (TC) timer32-0. } void UART_Init(unsigned int baudrate) { unsigned int regVal; unsigned int Fdiv; LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16); /* Enable IOCON block */ LPC_IOCON->PIO1_6 &= ~0x07; //Clear the first 3 bits in the FUNC of the 'PIO1_6' LPC_IOCON->PIO1_6 |= 0x01; //Set PIO1_6 as UART RXD LPC_IOCON->PIO1_7 &= ~0x07; //Clear the first 3 bits in the FUNC of the 'PIO1_7' LPC_IOCON->PIO1_7 |= 0x01; //Set PIO1_7 as UART TXD /*Enable UART Clock*/ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); //Enable UART clock LPC_SYSCON->UARTCLKDIV = 0x01; //Divide by 1 LPC_UART->LCR = 0x83; //8 bits, no parity, 1 stop bit, DLAB(divisor latch access bit) = 1 regVal = LPC_SYSCON->UARTCLKDIV; Fdiv = (((SystemCoreClock)/regVal)/16)/baudrate ; /*baud rate */ LPC_UART->DLM = Fdiv / 256; LPC_UART->DLL = Fdiv % 256; LPC_UART->LCR = 0x03; /* Diable latch access bit DLAB = 0 */ //Skal være 0 for at kunne skrive til THR LPC_UART->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ /* Read to clear the line status. */ regVal = LPC_UART->LSR; /* Ensure a clean start, no data in either TX or RX FIFO. */ while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) ); while ( LPC_UART->LSR & LSR_RDR ) { regVal = LPC_UART->RBR; /* Dump data from RX FIFO */ } } void UARTSend(uint8_t *BufferPtr, uint32_t Length) { //while ( !(LPC_USART->LSR & LSR_TEMT) ); while ( Length != 0 ) { while ( !(LPC_UART->LSR & LSR_THRE) ); LPC_UART->THR = *BufferPtr; BufferPtr++; Length--; } return; } int main(void) { unsigned char buff[4] = {0x30,0x31,0x32,0x33}; //LPC_GPIO3->DIR = 4; //LPC_GPIO3->DATA = 4; UART_Init(9600); setup_main_clock(); setup_power(); setup_clocks(); setup_timers(); while(1) { while ( !(LPC_UART->LSR & LSR_THRE) ); LPC_UART->THR = 'A'; } return 0 ; } |
[COLOR=Red]UART_Init(9600);[/COLOR] setup_main_clock(); setup_power(); [COLOR=Red] setup_clocks();[/COLOR] setup_timers(); |