新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 v3.5固件庫systick ms延時

        STM32 v3.5固件庫systick ms延時

        作者: 時間:2016-12-02 來源:網絡 收藏
        《如何從STM32F10xxx固件庫V2.0.3升級為STM32F10xxx標準外設庫V3.0.0》一文中的“3.3.2 SysTick”講到:
        在標準外設庫中移除了SysTick的驅動,因此用戶必須調用CMSIS定義的函數。
        CMSIS只提供了一個SysTick設置的函數,替代了STM32原有SysTick驅動的全部函數。
        SysTick_Config(uint32_t ticks);
        該函數設置了自動重載入計數器(LOAD)的值,SysTick IRQ的優先級,復位了計數器(VAL)的值,開始計數并打開SysTick IRQ中斷。SysTick時鐘默認使用系統時鐘。
        下面的例程為使用固件庫V2.0.3進行SysTick設置:
        /* Select the HCLK Clock as SysTick clock source (72MHz) */
        SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
        /* SysTick end of count event each 1ms with input clock equal to 72MHz (HCLK) */
        SysTick_SetReload(72000);
        /* Enable SysTick interrupt */
        SysTick_ITConfig(ENABLE);
        下面的例程為使用標準外設庫V3.0.0進行SysTick設置:
        /* Setup SysTick Timer for 1 msec interrupts */
        if (SysTick_Config(SystemFrequency / 1000)) /* SystemFrequency is defined in “system_stm32f10x.h” and equal to HCLK frequency */
        {
        /* Capture error */
        while (1);
        }
        2.0庫函數延時代碼

        /*初始化時鐘*/

        本文引用地址:http://www.104case.com/article/201612/325082.htm

        void Init_SysTick(void)
        {
        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);

        /* Disable the SysTick Interrupt */
        SysTick_ITConfig(DISABLE);

        /* Configure HCLK clock as SysTick clock source */
        SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

        /* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
        SysTick_SetReload(9000);

        /* Enable the SysTick Interrupt */
        SysTick_ITConfig(ENABLE);
        }

        /*延時1ms函數*/

        __IO uint32_t TimingDelay;

        void delay_ms(__IO uint32_t nTime)
        {
        /* Enable the SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Enable);

        TimingDelay = nTime;

        while(TimingDelay != 0);

        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);

        /* Clear SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Clear);
        }

        /*stm32f10x_it.c中的void SysTick_Handler(void)函數改為*/
        extern __IO uint32_t TimingDelay;

        void SysTick_Handler(void)
        {
        if (TimingDelay != 0x00)
        {
        TimingDelay--;
        }
        }

        /*用法,延時1秒*/
        delay_ms(1000);

        3.5庫函數延時代碼

        /*初始化時鐘*/

        void Init_SysTick(void)
        {
        if(SysTick_Config(SystemCoreClock / 1000)) //注意:3.5庫中SystemFrequency 被SystemCoreClock 取代。
        while(1);
        }

        /*延時1ms函數*/

        __IO uint32_t TimingDelay;

        void delay_ms(__IO uint32_t nTime)
        {
        TimingDelay = nTime;
        while(TimingDelay != 0);
        }

        /*stm32f10x_it.c中的void SysTick_Handler(void)函數改為*/

        extern __IO uint32_t TimingDelay;

        void SysTick_Handler(void)
        {
        if (TimingDelay != 0x00)
        {
        TimingDelay--;
        }
        }

        /*用法,延時1秒*/

        delay_ms(1000);



        評論


        技術專區

        關閉
        主站蜘蛛池模板: 易门县| 黄骅市| 樟树市| 咸阳市| 宝坻区| 绥江县| 临西县| 柳江县| 周至县| 太仓市| 祁阳县| 大同县| 九寨沟县| 肥西县| 莱州市| 新津县| 镇坪县| 北海市| 湄潭县| 阿拉善右旗| 托里县| 柏乡县| 离岛区| 喀喇| 游戏| 罗江县| 长丰县| 卓资县| 邢台县| 绥江县| 桐庐县| 鄂州市| 汽车| 平舆县| 元谋县| 兴山县| 岳池县| 于都县| 贵港市| 时尚| 奉化市|