GeekIMU/2.Firmware/STM32/Firmware/system/delay/delay.c

84 lines
3.2 KiB
C
Raw Permalink Normal View History

2024-11-09 21:39:20 +08:00
/******************** (C) COPYRIGHT 2020 GEEKIMU *******************************
* File Name : delay.c
* Current Version : V2.0 & ST 3.5.0
* Author : zhanli 719901725@qq.com & JustFeng.
* Date of Issued : 2015.5.15 zhanli : Create
* Comments : STM32ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
********************************************************************************/
#include <stdint.h>
#include "delay.h"
#include "misc.h"
static uint8_t fac_us = 0; // us<75><73>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
static uint16_t fac_ms = 0;
/**----------------------------------------------------------------------
* Function : Delay_Init
* Description : <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>ucos<EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void Delay_Init(void)
{
// ѡ<><D1A1><EFBFBD>ⲿʱ<E2B2BF><CAB1> HCLK/8
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
// Ϊϵͳʱ<CDB3>ӵ<EFBFBD>1/8
fac_us = SystemCoreClock / 8000000;
// <20><>ucos<6F><73>,<2C><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF>ms<6D><73>Ҫ<EFBFBD><D2AA>systickʱ<6B><CAB1><EFBFBD><EFBFBD>
fac_ms = (uint16_t)fac_us * 1000;
}
/**----------------------------------------------------------------------
* Function : Delay_DeInit
* Author : zhanli&719901725@qq.com
* Description : <EFBFBD><EFBFBD><EFBFBD>³<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֧<EFBFBD><EFBFBD>ucos<EFBFBD><EFBFBD>
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void Delay_DeInit(void){
SysTick->CTRL = 0;
}
/**----------------------------------------------------------------------
* Function : Delay_Ms(uint32_t ms)
* Description : <EFBFBD><EFBFBD>ʱn<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ȡֵ<EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>1864.SysTick->LOADΪ
24λ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱΪ:
nms <= 0xffffff * 8 * 1000 / SYSCLK; SYSCLK<EFBFBD><EFBFBD>λΪHz,nms<EFBFBD><EFBFBD>
λΪms.<EFBFBD><EFBFBD>Ӧ72M<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ,nms<=1864nms<EFBFBD><EFBFBD>
* Author : zhanli&719901725@qq.com
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void Delay_Ms(uint32_t ms)
{
uint32_t temp;
SysTick->LOAD =(u32)ms*fac_ms; //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(SysTick->LOADΪ24bit)
SysTick->VAL = 0x00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16))); //<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //<2F>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL =0X00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
}
/**----------------------------------------------------------------------
* Function : Delay_Us(uint32_t nus)
* Description : <EFBFBD><EFBFBD>ʱn΢<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>ȡֵ<EFBFBD><EFBFBD><EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>1864us
* Author : zhanli&719901725@qq.com
* Date : 2015/2/13 zhanli
*---------------------------------------------------------------------**/
void Delay_Us(uint32_t nus)
{
uint32_t temp;
SysTick->LOAD=nus*fac_us; //ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL=0x00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
do{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16))); //<2F>ȴ<EFBFBD>ʱ<EFBFBD><EFBFBD><E4B5BD>
SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //<2F>رռ<D8B1><D5BC><EFBFBD><EFBFBD><EFBFBD>
SysTick->VAL =0x00; //<2F><><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
}