首页 > 代码库 > LPC1768串口时钟配置全解析
LPC1768串口时钟配置全解析
外设频率是一个比较难把握的知识,请按照我的理解 不可相信其他教程
晶振位12MHZ
配置system_LPC17xx.c 参数值如下:
#define CLOCK_SETUP 1
#define SCS_Val 0x00000020 //系统时钟源 ->12M晶振
#define CLKSRCSEL_Val 0x00000001
#define PLL0_SETUP 1
#define PLL0CFG_Val 0x00050063 //M N位锁相环倍频参数分别为6和100
#define PLL1_SETUP 1
#define PLL1CFG_Val 0x00000023
#define CCLKCFG_Val 0x00000003 //锁相环输出分频位CPU时钟 CCLK=FCCO/4;
#define USBCLKCFG_Val 0x00000000 //无USB
#define PCLKSEL0_Val 0x00000000//0x00000140// 外设在CPU时钟的基础上选择是否为主时钟的1/4
#define PCLKSEL1_Val 0x00000000//0x00050000// 是否选择UART2、3为主时钟的1/4
#define PCONP_Val 0x042887DE
#define CLKOUTCFG_Val 0x00000000
系统初始化函数:
void SystemInit (void)
{
#if (CLOCK_SETUP) /* Clock Setup */
LPC_SC->SCS = SCS_Val;
if (SCS_Val & (1 << 5)) { /* If Main Oscillator is enabled */
while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
}
LPC_SC->CCLKCFG = CCLKCFG_Val; /* Setup Clock Divider */
#if (PLL0_SETUP)
LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for PLL0 */
LPC_SC->PLL0CFG = PLL0CFG_Val; /* configure PLL0 set M and N Value */
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
LPC_SC->PLL0CON = 0x01; /* PLL0 Enable Lock M and N Value */
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & (1<<26)));/* Wait for PLOCK0 STAT include M and N */
LPC_SC->PLL0CON = 0x03; /* PLL0 Enable & Connect */
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));/* Wait for PLLC0_STAT & PLLE0_STAT */
#endif
#if (PLL1_SETUP)
LPC_SC->PLL1CFG = PLL1CFG_Val;
LPC_SC->PLL1FEED = 0xAA;
LPC_SC->PLL1FEED = 0x55;
LPC_SC->PLL1CON = 0x01; /* PLL1 Enable */
LPC_SC->PLL1FEED = 0xAA;
LPC_SC->PLL1FEED = 0x55;
while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1 */
LPC_SC->PLL1CON = 0x03; /* PLL1 Enable & Connect */
LPC_SC->PLL1FEED = 0xAA;
LPC_SC->PLL1FEED = 0x55;
while (!(LPC_SC->PLL1STAT & ((1<< 9) | (1<< 8))));/* Wait for PLLC1_STAT & PLLE1_STAT */
#else
LPC_SC->USBCLKCFG = USBCLKCFG_Val; /* Setup USB Clock Divider */
#endif
LPC_SC->PCLKSEL0 = PCLKSEL0_Val; /* Peripheral Clock Selection CCLK/4 CPU时钟的1/4 */
LPC_SC->PCLKSEL1 = PCLKSEL1_Val;
LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals 0x042887DE=1110 1111 1110 1111 1111 0111 1101 1110 */
LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
#endif
#if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
LPC_SC->FLASHCFG = FLASHCFG_Val;
#endif
}
时钟配置结果:
FOSC=12000000=12M
FCCO=2*FOSC*100/6=400M
FCCLK=FCCO/4=100M
对应PCLKSEL0_Val相应位的数据 00-》1/4分频 外设波特率位25M
01-》表示不分频 则外设 (如串口)的频率为100M
串口初始化:
void UART0_Init (void)
{
uint16_t usFdiv;
/* UART0 */
LPC_PINCON->PINSEL0 |= (1 << 4); /* Pin P0.2 used as TXD0 (Com0) */
LPC_PINCON->PINSEL0 |= (1 << 6); /* Pin P0.3 used as RXD0 (Com0) */
LPC_UART0->LCR = 0x83; /* 允许设置波特率 */
/*
usFdiv = (FPCLK / 16) / UART0_BPS; // 设置波特率
LPC_UART0->DLM = usFdiv / 256;
LPC_UART0->DLL = usFdiv % 256;
LPC_UART0->LCR = 0x03;
LPC_UART0->FCR = 0x06;
*/
若频率为25MHZ 波特率115200
LPC_UART0->FDR = (2<<4) | 1;
LPC_UART0->DLM = 0;
LPC_UART0->DLL = 9;
LPC_UART0->LCR = 0x03; // 锁定波特率
LPC_UART0->FCR = 0xc7;
/*
LPC_UART0->FDR = (14<<4) | 9;
LPC_UART0->DLM = 0;
LPC_UART0->DLL = 33;
LPC_UART0->LCR = 0x03; // 锁定波特率
LPC_UART0->FCR = 0xc7;
*/
/*
NVIC_SetPriority(UART0_IRQn,1);//UART0_INT_PRIORITY);
NVIC_EnableIRQ(UART0_IRQn);
LPC_UART0->IER = 0x03;
*/
}
参数配置规律如下:
确定外设频率FPCLK (通常为25M 或100M)
FDR寄存器配置如下:Bits 3:0 为DIVADDVAL 7:4为MULVAL (>=1);
UARTnbaudrate=PCLK/( 16*(256*UnDLM+UnDLL)*( 1+(DivAddVal/MulVal) ) )
1. 1<=MULVAL<=15
2. 0<=DIVADDVAL<=14
3. DIVADDVAL<MULVAL
例子如下:
PCLK=12MHZ,BR=115200
由公式得知DLest=PCLK/(16*BR)=6.51 不为整数
则用FRest=1.5来确定DLest的值DLest=int(PCLK/(16*BR*1.5))=4;
FRest=PCLK/(16*BR*DLest)=1.625 (1.1<FRest<1.9)
1.628与表格table中1.625相邻 取1.625的数据
实际串口率为115384 与115200有0.16%的误差 但是不影响串口的正常工作
LPC1768串口时钟配置全解析