什么是SYSTICK: 這是一個(gè)24位的系統(tǒng)節(jié)拍定時(shí)器system tick timer,SysTick,具有自動重載和溢出中斷功能,,所有基于Cortex_M3處理器的微控制器都可以由這個(gè)定時(shí)器獲得一定的時(shí)間間隔。 作用: 在單任務(wù)引用程序中,,因?yàn)槠浼軜?gòu)就決定了它執(zhí)行任務(wù)的串行性,,這就引出一個(gè)問題:當(dāng)某個(gè)任務(wù)出現(xiàn)問題時(shí),就會牽連到后續(xù)的任務(wù),,進(jìn)而導(dǎo)致整個(gè)系統(tǒng)崩潰,。要解決這個(gè)問題,可以使用實(shí)時(shí)操作系統(tǒng)(RTOS). 因?yàn)镽TOS以并行的架構(gòu)處理任務(wù),,單一任務(wù)的崩潰并不會牽連到整個(gè)系統(tǒng),。這樣用戶出于可靠性的考慮可能就會基于RTOS來設(shè)計(jì)自己的應(yīng)用程序。這樣SYSTICK存在的意義就是提供必要的時(shí)鐘節(jié)拍,,為RTOS的任務(wù)調(diào)度提供一個(gè)有節(jié)奏的“心跳”,。 微控制器的定時(shí)器資源一般比較豐富,比如STM32存在8個(gè)定時(shí)器,,為啥還要再提供一個(gè)SYSTICK,?原因就是所有基于ARM Cortex_M3內(nèi)核的控制器都帶有SysTick定時(shí)器,這樣就方便了程序在不同的器件之間的移植,。而使用RTOS的第一項(xiàng)工作往往就是將其移植到開發(fā)人員的硬件平臺上,,由于SYSTICK的存在無疑降低了移植的難度。 SysTick定時(shí)器除了能服務(wù)于操作系統(tǒng)之外,,還能用于其它目的:如作為一個(gè)鬧鈴,,用于測量時(shí)間等。 要注意的是,,當(dāng)處理器在調(diào)試期間被喊停(halt)時(shí),,則SysTick定時(shí)器亦將暫停運(yùn)作。 時(shí)鐘的選擇: 用戶可以在位于Cortex_M3處理器系統(tǒng)控制單元中的系統(tǒng)節(jié)拍定時(shí)器控制和狀態(tài)寄存器(SysTick control and status register ,SCSR)選擇systick 時(shí)鐘源,。如將SCSR中的CLKSOURCE位置位,,SysTick會在CPU頻率下運(yùn)行;而將CLKSOUCE位清除則SysTick會以CPU主頻的1/8頻率運(yùn)行,。 3.5版本的庫函數(shù)與以往的有所區(qū)別 不存在stm32f10x_systick.c文件,,故原來的一些函數(shù)也不存在,比如SysTick_SetReload(u32 reload);SysTick_ITConfig(FunctionalState NewState);等 在3.5版本的庫函數(shù)中與systick相關(guān)的函數(shù)只有兩個(gè) 第一個(gè),,SysTick_Config(uint32_t ticks),,在core_cm3.h頭文件中進(jìn)行定義的。 第二個(gè),,void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource),,在misc.c文件中定義的。 SysTick_Config(uint32_t ticks),,在core_cm3.h 主要的作用: 1,、初始化systick 2,、打開systick 3、打開systick的中斷并設(shè)置優(yōu)先級 4,、返回一個(gè)0代表成功或1代表失敗 注意: Uint32_t ticks 即為重裝值,, 這個(gè)函數(shù)默認(rèn)使用的時(shí)鐘源是AHB,即不分頻,。 要想分頻,,調(diào)用void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource), 但是要注意函數(shù)調(diào)用的次序,,先SysTick_Config(uint32_t ticks),, 后SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource) 函數(shù)說明: /** * @brief Initialize and start the SysTick counter and its interrupt. * * @param ticks number of ticks between two interrupts * @return 1 = failed, 0 = successful * * Initialise the system tick timer and its interrupt and start the * system tick timer / counter in free running mode to generate * periodical interrupts. */ static __INLINE uint32_t SysTick_Config(uint32_t ticks) { if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */重裝載值必須小于0XFF FFFF,為什么,這是一個(gè)24位的遞減計(jì)數(shù)器,。 SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */設(shè)置重裝載值,,SysTick_LOAD_RELOAD_Msk定義見后面 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 */ } #endif 與systick相關(guān)的寄存器定義 /** @addtogroup CMSIS_CM3_SysTick CMSIS CM3 SysTick memory mapped structure for SysTick @{ */ typedef struct { __IO uint32_t CTRL; /*!< Offset: 0x00 SysTick Control and Status Register */ __IO uint32_t LOAD; /*!< Offset: 0x04 SysTick Reload Value Register */ __IO uint32_t VAL; /*!< Offset: 0x08 SysTick Current Value Register */ __I uint32_t CALIB; /*!< Offset: 0x0C SysTick Calibration Register */ } SysTick_Type; 與systick寄存器相關(guān)的寄存器及位的定義 /* SysTick Control / Status Register Definitions */控制/狀態(tài)寄存器 #define SysTick_CTRL_COUNTFLAG_Pos 16 /*!< SysTick CTRL: COUNTFLAG Position */#define SysTick_CTRL_COUNTFLAG_Msk (1ul << SysTick_CTRL_COUNTFLAG_Pos) /*!< SysTick CTRL: COUNTFLAG Mask */ 溢出標(biāo)志位 #define SysTick_CTRL_CLKSOURCE_Pos 2 /*!< SysTick CTRL: CLKSOURCE Position */ #define SysTick_CTRL_CLKSOURCE_Msk (1ul << SysTick_CTRL_CLKSOURCE_Pos) /*!< SysTick CTRL: CLKSOURCE Mask */時(shí)鐘源選擇位,0=外部時(shí)鐘,;1=內(nèi)核時(shí)鐘 #define SysTick_CTRL_TICKINT_Pos 1 /*!< SysTick CTRL: TICKINT Position */ #define SysTick_CTRL_TICKINT_Msk (1ul << SysTick_CTRL_TICKINT_Pos) /*!< SysTick CTRL: TICKINT Mask */異常請求位 #define SysTick_CTRL_ENABLE_Pos 0 /*!< SysTick CTRL: ENABLE Position */ #define SysTick_CTRL_ENABLE_Msk (1ul << SysTick_CTRL_ENABLE_Pos) /*!< SysTick CTRL: ENABLE Mask */使能位 /* SysTick Reload Register Definitions */ #define SysTick_LOAD_RELOAD_Pos 0 /*!< SysTick LOAD: RELOAD Position */ #define SysTick_LOAD_RELOAD_Msk (0xFFFFFFul << SysTick_LOAD_RELOAD_Pos) /*!< SysTick LOAD: RELOAD Mask */ /* SysTick Current Register Definitions */ #define SysTick_VAL_CURRENT_Pos 0 /*!< SysTick VAL: CURRENT Position */ #define SysTick_VAL_CURRENT_Msk (0xFFFFFFul << SysTick_VAL_CURRENT_Pos) /*!< SysTick VAL: CURRENT Mask */ /* SysTick Calibration Register Definitions */ #define SysTick_CALIB_NOREF_Pos 31 /*!< SysTick CALIB: NOREF Position */ #define SysTick_CALIB_NOREF_Msk (1ul << SysTick_CALIB_NOREF_Pos) /*!< SysTick CALIB: NOREF Mask */ #define SysTick_CALIB_SKEW_Pos 30 /*!< SysTick CALIB: SKEW Position */ #define SysTick_CALIB_SKEW_Msk (1ul << SysTick_CALIB_SKEW_Pos) /*!< SysTick CALIB: SKEW Mask */ #define SysTick_CALIB_TENMS_Pos 0 /*!< SysTick CALIB: TENMS Position */ #define SysTick_CALIB_TENMS_Msk (0xFFFFFFul << SysTick_VAL_CURRENT_Pos) /*!< SysTick CALIB: TENMS Mask */ /*@}*/ /* end of group CMSIS_CM3_SysTick */ 與systick相關(guān)的寄存器的說明 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource) 作用: 選擇systick的時(shí)鐘源,,AHB時(shí)鐘或AHB的8分頻 默認(rèn)使用的是AHB時(shí)鐘,即72MHz 函數(shù)說明: /** * @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; } } Systick時(shí)鐘源的定義: /** @defgroup SysTick_clock_source * @{ */ #define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB)//將控制狀態(tài)寄存器的第二位置0,,即用外部時(shí)鐘源 #define SysTick_CLKSource_HCLK ((uint32_t)0x00000004)//將控制狀態(tài)寄存器的第二位置1,,即用內(nèi)核時(shí)鐘 #define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \ ((SOURCE) == SysTick_CLKSource_HCLK_Div8)) Systick定時(shí)時(shí)間的設(shè)定: 重裝載值=systick 時(shí)鐘頻率(Hz)X想要的定時(shí)時(shí)間(S) 如:時(shí)鐘頻率為:AHB的8分頻;AHB=72MHz那么systick的時(shí)鐘頻率為72/8MHz=9MHz;要定時(shí)1秒,,則 重裝載值=9000000X1=9000000,; 定時(shí)10毫秒 重狀態(tài)值=9000000X0.01=90000 Systick的中斷處理函數(shù), 在startup_stm32f10x_hd.s啟動文件中有定義,。 DCD SysTick_Handler ; SysTick Handler 根據(jù)需要直接編寫中斷處理函數(shù)即可: Void SysTick_Handler (void) { ;} 注意: 如果在工程中,,加入了stm32f10x_it.c,而又在主函數(shù)中編寫中斷函數(shù),則會報(bào)錯(cuò),。 因?yàn)樵趕tm32f10x_it.c文件中,,也有這個(gè)中斷函數(shù)的聲明,只是內(nèi)容是空的,。 /** * @brief This function handles SysTick Handler. * @param None * @retval None */ void SysTick_Handler(void) { } 中斷優(yōu)先級的修改 在調(diào)用SysTick_Config(uint32_t ticks)之后,,調(diào)用 void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)。這個(gè)函數(shù)在core_cm3.h頭文件中,。 具體內(nèi)容如下: /** * @brief Set the priority for an interrupt * * @param IRQn The number of the interrupt for set priority * @param priority The priority to set * * Set the priority for the specified interrupt. The interrupt * number can be positive to specify an external (device specific) * interrupt, or negative to specify an internal (core) interrupt. * * Note: The priority cannot be set for every core interrupt. */ static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) { if(IRQn < 0) { SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */ else { NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for device specific Interrupts */ } 下面以一個(gè)實(shí)例來說明: 利用systick來實(shí)現(xiàn)以1秒的時(shí)間間隔,,閃亮一個(gè)LED指示燈,指示燈接在GPIOA.8,,低電平點(diǎn)亮,。 #include "stm32f10x.h" //函數(shù)聲明 void GPIO_Configuration(void);//設(shè)置GPIOA.8端口 u32 t;//定義一個(gè)全局變量 int main(void) { // SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); SysTick_Config(9000000); SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); GPIO_Configuration(); while(1); } //GPIOA.8設(shè)置函數(shù) void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStruct;//定義一個(gè)端口初始化結(jié)構(gòu)體 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//打開GPIOA口時(shí)鐘 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//設(shè)置為推挽輸出 GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;//設(shè)置輸出頻率50M GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;//指定第8腳 GPIO_Init(GPIOA,&GPIO_InitStruct);//初始化GPIOA.8 GPIO_SetBits( GPIOA, GPIO_Pin_8);//置高GPIOA.8,關(guān)閉LED } //systick中斷函數(shù) void SysTick_Handler(void) { t++; if(t>=1) { if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)==1) {GPIO_ResetBits( GPIOA, GPIO_Pin_8);} } if(t>=2) { if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8)==0) {GPIO_SetBits( GPIOA, GPIO_Pin_8);} t=0; } } 模擬后的結(jié)果 1、8分頻后結(jié)果 2,、直接調(diào)用SysTick_Config(9000000);即不分頻的結(jié)果,間隔為1/8=0.125s 總結(jié): 1,、要使用systick定時(shí)器,,只需調(diào)用SysTick_Config(uint32_t ticks)函數(shù)即可, 自動完成了,,重裝載值的裝載,,時(shí)鐘源選擇,計(jì)數(shù)寄存器復(fù)位,,中斷優(yōu)先級的設(shè)置(最低),,開中斷,開始計(jì)數(shù)的工作,。 2,、要修改時(shí)鐘源調(diào)用SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)。 3,、要修改中斷優(yōu)先級調(diào)用 void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) 應(yīng)用說明: 1,、因systick是一個(gè)24位的定時(shí)器,故重裝值最大值為2的24次方=16 777 215,, 要注意不要超出這個(gè)值,。 2、systick是cortex_m3的標(biāo)配,,不是外設(shè),。故不需要在RCC寄存器組打開他的時(shí)鐘。 3,、每次systick溢出后會置位計(jì)數(shù)標(biāo)志位和中斷標(biāo)志位,,計(jì)數(shù)標(biāo)志位在計(jì)數(shù)器重裝載后被清除,而中斷標(biāo)志位也會隨著中斷服務(wù)程序的響應(yīng)被清除,,所以這兩個(gè)標(biāo)志位都不需要手動清除,。 4、采用使用庫函數(shù)的方法,,只能采用中斷的方法響應(yīng)定時(shí)器計(jì)時(shí)時(shí)間到,,如要采用查詢的方法,那只能采用設(shè)置systick的寄存器的方法,,具體操作以后再做分析,。 |
|