• systick运用


    systick的原理前一篇博文有介绍,简而言之就是
    SysTick定时器是一个24位的倒计数,当倒计数为0时,将从RELOAD寄存器中取值作为定时器的初始值,同时可以选择在这个时候产生中断(异常号:15)。
    例如从RELOAD的值为999,那么当倒计数为0时,就会从复位为999继续倒计数。
    库文件当中有systick的专用库函数的,这里暂时不用到。
    在keil工程当中,新建systick.c,systick.h文件,

    #ifndef __SYSTICK_H
    #define __SYSTICK_H

    #include "stm32f10x.h"

    void RCC_Configuration(void);
    void Delay_SYSTICK(__IO uint32_t nTime);

    #endif /* __SYSTICK_H */


    #include "SYSTICK.h"

    static __IO uint32_t TimingDelay;

    /****************************************************************************
    * 名 称:void TimingDelay_Decrement(void)
    * 功 能:获取节拍程序
    * 入口参数:无
    * 出口参数:无
    * 说 明:此函数在stm32f10x_it.c文件调用
    * 调用方法:无
    ****************************************************************************/
    void TimingDelay_Decrement(void)
    {
    if (TimingDelay != 0x00)
    {
    TimingDelay--;
    }
    }


    /****************************************************************************
    * 名 称:void Delay(__IO uint32_t nTime)
    * 功 能:定时延时程序 1ms为单位
    * 入口参数:无
    * 出口参数:无
    * 说 明:
    * 调用方法:无
    ****************************************************************************/
    void Delay_SYSTICK(__IO uint32_t nTime)
    {
    TimingDelay = nTime;
    while(TimingDelay != 0);
    }

    ***注意在CC++的目录中添加相应的头文件目录。

    在stm32f10x_it.c文件中添加如下声明:extern void TimingDelay_Decrement(void);
    同时更新如下函数为:
    void SysTick_Handler(void)
    {
    TimingDelay_Decrement();
    }


    在main函数中,
    添加 //First to config the system clock
    RCC_Configuration(); //系统时钟设置及各外设时钟使能

    if (SysTick_Config(72000)) //时钟节拍中断时1ms一次 用于定时
    {
    /* Capture error */
    while (1);
    }
    让clock运行起来,同时,开启systick定时器,其时钟可以是HCLK的8分频或是HCLK,SysTick_Config函数默认是选用HCLK时钟的,72000/72000000 = 1/1000 = 1ms。
    之后就可以调用Delay_SYSTICK(500)来延时500ms。而且不占用cpu,比for(;;)循环高效多了。

  • 相关阅读:
    新建SVN仓库并上传项目
    如何查看某个端口被谁占用
    Sql Server系列:索引基础
    Sql Server系列:索引设计原则及优化
    Sql Server系列:键和约束
    Sql Server系列:Select基本语句
    Sql Server系列:Delete语句
    Sql Server系列:Update语句
    Sql Server系列:Insert语句
    Sql Server系列:数据控制语句
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/9738738.html
Copyright © 2020-2023  润新知