标签:嵌入式
LPC1768的时钟源可以来自三个:
1)内部RC振荡器
内部振荡器可看作看门狗定时器的时钟源,也可作PLL0和cpu的时钟源,但是无法作为usb的时钟源,因为精度达不到。而且如果CAN波特率高于100kb/s,则也不适用了。在系统上电时,LPC1768都使用内部振荡器,直到软件将其切换为另一种可用的时钟源。
2)主振荡器
主振荡器可作为CPU的时钟源,需要通过分频和倍频进行配置使用。基本会使用主振荡器作为时钟源
3)RTC振荡器
RTC振荡器可提供1Hz-32kHz的RTC时钟输出,可用作PLL0、CPU和看门狗定时的时钟源。
LPC1768时钟配置会涉及到以下几个过程:
1)时钟源选择
时钟源的选择就涉及到了SCS寄存器的配置
2)时钟分频
涉及到CCLKCFG寄存器的配置
3)PLL0配置
涉及到CLKSRCSEL、PLL0CFG、PLL0FEED等寄存器的配置
4)PLL1配置
涉及到PLL1CFG、PLL1FEED等寄存器的配置
5)外设时钟输出
具体的代码如下:
#define CLOCK_SETUP 1
#define SCS_Val 0x00000020
#define CLKSRCSEL_Val 0x00000001
#define PLL0_SETUP 1
#define PLL0CFG_Val 0x00050063
#define PLL1_SETUP 1
#define PLL1CFG_Val 0x00000023
#define CCLKCFG_Val 0x00000003
#define USBCLKCFG_Val 0x00000000
#define PCLKSEL0_Val 0x00000000
#define PCLKSEL1_Val 0x00000000
#define PCONP_Val 0x042887DE
#define CLKOUTCFG_Val 0x00000000
#define FLASH_SETUP 1
#define FLASHCFG_Val 0x0000303A
//时钟配置
void SystemInit()
{
#if(CLOCK_SETUP) //时钟源设置
LPC_SC->SCS = SCS_Val;
if(SCS_Val &(1 << 5)){
while((LPC_SC->SCS & (1 << 6)) == 0); //主振荡器已稳定
}
LPC_SC->CCLKCFG = CCLKCFG_Val; //setup clock divider,CCLKCFG_Val = 3, 为4分频,即CCLK为PLL0的四分之一
#if (PLL0_SETUP)
LPC_SC->CLKSRCSEL = CLKSRCSEL_Val;//选择主振荡器作为PLL0时钟源
LPC_SC->PLL0CFG = PLL0CFG_Val; //在此选择PLL0倍频器值和预分频器值,分别是0~14位和16~23位
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55; //以上两行代码表示使PLL0CON和PLL0CFG寄存器的更改生效
LPC_SC->PLL0CON = 0x01; //PLL0使能
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & (1<<26))); // Wait for PLOCK0
LPC_SC->PLL0CON = 0x03; /* PLL0 Enable & Connect */
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while(!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));
#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 */
LPC_SC->PCLKSEL1 = PCLKSEL1_Val;
LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals */
LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
#endif
#if (FLASH_SETUP == 1)
LPC_SC->FLASHCFG = FLASHCFG_Val; //Flash加速器配置
#endif
}
这样就配置完成,并且配置的100MHz的时钟频率。
Fcco = 12MHz/4 * 2 * 100 / 6 = 100MHz
以上是来自网上云飞翔的博客,地址:
http://www.cnblogs.com/flyingcloude/p/3745023.html
但是在system_lpc17xx.c里面,系统初始化函数还包括下面
/* 根据时钟寄存器的值确定时钟频率 */
if (((LPC_SC->PLL0STAT >> 24)&3)==3) {/* If PLL0 enabled and connected */
switch (LPC_SC->CLKSRCSEL & 0x03) {
case 0: /* Internal RC oscillator => PLL0 */
case 3: /* Reserved, default to Internal RC */
SystemFrequency = (IRC_OSC *
((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) /
(((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1) /
((LPC_SC->CCLKCFG & 0xFF)+ 1));
break;
case 1: /* Main oscillator => PLL0 */
SystemFrequency = (OSC_CLK *
((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) /
(((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1) /
((LPC_SC->CCLKCFG & 0xFF)+ 1));
break;
case 2: /* RTC oscillator => PLL0 */
SystemFrequency = (RTC_CLK *
((2 * ((LPC_SC->PLL0STAT & 0x7FFF) + 1))) /
(((LPC_SC->PLL0STAT >> 16) & 0xFF) + 1) /
((LPC_SC->CCLKCFG & 0xFF)+ 1));
break;
}
} else {
switch (LPC_SC->CLKSRCSEL & 0x03) {
case 0: /* Internal RC oscillator => PLL0 */
case 3: /* Reserved, default to Internal RC */
SystemFrequency = IRC_OSC / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
break;
case 1: /* Main oscillator => PLL0 */
SystemFrequency = OSC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
break;
case 2: /* RTC oscillator => PLL0 */
SystemFrequency = RTC_CLK / ((LPC_SC->CCLKCFG & 0xFF)+ 1);
break;
}
}
#if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
LPC_SC->FLASHCFG = (LPC_SC->FLASHCFG & ~0x0000F000) | FLASHCFG_Val;
#endif
}
一般情况下,LPC_SC->CLKSRCSEL=0x01,即跳转到case 1, SystemFrequency =100M,
在PCLKSEL0和PCLKSEL1寄存器中队各个外设的值都设为0,表明PCLK外设时钟=CCLK/4。
即 PCLK=25Mhz.
标签:嵌入式
原文地址:http://blog.csdn.net/sunflowerfsw/article/details/45825369