作者:小僧李
转自:http://blog.csdn.net/liqinghan/article/details/8451494
1.首先要配置好系统的时钟如下:
RCC_DeInit();//将外设RCC寄存器重设为缺省值
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);//设置外部高速晶振(HSE )
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待HSE 起振,等待外部的时钟起振
if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能或者失能预取指缓存
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);//设置代码延时值
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置AHB时钟(HCLK)
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速AHB时钟(PCLK2) to APB2
/* PCLK1 = HCLK/2 */
//stm32的时钟比较复杂
RCC_PCLK1Config(RCC_HCLK_Div2); //设置高速AHB时钟(PCLK1)to APB1
/* ADCCLK = PCLK2/8 */
RCC_ADCCLKConfig(RCC_PCLK2_Div8); //设置ADC时钟(ADCCLK)
/* PLLCLK = 8MHz * 9 = 72 MHz */
//设置外部时钟源,且PLL等于外部时钟频率*9
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//此时PLLCLK =72MHz,这个得知道他的值,因为USB的时钟USBCLK = PLLCLK/1或者PLLCLK/1.5,但得确保USBCLK =48MHz
//所以配置USBCLK的时候选择是1.5分频
RCC_PLLCmd(ENABLE);//最后才激活PLL,必须最后才激活!
/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//配置SYSCLK
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
else
{
/* If HSE fails to start-up, the application will have wrong clock configuration.
User can add here some code to deal with this error */
/* Go to infinite loop */
while (1)
{
}
}
void USBclock_Config()
{
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);//1.5分频
/* Enable the USB clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);//此处是激活USB外设,USB属于APB1外设
}