目的
滴答定时器整理(STM32F103)。用了一段时间的滴答定时器,突然忘记其中配置方法,重新翻阅手册及博文,在此记录备忘。
SysTick的重装寄存器决定了定时器频率,若SysTick的时钟源是72M, SystemFrequency = 72000000Hz
//1ms时基
//HCLK_Frequency = 72000000Hz;
//#define OS_TICKS_PER_SEC 1000
SysTick_SetReload((u32)RCC_Clocks.HCLK_Frequency / OS_TICKS_PER_SEC);
时钟树
寄存器
寄存器名称和地址
STK_CSR, 0xE000E010 – 控制寄存器
STK_LOAD, 0xE000E014 – 重载寄存器
STK_VAL, 0xE000E018 – 当前值寄存器
STK_CALRB, 0xE000E01C – 校准值寄存器
寄存器描述
STK_CSR控制寄存器:寄存器低4位含义
第0位:ENABLE,Systick 使能位 (0:关闭Systick功能;1:开启Systick功能)
第1位:TICKINT,Systick 中断使能位(0:关闭Systick中断;1:开启Systick中断)
第2位:CLKSOURCE,Systick时钟源选择 (0:使用HCLK/8 作为Systick时钟;1:使用HCLK作为Systick时钟)
第3位:COUNTFLAG,Systick计数比较标志,如果在上次读取本寄存器后,SysTick 已经数到了0,则该位为1。如果读取该位,该位将自动清零
STK_LOAD 重载寄存器:
Systick是一个递减的定时器,当定时器递减至0时,重载寄存器中的值就会被重装载,继续开始递减。STK_LOAD 重载寄存器是个24位的寄存器最大计数0xFFFFFF。
STK_VAL当前值寄存器:
也是个24位的寄存器,读取时返回当前倒计数的值,写它则使之清零,同时还会清除在SysTick 控制及状态寄存器中的COUNTFLAG 标志。
STK_CALRB校准值寄存器:
位31 NOREF :1=没有外部参考时钟(STCLK 不可用)0=外部参考时钟可用
位30 SKEW:1=校准值不是准确的1ms 0=校准值是准确的1ms
位[23:0] :Calibration value Indicates the calibration value when the SysTick counter runs on HCLK max/8 as external clock. The value is product dependent, please refer to the Product Reference Manual, SysTick Calibration Value section. When HCLK is programmed at the maximum frequency, the SysTick period is 1ms. If calibration information is not known, calculate the calibration value required from the frequency of the processor clock or external clock.
代码块
SysTick配置代码
/*
* SystemFrequency / 1000 1ms中断一次
* SystemFrequency / 100000 10us中断一次
* SystemFrequency / 1000000 1us中断一次
*/
void SysTick_Configuration(void)
{
#if defined(__STM32F10x_LIB_H)
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_SetReload((u32)RCC_Clocks.HCLK_Frequency / OS_TICKS_PER_SEC);//#define OS_TICKS_PER_SEC 1000u
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_ITConfig(ENABLE);
#elif defined(__STM32F4xx_H)
RCC_ClocksTypeDef RCC_Clocks;
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / OS_TICKS_PER_SEC);
#else
#error SysTick_Configuration
#endif
}
SysTickHandler
void SysTickHandler(void)// 1ms 时间基准
{
OS_CPU_SR cpu_sr;
g_tickCount++;
OS_ENTER_CRITICAL();
OSIntNesting++;
OS_EXIT_CRITICAL();
OSTimeTick();
OSIntExit();
}
SysTick_Config
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk) 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 */
}
上一篇:arm看门狗的作用
下一篇:STM32是如何进入中断函数的
推荐阅读最新更新时间:2024-03-16 16:18