微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32 v3.5固件库systick ms延时

STM32 v3.5固件库systick ms延时

时间:12-02 来源:互联网 点击:
《如何从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);

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top