stm32 systick使用

一开始在stm32参考手册里找关于systick的内容,没找到任何有用的信息,后来百度了下,发现是在cortex m3参考手册里有

几个寄存器的描述在第8.2节,寄存器的定义在core_cm3.h中而不是stm32f10.h,应该这个systick是属于cm3内核的内容,所以才放在这个文件中定义。

先来看看是如何定义的:

/* Memory mapping of Cortex-M3 Hardware */

#define SCS_BASE            (0xE000E000)                              /*!< System Control Space Base Address */

#define ITM_BASE            (0xE0000000)                              /*!< ITM Base Address                  */

#define CoreDebug_BASE      (0xE000EDF0)                              /*!< Core Debug Base Address           */

#define SysTick_BASE        (SCS_BASE +  0x0010)                      /*!< SysTick Base Address              */

#define NVIC_BASE           (SCS_BASE +  0x0100)                      /*!< NVIC Base Address                 */

#define SCB_BASE            (SCS_BASE +  0x0D00)                      /*!< System Control Block Base Address */

 

#define InterruptType       ((InterruptType_Type *) SCS_BASE)         /*!< Interrupt Type Register           */

#define SCB                 ((SCB_Type *)           SCB_BASE)         /*!< SCB configuration struct          */

#define SysTick             ((SysTick_Type *)       SysTick_BASE)     /*!< SysTick configuration struct      */

#define NVIC                ((NVIC_Type *)          NVIC_BASE)        /*!< NVIC configuration struct         */

#define ITM                 ((ITM_Type *)           ITM_BASE)         /*!< ITM configuration struct          */

#define CoreDebug           ((CoreDebug_Type *)     CoreDebug_BASE)   /*!< Core Debug configuration struct   */

 

#if defined (__MPU_PRESENT) && (__MPU_PRESENT == 1)

  #define MPU_BASE          (SCS_BASE +  0x0D90)                      /*!< Memory Protection Unit            */

  #define MPU               ((MPU_Type*)            MPU_BASE)         /*!< Memory Protection Unit            */

#endif

可以看到,中间有systick的定义,类型为结构体指针,该结构体定义如下

typedef struct

{

  __IO uint32_t CTRL;                         /*!< Offset: 0x00  SysTick Control and Status Register */

  __IO uint32_t LOAD;                         /*!< Offset: 0x04  SysTick Reload Value Register       */

  __IO uint32_t VAL;                          /*!< Offset: 0x08  SysTick Current Value Register      */

  __I  uint32_t CALIB;                        /*!< Offset: 0x0C  SysTick Calibration Register        */

} SysTick_Type;

可以看到,systick相关的四个寄存器都定义了,后面还很方便的把各个为的功能都用宏定义指明了,如下

/* SysTick Control / Status Register Definitions */

#define SysTick_CTRL_COUNTFLAG_Pos         16                                             /*!< SysTick CTRL: COUNTFLAG Position */

#define SysTick_CTRL_COUNTFLAG_Msk         (1ul << SysTick_CTRL_COUNTFLAG_Pos)            /*!< SysTick CTRL: COUNTFLAG Mask */

 

#define SysTick_CTRL_CLKSOURCE_Pos          2                                             /*!< SysTick CTRL: CLKSOURCE Position */

#define SysTick_CTRL_CLKSOURCE_Msk         (1ul << SysTick_CTRL_CLKSOURCE_Pos)            /*!< SysTick CTRL: CLKSOURCE Mask */

 

#define SysTick_CTRL_TICKINT_Pos            1                                             /*!< SysTick CTRL: TICKINT Position */

#define SysTick_CTRL_TICKINT_Msk           (1ul << SysTick_CTRL_TICKINT_Pos)              /*!< SysTick CTRL: TICKINT Mask */

 

#define SysTick_CTRL_ENABLE_Pos             0                                             /*!< SysTick CTRL: ENABLE Position */

#define SysTick_CTRL_ENABLE_Msk            (1ul << SysTick_CTRL_ENABLE_Pos)               /*!< SysTick CTRL: ENABLE Mask */

 

/* SysTick Reload Register Definitions */

#define SysTick_LOAD_RELOAD_Pos             0                                             /*!< SysTick LOAD: RELOAD Position */

#define SysTick_LOAD_RELOAD_Msk            (0xFFFFFFul << SysTick_LOAD_RELOAD_Pos)        /*!< SysTick LOAD: RELOAD Mask */

 

/* SysTick Current Register Definitions */

#define SysTick_VAL_CURRENT_Pos             0                                             /*!< SysTick VAL: CURRENT Position */

#define SysTick_VAL_CURRENT_Msk            (0xFFFFFFul << SysTick_VAL_CURRENT_Pos)        /*!< SysTick VAL: CURRENT Mask */

 

/* SysTick Calibration Register Definitions */

#define SysTick_CALIB_NOREF_Pos            31                                             /*!< SysTick CALIB: NOREF Position */

#define SysTick_CALIB_NOREF_Msk            (1ul << SysTick_CALIB_NOREF_Pos)               /*!< SysTick CALIB: NOREF Mask */

 

#define SysTick_CALIB_SKEW_Pos             30                                             /*!< SysTick CALIB: SKEW Position */

#define SysTick_CALIB_SKEW_Msk             (1ul << SysTick_CALIB_SKEW_Pos)                /*!< SysTick CALIB: SKEW Mask */

 

#define SysTick_CALIB_TENMS_Pos             0                                             /*!< SysTick CALIB: TENMS Position */

#define SysTick_CALIB_TENMS_Msk            (0xFFFFFFul << SysTick_VAL_CURRENT_Pos)        /*!< SysTick CALIB: TENMS Mask */

/*@}*/ /* end of group CMSIS_CM3_SysTick */

pos指明偏移位数,1ulul表明1unsigned long型数据,32

接下来看看各个位的具体描述,先是控制寄存器

stm32 systick使用_第1张图片

 

重装载寄存器里面就放重装载值

目前值寄存器就是保存目前值,最后一个校准没用到。

以下为秒钟的测试

main部分代码----------------

 

    SysTick->CTRL&=~SysTick_CTRL_CLKSOURCE_Msk;//clksource清零

    SysTick->CTRL|=SysTick_CTRL_CLKSOURCE_Msk;//clksource置1

   

    SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;//开中断

   

    SysTick->LOAD=72000;//设置重装载值 1ms

    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;//启动systick

      while(1)

    {        

        if(cnt==1000)

        {

            sec++;

            cnt=0;            

            LCD_ShowNum(30,30,sec,10,12);

        }

    }

中断部分,在stm32f10x_it.c

void SysTick_Handler(void)

{

    extern u32 cnt;

    cnt++;

}

 

刚刚发现,在core_cm3.h中已经有个函数用于设置systick,为SysTick_Config

 

你可能感兴趣的:(stm32 systick使用)