まず、システムタイマ
SysTickは、システム・クロック・チック、システムタイマーと呼ばれる、に属する周辺のCortex-M4カーネル、それはカウンターのデクリメントダウンの24bit
第二に、システムタイマ割り込みの使用状況
1.初期化コード
//は、システムタイマ、トリガ千割り込み1Sカーネル、その白の1msのタイミング初期化 SysTick_Config(SystemCoreClock / 1000)。
2.書き込み割り込みサービス・機能
空隙 SysTick_Handler(ボイド) { 静的 CNT =のuint32_t 0 ; CNT ++ ; // の到着タイミング500msの IF(CNT> = 500 ) { CNT = 0 ; PFout(9 = ^)1。; } }
3.タイマーの時間を計算し
SysTick_Config(SystemCoreClock / 频率)。
システムタイマトリガしてみましょう1秒の休憩をすることができます場合は?ない場合は、通常の時間の最大のものは、その後何ですか?
あなたはトリガすることができない1秒の中断を。
定格周波数で、最大タイミング時間= 2 ^ 168分の24 000 000≈ 99.86ms
(オーバークロック周波数216MHzにおいて)、最大タイミング時間= 2 ^ 24 /216000000≈ 77.67ms
テスト結果:
//は、システムタイマ、1Sカーネル割込みトリガ1000年、はっきりタイミングは1ms、できる正常に初期化 // SysTick_Config(SystemCoreClock / 1000); //は、システムタイマ、1Sカーネルの割り込みトリガ10倍、はっきり時限100ミリ秒、現象を初期化します失敗 SysTick_Config(SystemCoreClock / 10 ); //は、システムタイマ、1Sカーネル割込みトリガ11回、はっきり時限90.90ms、正常に初期化することができます SysTick_Config(SystemCoreClock / 11)。
第三に、システムタイマが使用
二つの側面:
なしオペレーティングシステム:遅延のみのために
有操作系统(ucos2 ucos3 freertos....):为操作系统提供精准的定时中断(1ms~50ms)
四、使用系统定时器用于延时的用途
If you want to use the SysTick timer in polling mode, you can use the count flag in the SysTick Control and
Status Register (SysTick->CTRL) to determine when the timer reaches zero.
For example, you can create a timed delay by setting the SysTick timer to a certain value and waiting until it reaches zero:
SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = 0xFF; // Count from 255 to 0 (256 cycles) SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 5; // Enable SysTick timer with processor clock while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick
1. 配置系统定时器的时钟源
/** * @brief Configures the SysTick clock source. * @param SysTick_CLKSource: specifies the SysTick clock source. * This parameter can be one of the following values: * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source. * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source. * @retval None */ void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource) { /* Check the parameters */ assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource)); if (SysTick_CLKSource == SysTick_CLKSource_HCLK) { SysTick->CTRL |= SysTick_CLKSource_HCLK; } else { SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8; } }
2,系统定时器寄存器
1. 当SysTick使用168MHz系统时钟频率时,代码编写如下:
void delay_us(uint32_t nus) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/1000000)*nus; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 5; // Enable SysTick timer with processor clock while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } void delay_ms(uint32_t nms) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/1000)*nms; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 5; // Enable SysTick timer with processor clock while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } 最大的延时为99.86ms
2. 当SysTick使用168MHz系统时钟频率并进行8分频时,代码编写如下:
void delay_us(uint32_t nus) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/8/1000000)*nus; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 1; // Enable SysTick timer with processor clock while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } void delay_ms(uint32_t nms) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/8/1000)*nms; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 1; // Enable SysTick timer with processor clock while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } 思考题,当前最大的延时时间是多少?如何优化代码,支持秒级别或更长时间的延时? 最大的延时时间 = 2^24 / 21000000 ≈ 798.91ms void delay_ms(uint32_t nms) { uint32_t m,n; m = nms/500; n = nms %500; //m个500ms的延时 while(m--) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/8/1000)*500; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 1; // Enable SysTick timer with processor clock,当使用21MHz的时候,1;当使用168MHz的时候,5; while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } //不足500ms的延时 if(n) { SysTick->CTRL = 0; // Disable SysTick SysTick->LOAD = (SystemCoreClock/8/1000)*n; // 计数值 SysTick->VAL = 0; // Clear current value as well as count flag SysTick->CTRL = 1; // Enable SysTick timer with processor clock,当使用21MHz的时候,1;当使用168MHz的时候,5; while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set SysTick->CTRL = 0; // Disable SysTick } }