LPC1788系统时钟初始化
扫描二维码
随时随地手机看文章
#ifndef __SYS_H_
#define __SYS_H_
#include "common.h"
#define SystemCoreClock 120000000 //cpu时钟频率,计算时有用
#define ApbClock 120000000 //120M
#define EmcClock 60000000 //60M
#define UsbClock 48000000 //48M
void SystemInit(void);//系统时钟初始化,启动代码调用
#endif
#include "sys.h"
#define XTAL_FREQ 12000000
#define LPC_PBOOST *((volatile unsigned long *)(0X400FC1B0))
void SystemInit(void)
{
LPC_SC->SCS=0x00000021;//osc晶振使能,地址线不移位模式
while((LPC_SC->SCS&(1<<6))==0);/* 等待osc晶振准备好 */
LPC_SC->CLKSRCSEL=0x00000001; /*选择osc为系统输入时钟*/
LPC_PBOOST|=(3<<0); //打开功耗提升,可以提升到120MHZ
// PLL0 Configuration (Main PLL)
// PLL0 Configuration Register (PLL0CFG)
// PLL out clock = (F_cco / (2 * P))
// F_cco = (F_in * M * 2 * P)
// F_in must be in the range of 1 MHz to 25 MHz
// PLL out clock must be in the range of 9.75 MHz to 160 MHz
// F_cco 156-320M
// MSEL: PLL Multiplier Selection 0-4bit m
// M Value
// PSEL: PLL Divider Selection 5-6bit p
// P Value
// 1
// 2
// 4
// 8
LPC_SC->PLL0CFG=0x00000009;//选择倍频系数 P = 1 M = 10 FCCO = FIN*P*M*2=240M PLL_OUT = FCCO/(2*P)=120M
LPC_SC->PLL0CON=0x01; /* PLL0 使能 */
LPC_SC->PLL0FEED=0xAA;
LPC_SC->PLL0FEED=0x55;
while(!(LPC_SC->PLL0STAT&(1<<10)));/* 等待PLL锁定 */
LPC_SC->PLL1CFG=0x00000023;//选择倍频系数 m = 4 p = 2 fcco = fin*2*p*m = 192m pllout = 192/2*p = 48M
LPC_SC->PLL1CON=0x01; /* PLL1 使能 */
LPC_SC->PLL1FEED=0xAA;
LPC_SC->PLL1FEED=0x55;
while(!(LPC_SC->PLL1STAT&(1<<10))); /* 等待PLL1 锁定 */
LPC_SC->CCLKSEL=(1<<0)|(1<<8); /* pll0为主时钟,分频数为1 */
LPC_SC->USBCLKSEL=(1<<0)|(2<<8); /* pll1为cpu主时钟 */
LPC_SC->EMCCLKSEL=(1<<0); /* EMC 为系统是时钟的一半 */
LPC_SC->PCLKSEL=(1<<0); /* 外设时钟分频数为1,120M */
LPC_SC->PCONP=0x00; /* 可以关闭的外设全部被关闭 */
LPC_SC->CLKOUTCFG&=~(1<<8); /* 停止时钟输出 */
LPC_SC->FLASHCFG=(5<<12)|0x03A;//6个cpu flash访问时钟,最安全的设置,120M时候可用
#ifdef __RAM_MODE__//设置中断向量表的位置,一般不改
SCB->VTOR=0x10000000&0x3FFFFF80;
#else
SCB->VTOR=0x00000000&0x3FFFFF80;
#endif
}