STM32 v3.5固件库systick ms延时

http://yaoohui.blog.163.com/blog/static/173794970201231110648251/

《如何从STM32F10xxx固件库V2.0.3升级为STM32F10xxx标准外设库V3.0.0》一文中的“3.3.2 SysTick”讲到:

在标准外设库中移除了SysTick的驱动,因此用户必须调用CMSIS定义的函数。
CMSIS只提供了一个SysTick设置的函数,替代了STM32原有SysTick驱动的全部函数。
SysTick_Config(uint32_t ticks);
该函数设置了自动重载入计数器(LOAD)的值,SysTick IRQ的优先级,复位了计数器(VAL)的值,开始计数并打开SysTick IRQ中断。SysTick时钟默认使用系统时钟。
下面的例程为使用固件库V2.0.3进行SysTick设置:
/* Select the HCLK Clock as SysTick clock source (72MHz) */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
/* SysTick end of count event each 1ms with input clock equal to 72MHz (HCLK) */
SysTick_SetReload(72000);
/* Enable SysTick interrupt */
SysTick_ITConfig(ENABLE);
下面的例程为使用标准外设库V3.0.0进行SysTick设置:
/* Setup SysTick Timer for 1 msec interrupts */
if (SysTick_Config(SystemFrequency / 1000)) /* SystemFrequency is defined in “system_stm32f10x.h” and equal to HCLK frequency */
{
  /* Capture error */
  while (1);
}

2.0库函数延时代码
 
     

/*初始化时钟*/

void Init_SysTick(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); }

/*延时1ms函数*/

__IO uint32_t TimingDelay;

void delay_ms(__IO uint32_t nTime) { /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); TimingDelay = nTime; while(TimingDelay != 0); /* Disable SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Disable); /* Clear SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Clear); }

/*stm32f10x_it.c中的void SysTick_Handler(void)函数改为*/ extern __IO uint32_t TimingDelay;

void SysTick_Handler(void) { if (TimingDelay != 0x00) { TimingDelay--; } }

/*用法,延时1秒*/ delay_ms(1000);


3.5库函数延时代码
 
     

/*初始化时钟*/

void Init_SysTick(void) { if(SysTick_Config(SystemCoreClock / 1000)) //注意:3.5库中 SystemFrequency 被 SystemCoreClock 取代。 while(1); }

/*延时1ms函数*/

__IO uint32_t TimingDelay;

void delay_ms(__IO uint32_t nTime) { TimingDelay = nTime; while(TimingDelay != 0); }

/*stm32f10x_it.c中的void SysTick_Handler(void)函数改为*/

extern __IO uint32_t TimingDelay;

void SysTick_Handler(void) { if (TimingDelay != 0x00) { TimingDelay--; } }

/*用法,延时1秒*/

delay_ms(1000);


你可能感兴趣的:(stm32)