新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32 v3.5固件庫systick ms延時

STM32 v3.5固件庫systick ms延時

作者: 時間:2016-12-02 來源:網(wǎng)絡 收藏
《如何從STM32F10xxx固件庫V2.0.3升級為STM32F10xxx標準外設庫V3.0.0》一文中的“3.3.2 SysTick”講到:
在標準外設庫中移除了SysTick的驅動,因此用戶必須調(diào)用CMSIS定義的函數(shù)。
CMSIS只提供了一個SysTick設置的函數(shù),替代了STM32原有SysTick驅動的全部函數(shù)。
SysTick_Config(uint32_t ticks);
該函數(shù)設置了自動重載入計數(shù)器(LOAD)的值,SysTick IRQ的優(yōu)先級,復位了計數(shù)器(VAL)的值,開始計數(shù)并打開SysTick IRQ中斷。SysTick時鐘默認使用系統(tǒng)時鐘。
下面的例程為使用固件庫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庫函數(shù)延時代碼

/*初始化時鐘*/

本文引用地址:http://m.butianyuan.cn/article/201612/325082.htm

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函數(shù)*/

__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)函數(shù)改為*/
extern __IO uint32_t TimingDelay;

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

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

3.5庫函數(shù)延時代碼

/*初始化時鐘*/

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

/*延時1ms函數(shù)*/

__IO uint32_t TimingDelay;

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

/*stm32f10x_it.c中的void SysTick_Handler(void)函數(shù)改為*/

extern __IO uint32_t TimingDelay;

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

/*用法,延時1秒*/

delay_ms(1000);



評論


技術專區(qū)

關閉