STM32f103R8实验:SysTick实验中的问题
最近在看《STM32自学笔记》,仿照书上的例子写了个用SysTick查询方式定时,控制LED灯闪烁的实验程序,源代码如下:
#include "stm32f10x_lib.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void SysTick_Configuration(void);
void Delay_Second(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
SysTick_Configuration();
while(1)
{
GPIO_SetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
Delay_Second();
GPIO_ResetBits(GPIOB, GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
Delay_Second();
}
}
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
if(RCC_WaitForHSEStartUp() == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);//16M晶振,16/2*9 = 72M
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
}
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_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void SysTick_Configuration(void)
{
SysTick_CounterCmd(SysTick_Counter_Disable);
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//SysTick使用HCLK/8 = 9.0MHz的时钟
SysTick_CounterCmd(SysTick_Counter_Clear);
SysTick_SetReload(9000 * 1000);// 9.0MHz的时钟定时1秒钟
}
void Delay_Second(void)
{
SysTick_CounterCmd(SysTick_Counter_Enable);
while(SysTick_GetFlagStatus(SysTick_FLAG_COUNT) == 0);
SysTick_CounterCmd(SysTick_Counter_Disable);
SysTick_CounterCmd(SysTick_Counter_Clear);
}
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
// On ST-ICE the PLL output clock is fixed to 72 MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
// Enable PLL
RCC_PLLCmd(ENABLE);
// Wait till PLL is ready
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}
// Select PLL as system clock source
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
// Wait till PLL is used as system clock source
while (RCC_GetSYSCLKSource() != 0x08)
{}
}
我的晶体是8M的,具体的设置你依据自己系统参数去定。