• 系统节拍的使用


       我们在使用SysTick时,首先要确定时钟周期;然后才能使用它。

        SysTick的最大计数值为:2^24(2的24次方)

        超过此值,系统自动置0。

        在配置的时候,如果超过此值;不启动SysTick。

    #define SYSTICK_MAXCOUNT       ((1<<24) -1)                                    /* SysTick MaxCount                                                      */

    #if (__Vendor_SysTickConfig == 0)

    /** rief  System Tick Configuration

        This function initialises the system tick timer and its interrupt and start the system tick timer.
        Counter is in free running mode to generate periodical interrupts.

        param [in]  ticks  Number of ticks between two interrupts
        eturn          0  Function succeeded
        eturn          1  Function failed
    */
    static __INLINE uint32_t SysTick_Config(uint32_t ticks)
    {
      //if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */
      if (ticks > SYSTICK_MAXCOUNT)  return (1);            /* Reload value impossible */
      SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
      NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */
      SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
      SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                       SysTick_CTRL_TICKINT_Msk   |
                       SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
      return (0);                                                  /* Function successful */
    }

    #endif

    /*@} end of CMSIS_Core_SysTickFunctions */

    注意,在CMSIS V2.02是没有“SYSTICK_MAXCOUNT”宏定义的。

    我们在使用这个函数,之前需要作2件事情:

    1. 初始化系统时钟

    SystemCoreClockUpdate();

    2. 确定50us的时钟值

    #define PCLK            (SystemCoreClock / 4)
    #define PCLK_BENCHMARK  (PCLK / 1000)
    #define PCLK_50US       (PCLK_BENCHMARK / 5)

    主程序:

    /****************************************Copyright (c)****************************************************
    **                                 TDTC Tech Dev
    **                                     HRB
    **--------------File Info---------------------------------------------------------------------------------
    ** File name:           main.c
    ** Last modified Date: 
    ** Last Version:       
    ** Descriptions:       
    **
    **--------------------------------------------------------------------------------------------------------
    ** Created by:          TDTC
    ** Created date:        2015-07-09
    ** Version:             V0.01
    ** Descriptions:        UART Send
    **
    **--------------------------------------------------------------------------------------------------------      
    *********************************************************************************************************/
    #include "LPC17xx.h"
    //#include "uart.h"

    #define    LED          1UL << 2                                 /* P2.2       */
    #define    LED_INIT()   LPC_GPIO2->FIODIR |= LED                 /* LED INIT   */
    #define    LED_OFF()    LPC_GPIO2->FIOSET |= LED                 /* LED OFF    */
    #define    LED_ON()     LPC_GPIO2->FIOCLR |= LED                 /* LED ON     */

    #define PCLK            (SystemCoreClock / 4)
    #define PCLK_BENCHMARK  (PCLK / 1000)
    #define PCLK_50US       (PCLK_BENCHMARK / 5)

    void SysTick_Handler (void)
    {
        static uint32_t count=0;
            count++;
            if(count%2) {
                LED_ON();
            } else {
                LED_OFF();
            } 
    }

    int main(void)
    {
        SystemCoreClockUpdate();
        //UARTInit(0, 19200);
        LED_INIT();
        LED_OFF();
        SysTick_Config( PCLK_50US * 20 ); // 1ms


        while (1) {
            ;
        }
    }

    我们在系统节拍中断(SysTick_Handler)中进行闪灯, 以此激活波形。

    在Saleae逻辑分析仪中的波形图

    wave_systick

  • 相关阅读:
    bzoj 2969: 矩形粉刷 概率期望+快速幂
    loj #6191. 「美团 CodeM 复赛」配对游戏 期望dp
    CF446C DZY Loves Fibonacci Numbers 线段树 + 数学
    CF696B Puzzles 概率期望
    bzoj 3566: [SHOI2014]概率充电器 数学期望+换根dp
    loj #6342. 跳一跳 期望dp
    CF316G3 Good Substrings 广义后缀自动机
    bzoj 3829: [Poi2014]FarmCraft 树形dp+贪心
    bzoj 2131: 免费的馅饼
    CF19D Points 平衡树
  • 原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/4712397.html
Copyright © 2020-2023  润新知