STM32 v3.5固件库systick ms延时

《如何从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库函数延时代码
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span style="line-height: 22px;"><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">/*初始化时钟*/</span></span>
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Init_SysTick</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Disable SysTick Counter */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CounterCmd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Counter_Disable</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Disable the SysTick Interrupt */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_ITConfig</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">DISABLE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Configure HCLK clock as SysTick clock source */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CLKSourceConfig</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CLKSource_HCLK_Div8</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_SetReload</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">9000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Enable the SysTick Interrupt */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_ITConfig</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">ENABLE</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><span style="line-height: 22px;"><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">/*延时1ms函数*/</span></span>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">__IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> delay_ms</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">__IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> nTime</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Enable the SysTick Counter */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CounterCmd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Counter_Enable</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nTime</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Disable SysTick Counter */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CounterCmd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Counter_Disable</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="com" style="color: rgb(136, 0, 0);">/* Clear SysTick Counter */</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_CounterCmd</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Counter_Clear</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*stm32f10x_it.c中的void SysTick_Handler(void)函数改为*/</span>
<span class="kwd" style="color: rgb(0, 0, 136);">extern</span><span class="pln" style="color: rgb(0, 0, 0);"> __IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Handler</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0x00</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">--;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*用法,延时1秒*/</span>
<span class="pln" style="color: rgb(0, 0, 0);">delay_ms</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"></p>

3.5库函数延时代码
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*初始化时钟*/</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Init_SysTick</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Config</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">SystemCoreClock</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">/</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">1000</span><span class="pun" style="color: rgb(102, 102, 0);">))</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="com" style="color: rgb(136, 0, 0);">//</span><span style="line-height: 22px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">注意:3.5库中 </span></span><span style="line-height: 22px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">SystemFrequency 被 </span></span><span style="line-height: 22px; font-family: Arial, Helvetica, sans-serif; white-space: normal;"><span class="com" style="line-height: 25px; color: rgb(136, 0, 0);">SystemCoreClock 取代。</span></span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*延时1ms函数*/</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">__IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> delay_ms</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="pln" style="color: rgb(0, 0, 0);">__IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> nTime</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">=</span><span class="pln" style="color: rgb(0, 0, 0);"> nTime</span><span class="pun" style="color: rgb(102, 102, 0);">;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">while</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0</span><span class="pun" style="color: rgb(102, 102, 0);">);</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*stm32f10x_it.c中的void SysTick_Handler(void)函数改为*/</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">extern</span><span class="pln" style="color: rgb(0, 0, 0);"> __IO </span><span class="typ" style="color: rgb(102, 0, 102);">uint32_t</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">;</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">SysTick_Handler</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="kwd" style="color: rgb(0, 0, 136);">void</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pun" style="color: rgb(102, 102, 0);">{</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="kwd" style="color: rgb(0, 0, 136);">if</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="pun" style="color: rgb(102, 102, 0);">!=</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="lit" style="color: rgb(0, 102, 102);">0x00</span><span class="pun" style="color: rgb(102, 102, 0);">)</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">{</span><span class="pln" style="color: rgb(0, 0, 0);"> </span>
<span class="pln" style="color: rgb(0, 0, 0);">    </span><span class="typ" style="color: rgb(102, 0, 102);">TimingDelay</span><span class="pun" style="color: rgb(102, 102, 0);">--;</span>
<span class="pln" style="color: rgb(0, 0, 0);">  </span><span class="pun" style="color: rgb(102, 102, 0);">}</span>
<span class="pun" style="color: rgb(102, 102, 0);">}</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;">
</p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="com" style="color: rgb(136, 0, 0);">/*用法,延时1秒*/</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pln" style="color: rgb(0, 0, 0);">delay_ms</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="lit" style="color: rgb(0, 102, 102);">1000</span><span class="pun" style="color: rgb(102, 102, 0);">);</span></p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值