/** @addtogroup LPC17xx_System_Defines LPC17xx System Defines
@{
*/
#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
00499 void SystemInit (void)
00500 {
00501 #if (CLOCK_SETUP) /* Clock Setup */
00502 LPC_SC->SCS = SCS_Val;
00503 if (LPC_SC->SCS & (1 << 5)) { /* If Main Oscillator is enabled */
00504 while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
00505 }
00506
00507 LPC_SC->CCLKCFG = CCLKCFG_Val; /* Setup Clock Divider */
00508 /* Periphral clock must be selected before PLL0 enabling and connecting
00509 * - according errata.lpc1768-16.March.2010 -
00510 */
00511 LPC_SC->PCLKSEL0 = PCLKSEL0_Val; /* Peripheral Clock Selection */
00512 LPC_SC->PCLKSEL1 = PCLKSEL1_Val;
00513
00514 #if (PLL0_SETUP)
00515 LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for PLL0 */
00516
00517 LPC_SC->PLL0CFG = PLL0CFG_Val; /* configure PLL0 */
00518 LPC_SC->PLL0FEED = 0xAA;
00519 LPC_SC->PLL0FEED = 0x55;
00520
00521 LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */
00522 LPC_SC->PLL0FEED = 0xAA;
00523 LPC_SC->PLL0FEED = 0x55;
00524 while (!(LPC_SC->PLL0STAT & (1<<26)));/* Wait for PLOCK0 */
00525
00526 LPC_SC->PLL0CON = 0x03; /* PLL0 Enable & Connect */
00527 LPC_SC->PLL0FEED = 0xAA;
00528 LPC_SC->PLL0FEED = 0x55;
00529 while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24))));/* Wait for PLLC0_STAT & PLLE0_STAT */
00530 #endif
00531
00532 #if (PLL1_SETUP)
00533 LPC_SC->PLL1CFG = PLL1CFG_Val;
00534 LPC_SC->PLL1FEED = 0xAA;
00535 LPC_SC->PLL1FEED = 0x55;
00536
00537 LPC_SC->PLL1CON = 0x01; /* PLL1 Enable */
00538 LPC_SC->PLL1FEED = 0xAA;
00539 LPC_SC->PLL1FEED = 0x55;
00540 while (!(LPC_SC->PLL1STAT & (1<<10)));/* Wait for PLOCK1 */
00541
00542 LPC_SC->PLL1CON = 0x03; /* PLL1 Enable & Connect */
00543 LPC_SC->PLL1FEED = 0xAA;
00544 LPC_SC->PLL1FEED = 0x55;
00545 while (!(LPC_SC->PLL1STAT & ((1<< 9) | (1<< 8))));/* Wait for PLLC1_STAT & PLLE1_STAT */
00546 #else
00547 LPC_SC->USBCLKCFG = USBCLKCFG_Val; /* Setup USB Clock Divider */
00548 #endif
00549 LPC_SC->PCONP = PCONP_Val; /* Power Control for Peripherals */
00550
00551 LPC_SC->CLKOUTCFG = CLKOUTCFG_Val; /* Clock Output Configuration */
00552 #endif
00553
00554 #if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
00555 LPC_SC->FLASHCFG = FLASHCFG_Val;
00556 #endif
00557
00558 // Set Vector table offset value
00559 #if (__RAM_MODE__==1)
00560 SCB->VTOR = 0x10000000 & 0x3FFFFF80;
00561 #else
00562 SCB->VTOR = 0x00000000 & 0x3FFFFF80;
00563 #endif
00564 }
LPC_SC->SCS = SCS_Val; (1)
if (LPC_SC->SCS & (1 << 5)) { /* If Main Oscillator is enabled */ (2)
while ((LPC_SC->SCS & (1<<6)) == 0);/* Wait for Oscillator to be ready */
}
LPC_SC->CCLKCFG = CCLKCFG_Val; /* Setup Clock Divider */ (3)
/* Periphral clock must be selected before PLL0 enabling and connecting
* - according errata.lpc1768-16.March.2010 -
*/
LPC_SC->PCLKSEL0 = PCLKSEL0_Val; /* Peripheral Clock Selection */ (4)
LPC_SC->PCLKSEL1 = PCLKSEL1_Val; (5)
LPC_SC->CLKSRCSEL = CLKSRCSEL_Val; /* Select Clock Source for PLL0 */ (1)
LPC_SC->PLL0CFG = PLL0CFG_Val; /* configure PLL0 */ (2)
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
LPC_SC->PLL0CON = 0x01; /* PLL0 Enable */ (3)
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & (1<<26)));/* Wait for PLOCK0 */ (4)
LPC_SC->PLL0CON = 0x03; /* PLL0 Enable & Connect */ (5)
LPC_SC->PLL0FEED = 0xAA;
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & ((1<<25) | (1<<24)))); /* Wait for PLLC0_STAT & PLLE0_STAT */
PLL0CFG_Val = 0x00050063,即M=100,注意,M的实际值为寄存器的值+1,即99+1,同理N=6,(5)就是使能PLL0了。每次对PLL0的寄存器进行配置时需要依次向PLL0FEED写入0xAA、0x55以使配置生效。OK,最后我们来计算验证下PLL0的输出频率是多少?根据公式:Fpllclk = (Fpllclkin*2*M)/N,若Main OSC为12M,则Fpllclk = 12*2*100/6=400M。最后Fcclk = Fpllclk / 4 = 100M,就是系统时钟频率,那么外设时钟源频率为25M。
#if (FLASH_SETUP == 1) /* Flash Accelerator Setup */
LPC_SC->FLASHCFG = FLASHCFG_Val;
#endif
FLASHCFG_VAL = 0x0000303A,为默认值。FLASH加速模块在后面再介绍了。
#if (__RAM_MODE__==1)
SCB->VTOR = 0x10000000 & 0x3FFFFF80;
#else
SCB->VTOR = 0x00000000 & 0x3FFFFF80;
#endif