关于 System Tick设置,给出3种方法,学习并确认OK:
使用 M051BSPv3.01.001版本
一、使用函数CLK_EnableSysTick()
1 //Enable System Tick counter, Select HXT/2 as the clock source of SysTick,reload value. It could be 0~0xFFFFFF. 2 //HXT=12mHZ=(1/12)us
3 //12000*(1/12)=1000us=1ms 即1ms中断一次 4 CLK_EnableSysTick(CLK_CLKSEL0_STCLK_S_HXT, 12000);
函数CLK_EnableSysTick()在CLK.C
注意:此函数有保护寄存器操作,所在需在解保时执行。
1 /** 2 * @brief Enable System Tick counter 3 * @param[in] u32ClkSrc is System Tick clock source. Including: 4 * - \ref CLK_CLKSEL0_STCLK_S_HXT 5 * - \ref CLK_CLKSEL0_STCLK_S_HXT_DIV2 6 * - \ref CLK_CLKSEL0_STCLK_S_HCLK_DIV2 7 * - \ref CLK_CLKSEL0_STCLK_S_HIRC_DIV2 8 * - \ref CLK_CLKSEL0_STCLK_S_HCLK 9 * @param[in] u32Count is System Tick reload value. It could be 0~0xFFFFFF. 10 * @return None 11 * @details This function set System Tick clock source, reload value, enable System Tick counter and interrupt. 12 * The register write-protection function should be disabled before using this function. 13 */ 14 void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count) 15 { 16 /* Set System Tick counter disabled */ 17 SysTick->CTRL = 0; 18 19 /* Set System Tick clock source */ 20 if( u32ClkSrc == CLK_CLKSEL0_STCLK_S_HCLK ) 21 SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; 22 else 23 CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLK_S_Msk) | u32ClkSrc; 24 25 /* Set System Tick reload value */ 26 SysTick->LOAD = u32Count; 27 28 /* Clear System Tick current value and counter flag */ 29 SysTick->VAL = 0; 30 31 /* Set System Tick interrupt enabled and counter enabled */ 32 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; 33 }
二、使用函数SysTick_Config()
1 if (SysTick_Config(SysTick_int_us*50))//此函数SysTick时钟源默认为系统时钟源。 2 { 3 //Capture error 4 do 5 printf("SysTick_Init_失败 \n\n\n"); 6 while (1); 7 } 8 else 9 printf("SysTick_Init_成功 \n");
此函数在core_cm0.h
1 /** \brief System Tick Configuration 2 3 The function initializes the System Timer and its interrupt, and starts the System Tick Timer. 4 Counter is in free running mode to generate periodic interrupts. 5 6 \param [in] ticks Number of ticks between two interrupts. 7 8 \return 0 Function succeeded. 9 \return 1 Function failed. 10 11 \note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the 12 function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b> 13 must contain a vendor-specific implementation of this function. 14 15 */ 16 __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks) 17 { 18 if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */ 19 20 SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */ 21 NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Systick Interrupt */ 22 SysTick->VAL = 0; /* Load the SysTick Counter Value */ 23 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | 24 SysTick_CTRL_TICKINT_Msk | 25 SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ 26 return (0); /* Function successful */ 27 }
三、直接寄存器操作
1 //主时钟为:50Mhz 2 //HXT=50mHZ=1/50us 50000*1/50=1000us=1ms//OK 3 //重装载值=ticks 时钟频率(Hz)X想要的定时时间(S)=50x1000=50000 4 //SysTick控制与状态(SYST_CSR) 5 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | // 1= 内核时钟用于SysTick. 6 SysTick_CTRL_TICKINT_Msk | // 1:向下计数到0将引起SysTick中断。清SysTick当前值寄存器SYST_CVR的值将清除SysTick中断. 7 SysTick_CTRL_ENABLE_Msk; // ENABLE:1:计数器运行于多脉冲方式. 0:禁用计数器 8 // COUNTFLAG:计数由1到0时,COUNTFLAG 置位.在读该位或向系统定时器当前值寄存器(SYST_CVR)写时,COUNTFLAG 被清零。 9 //SysTick重装载值寄存器(SYST_RVR) 10 SysTick->LOAD = ((SysTick_int_us*50) & SysTick_LOAD_RELOAD_Msk) - 1; //RELOAD:当计数器达到0时,值加载到当前值寄存器. 11 12 //SysTick当前值寄存器(SYST_CVR) 13 SysTick->VAL = 0; //CURRENT:该寄存器为写清零软件写入任何值将清寄存器为0。 14 15 // set Priority for Systick Interrupt 16 //NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); 17 NVIC_EnableIRQ(SysTick_IRQn);
最后中断入口程序:
1 /******************************************************************* 2 功能 :SysTick中断入口 3 说明 :嘀嗒心跳 4 入口参数 :无 5 返回值 :无 6 应用子程序 : 7 全局变量 : 8 *******************************************************************/ 9 void SysTick_Handler(void) //非SysTick_IRQHandler(void) 10 { 11 SysTick_CountValue_Add(); //计数加1 12 /*SysTick当前值寄存器(SYST_CVR)*/ 13 SysTick->VAL = 0; //CURRENT:该寄存器为写清零,软件写入任何值将清寄存器为0,写值清中断 14 GPIO_TOGGLE(P00); //Test 1ms P00切换一次电平 15 }