×
单片机 > 单片机程序设计 > 详情

STM32初学笔记2之Systick

发布时间:2020-08-27 发布时间:
|
前面花了近一周的时间完成了STM32中关于复位与系统时钟的学习,终于,现在可以开始下一个任务了:SysTick。也不知道接着学这个对不对,但是感觉全都是跟时钟有关的东东,就放一起来学学,应该没什么问题吧。

        好吧,我不得不承认自己水平还不够,在第一个板子(金牛STM32F107VCT6)上跑了一个程序,但是没能成功,继续找原因吧。

        接下来,我还是不死心,在另外一块板子(ALIENTEK STM32F103RBT6)上面写了一个使用SysTick定时的代码,结果运行成功,在这里分享出来,以作以后复习只用。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

               Delay_SysTick.H

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 ** 文件名称:Delay_SysTick.H
 ** 功能描述:系统滴答及其延时的使用声明
 ** 硬件平台:STM32F10x
 ** 编译环境:Keil uversion4 IDE
 ** 库版本  :v3.5.0
 ** 版本信息:v0.0
 ** 编写作者:
 ** 编写时间:2011-11-26
 ** 附加说明:无
 ** 修改记录:无
 **/
#ifndef _DELAY_SYSTICK_H
#define _DELAY_SYSTICK_H
#include "stm32f10x.h"
/* Private function prototypes -----------------------------------------------*/
void SysTick_Configuration(void);
void SysTickDelay(__IO uint32_t nTime);
void TimingDelay_Decrement(void);
void SysTick_Handler(void);
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

               Delay_SysTick.C

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 ** 文件名称:Delay_SysTick.C
 ** 功能描述:系统滴答及其延时的使用
 ** 硬件平台:STM32F10x
 ** 编译环境:Keil uversion4 IDE
 ** 库版本  :v3.5.0
 ** 版本信息:v0.0
 ** 编写作者:
 ** 编写时间:2011-11-26
 ** 附加说明:无
 ** 修改记录:2011-11-26 将TimingDelay数据类型改为volatile
 **/
 /* Includes ------------------------------------------------------------------*/
//#include "main.h"
#include "delay_systick.h"
#include "stm32f10x.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile uint32_t TimingDelay;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/* Setup SysTick Timer for 1 msec interrupts.
 ------------------------------------------
1. The SysTick_Config() function is a CMSIS function which configure:
   - The SysTick Reload register with value passed as function parameter.
   - Configure the SysTick IRQ priority to the lowest value (0x0F).
   - Reset the SysTick Counter register.
   - Configure the SysTick Counter clock source to be Core Clock Source (HCLK).
   - Enable the SysTick Interrupt.
   - Start the SysTick Counter.

2. You can change the SysTick Clock source to be HCLK_Div8 by calling the
   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8) just after the
   SysTick_Config() function call. The SysTick_CLKSourceConfig() is defined
   inside the misc.c file.

3. You can change the SysTick IRQ priority by calling the
   NVIC_SetPriority(SysTick_IRQn,...) just after the SysTick_Config() function 
   call. The NVIC_SetPriority() is defined inside the core_cm3.h file.

4. To adjust the SysTick time base, use the following formula:
                        
     Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)

   - Reload Value is the parameter to be passed for SysTick_Config() function
   - Reload Value should not exceed 0xFFFFFF
*/

/**              
  * @brief  Configure the SysTick clock.
  * @param  None
  * @retval None
  */
void SysTick_Configuration(void)
{
 /*SysTick clock source select*/
 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
 /*Initialize and start systick clock and its interrupt*/
 if (SysTick_Config(SystemCoreClock / 1000))
   { 
     /* Capture error */ 
     while (1);
   }
}


/**              
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, in milliseconds.
  * @retval None
  */
void SysTickDelay(__IO uint32_t nTime)

   TimingDelay = nTime;

   while(TimingDelay != 0);
}

/**
  * @brief  Decrements the TimingDelay variable.
  * @param  None
  * @retval None
  */
void TimingDelay_Decrement(void)
{
   if (TimingDelay != 0x00)
   { 
     TimingDelay--;
   }
}

/**
  * @brief  This function handles SysTick Handler.
  * @param  None
  * @retval None
  */
void SysTick_Handler(void)
{
   TimingDelay_Decrement();
}

说明:本次没有直接对寄存器操作,因为是入门,所以没那么深入,先让板子跑起来再说吧,下面给出的参考资料中有对寄存器的讲解,很有价值,希望能帮上大家。


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈AVR中定时器几种工作模式