STM32 systick 定時 時間計算
阿新 • • 發佈:2019-01-02
系統嘀嗒(SysTick)校準值暫存器
1.(SysTick) 系統嘀嗒時鐘是由HCLK 分頻 出來的。HCLK=SYSCLK=72MHz
/* Select HCLK/8 as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
當系統嘀嗒時鐘設定為9 兆赫
或是:
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
當系統嘀嗒時鐘設定為72 兆赫 2.系統嘀嗒校準值固定到9000,當系統嘀嗒時鐘設定為9 兆赫,產生1ms 時基。
/* SysTick interrupt each 9ms with counter clock equal to 1MHz */
SysTick_SetReload(9000);
該引數取值必須在1和0x00FFFFFF之間 3.
使能一下:
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable); /* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
這還有另外一種設定方法:
經試驗驗證可行: //NVIC_InitTypeDef NVIC_InitStructure; /* Configure HCLK clock as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); /* Configure the SysTick handler priority */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* SysTick interrupt each 100 Hz with HCLK equal to 72MHz 每1ms發生一次 SysTick中斷 */
SysTick_SetReload(72000); /* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE); /* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
關鍵是:SysTick_CLKSourceConfig,和SysTick_SetReload
/* Select HCLK/8 as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
當系統嘀嗒時鐘設定為9 兆赫
或是:
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
當系統嘀嗒時鐘設定為72 兆赫 2.系統嘀嗒校準值固定到9000,當系統嘀嗒時鐘設定為9 兆赫,產生1ms 時基。
/* SysTick interrupt each 9ms with counter clock equal to 1MHz */
SysTick_SetReload(9000);
該引數取值必須在1和0x00FFFFFF之間 3.
使能一下:
/* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable); /* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE);
這還有另外一種設定方法:
經試驗驗證可行: //NVIC_InitTypeDef NVIC_InitStructure; /* Configure HCLK clock as SysTick clock source */
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); /* Configure the SysTick handler priority */
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* SysTick interrupt each 100 Hz with HCLK equal to 72MHz 每1ms發生一次 SysTick中斷 */
SysTick_SetReload(72000); /* Enable the SysTick Interrupt */
SysTick_ITConfig(ENABLE); /* Enable the SysTick Counter */
SysTick_CounterCmd(SysTick_Counter_Enable);
關鍵是:SysTick_CLKSourceConfig,和SysTick_SetReload