系统时钟的重要性不要我多说,呵呵,在操作系统中特别重要,夜深了,我也不多说了,要睡觉了,呵呵,以后有空跑个ucos再发文章了,最近比较忙,呵呵
#include "stm32f10x_lib.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Config(void);
void Delay_Ms(u32);
void TimingDelay_Decrement(void);
static vu32 TimingDelay;
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration();//配置系统时钟
NVIC_Configuration(); //配置 NVIC 和 Vector Table
GPIO_Configuration(); //配置使用的GPIO口
SysTick_Config();
//主循环
while (1)
{
// 每隔1S钟就点亮一次LED
Delay_Ms(500);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_Ms(500);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
}
//SysTick设置
void SysTick_Config(void)
{
/* Disable SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Disable);
/* Disable the SysTick Interrupt */
SysTick_ITConfig(DISABLE);
/* Configure HCLK clock as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
/* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
SysTick_SetReload(9000);
/* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
}
void Delay_Ms(u32 nTime)
{
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
TimingDelay = nTime;
while(TimingDelay);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay) TimingDelay--;
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();//将外设 RCC寄存器重设为缺省值
RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振(HSE)
HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待 HSE 起振
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //预取指缓存使能
FLASH_SetLatency(FLASH_Latency_2);//设置代码延时值 FLASH_Latency_2 2 延时周期
RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置 AHB 时钟(HCLK)RCC_SYSCLK_Div1 AHB 时钟 = 系统时钟
RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速 AHB 时钟(PCLK2)RCC_HCLK_Div2 APB1 时钟 = HCLK / 2
RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速 AHB 时钟(PCLK1)RCC_HCLK_Div2 APB1 时钟 = HCLK / 2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz 设置 PLL 时钟源及倍频系数
RCC_PLLCmd(ENABLE);//使能或者失能 PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待指定的 RCC 标志位设置成功 等待PLL初始化成功
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟(SYSCLK) 设置PLL为系统时钟源
while(RCC_GetSYSCLKSource() != 0x08) //等待PLL成功用作于系统时钟的时钟源 0x00:HSI 作为系统时钟 0x04:HSE作为系统时钟 0x08:PLL作为系统时钟
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能或者失能 APB2 外设时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
void delay()
{
int i;
for (i=0; i<0xffff; i++)
;
}
#ifdef DEBUG
void assert_failed(u8* file, u32 line)
{
while (1)
{
}
}
#endif