系统节拍的使用

   我们在使用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

原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/4712397.html