以下是一个用SysTick用作延时的程序,实现功能是使LED以1S时间闪烁,但调试不成功,灯一直是亮的,不知错在哪,请高手指点,谢谢了!!!
/****************************************************
Description: 将SysTick定时器用来延时,使LED_1以1S
间隔闪烁
Date: 05.23.2011
Question:
*****************************************************/
/**************include*******************************/
#include "stm32f10x_lib.h"
/***********bian liang ding yi***********/
ErrorStatus HSEStartUpStatus;
GPIO_InitTypeDef GPIO_InitStructure;
extern vu32 TimingDelay;
/**********han shu sheng ming************/
void RCC_Config(void);
void GPIO_Config(void);
void SysTick_Init(void);
void Init(void);
void Delay_Ms(u32 Ms);
/****************main********************/
int main(void)
{
Init();
while(1)
{
GPIO_SetBits(GPIOC , GPIO_Pin_7);
Delay_Ms(1);
GPIO_ResetBits(GPIOC , GPIO_Pin_7);
Delay_Ms(10000);
}
}
/**********************yan shi**************************************/
void Delay_Ms(u32 Ms)
{
SysTick_CounterCmd(SysTick_Counter_Enable); //使能滴答计数
TimingDelay = Ms ;
while(TimingDelay != 0);
SysTick_CounterCmd(SysTick_Counter_Disable); //失能滴答计数
SysTick_CounterCmd(SysTick_Counter_Clear); //清除滴答计数
}
/***************init**********************/
void Init()
{
RCC_Config();
SysTick_Init();
GPIO_Config();
}
/***************Systick init***************/
void SysTick_Init()
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //设置SysTick时钟为HCLK/8=9MHz.
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick , 3 , 0); //设置系统优先级
SysTick_SetReload(72000); //当计数器计数到0时,经历时间为1/(9MHZ/9000)=1ms.
SysTick_ITConfig(ENABLE); //使能中断
}
/***************RCC config*****************/
void RCC_Config()
{
RCC_DeInit(); //RCC reset
RCC_HSEConfig(RCC_HSE_ON); //HSE she zhi
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //config HSE
if (SUCCESS == HSEStartUpStatus) //wait for qi zheng
{
FLASH_SetLatency(FLASH_Latency_2); //set FLASH
RCC_PLLConfig(RCC_PLLSource_HSE_Div1 , RCC_PLLMul_9); //set PLL
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //set SYSCLK source
RCC_HCLKConfig(RCC_SYSCLK_Div1); //set HCLK = SYSCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //set PCLK1 = HCLK/2
RCC_PCLK2Config(RCC_HCLK_Div1); //set PCLK2 = HCLK
RCC_PLLCmd(ENABLE); //shi neng PLL
}
}
/****************GPIO config**************/
void GPIO_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
/********GPIOC Init***********************************/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //C jiao tuiwan shuchu
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOC , &GPIO_InitStructure);
}
中断函数的内容是
vu32 TimingDelay = 0;
void SysTickHandler(void)
{
TimingDelay--;
} |