回答

收藏

【赚周年币】技术帖Week2-Day4——LPC824 Lite之11、SysTick延时

#线上活动 #线上活动 3322 人阅读 | 0 人回复 | 2017-01-11

本帖最后由 toofree 于 2017-1-19 03:27 编辑

        【赚周年币】技术帖Week2-Day4——LPC824 Lite之11、SysTick延时。

        之前测试过软件精确延时,还有两种硬件延时方式,一个是SysTick延时,另一种是通用定时器延时。今天就来测试一下SysTick延时。

       使用YL-LPC824光盘资料.rar中程序模板,工程“YL-LPC824光盘资料\SDK\peri_example\_示例模板\_empty_example\project__empty_example.uvprojx”,是个样例工程模板。工程很简单,用到了SysTick定时器。
  1. #include <stdio.h>
  2. #include "board.h"


  3. /*****************************************************************************
  4. * Private types/enumerations/variables
  5. ****************************************************************************/

  6. #define TICKRATE_HZ (10)        /* 10 ticks per second */

  7. /*****************************************************************************
  8. * Public types/enumerations/variables
  9. ****************************************************************************/

  10. /*****************************************************************************
  11. * Private functions
  12. ****************************************************************************/

  13. /*****************************************************************************
  14. * Public functions
  15. ****************************************************************************/

  16. /**
  17. * @brief        Handle interrupt from SysTick timer
  18. * @return        Nothing
  19. */
  20. void SysTick_Handler(void)
  21. {
  22.         Board_LED_Toggle(0);
  23. }

  24. /**
  25. * @brief        main routine for template example
  26. * @return        Function should not exit.
  27. */
  28. int main(void)
  29. {
  30.         // SystemCoreClockUpdate会更新全局变量的值,因此只能在进入main()后再调用
  31.         SystemCoreClockUpdate();
  32.         Board_Init();
  33.         
  34.         Board_LED_Set(0, false);
  35.         /* Enable SysTick Timer */
  36.         SysTick_Config(SystemCoreClock / TICKRATE_HZ);

  37.         /* Loop forever */
  38.         while (1) {
  39.                 __WFI();
  40.         }
  41. }
复制代码
关于SysTick只有3个,一个宏定义,一个初始化,一个中断函数,也仅仅几行代码。
        TICKRATE_HZ是几,就是几Hz频率
        

      
       系统时钟除以频率Hz数,即为一个定时周期的计数值。
        


       SysTick中断响应函数
        


       我们要定时1ms,即1000Hz,那么TICKRATE_HZ宏定义为1000。
       做个Delay()函数,开启延时的时候,初始化变量TimingDelay,一直等变量为0。
       在SysTick_Handler()函数中让TimingDelay自减。
       这样是不是就可以实现了基准为1ms的延时了呢。根据以上思路,修改改程序为下。
  1. #include <stdio.h>
  2. #include "board.h"


  3. /*****************************************************************************
  4. * Private types/enumerations/variables
  5. ****************************************************************************/

  6. //#define TICKRATE_HZ (10)        /* 10 ticks per second */
  7. #define TICKRATE_HZ (1000)        /* 1000 ticks per second */
  8. static __IO uint32_t TimingDelay;

  9. void Delay(__IO uint32_t nTime);
  10. void SysTick_Handler(void);

  11. /*****************************************************************************
  12. * Public types/enumerations/variables
  13. ****************************************************************************/

  14. /*****************************************************************************
  15. * Private functions
  16. ****************************************************************************/

  17. /*****************************************************************************
  18. * Public functions
  19. ****************************************************************************/

  20. /**
  21. * @brief        Handle interrupt from SysTick timer
  22. * @return        Nothing
  23. */
  24. void SysTick_Handler(void)
  25. {
  26.         Board_LED_Toggle(0);
  27.         if (TimingDelay > 0)
  28.                 TimingDelay--;
  29. }


  30. /**
  31. * @brief        main routine for template example
  32. * @return        Function should not exit.
  33. */
  34. int main(void)
  35. {
  36.         // SystemCoreClockUpdate会更新全局变量的值,因此只能在进入main()后再调用
  37.         SystemCoreClockUpdate();
  38.         Board_Init();
  39.         
  40.         Board_LED_Set(0, false);
  41.         Board_LED_Set(1, false);
  42.         
  43.         TimingDelay = 0;
  44.         /* Enable SysTick Timer */
  45.         SysTick_Config(SystemCoreClock / TICKRATE_HZ);

  46.         /* Loop forever */
  47.         while (1) {
  48.                 //__WFI();
  49.                 Delay(500);
  50.                 Board_LED_Toggle(1);
  51.         }
  52. }

  53. /**
  54.   * @brief  Inserts a delay time.
  55.   * @param  nTime: specifies the delay time length, in milliseconds.
  56.   * @retval None
  57.   */
  58. void Delay(__IO uint32_t nTime)
  59. {
  60.   TimingDelay = nTime;
  61.    
  62.   while(TimingDelay != 0)
  63.   {
  64.   }        
  65. }
复制代码
除了原本开的LED_0(对应PCB实物丝印为LED1)之外 ,又开了LED_1(对应实物上LED2)。
       全速跑程序,可看到LED_0较暗,LED_1以1Hz的频率在闪烁。
       so easy! 今天偷个懒


关注下面的标签,发现更多相似文章

评分

参与人数 1 +30 收起 理由
EEboard爱板网 + 30 3周发帖养成记 奖励

查看全部评分

分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条