108 lines
2.6 KiB
C
108 lines
2.6 KiB
C
|
#include "systick_delay.h"
|
|||
|
#include "hk32f030m.h"
|
|||
|
|
|||
|
static uint8_t fac_us = 0;
|
|||
|
static uint16_t fac_ms = 0;
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* @2017-12-16
|
|||
|
* Function Name : delay_init
|
|||
|
* Description : <EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|||
|
* Input : none
|
|||
|
* Output : none
|
|||
|
* Return : none
|
|||
|
* SYSTICK<EFBFBD><EFBFBD>ʱ<EFBFBD>ӹ̶<EFBFBD>ΪHCLKʱ<EFBFBD>ӵ<EFBFBD>1/8
|
|||
|
* SYSCLK:ϵͳʱ<EFBFBD><EFBFBD>
|
|||
|
*******************************************************************************/
|
|||
|
void delay_init(void)
|
|||
|
{
|
|||
|
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
|
|||
|
fac_us = SystemCoreClock/8000000;
|
|||
|
fac_ms = (uint16_t)fac_us*1000;
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* @2017-12-16
|
|||
|
* Function Name : delay_us
|
|||
|
* Description : us<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* Input : nus--<EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
* Output : none
|
|||
|
* Return : none
|
|||
|
*******************************************************************************/
|
|||
|
void delay_us(uint32_t nus)
|
|||
|
{
|
|||
|
if( nus > 0 )
|
|||
|
{
|
|||
|
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>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/*******************************************************************************
|
|||
|
* @2017-12-16
|
|||
|
* Function Name : delay_ms
|
|||
|
* Description : <EFBFBD><EFBFBD>ʱnms<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
|
|||
|
ע<EFBFBD><EFBFBD>nms<EFBFBD>ķ<EFBFBD>Χ
|
|||
|
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>32M<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,nms<=4194
|
|||
|
* Input :
|
|||
|
* Output :
|
|||
|
* Return :
|
|||
|
*******************************************************************************/
|
|||
|
void delay_ms(uint16_t nms)
|
|||
|
{
|
|||
|
uint32_t temp;
|
|||
|
|
|||
|
if( nms > 0 )
|
|||
|
{
|
|||
|
SysTick->LOAD=(uint32_t)nms*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>
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|