需要屏蔽官方SDK里HAL_Init里的HAL_InitTick(TICK_INT_PRIORITY);
代码参考STM32相关实现,CTRL直接等于0了,之前用啥 |= ~ &,不知道systick一直进中断,看寄存器是中断位置1了,所以直接改等于0了
uint8_t fac_us;//us
uint32_t fac_ms;//ms
// Public functions prototypes --------------------------------------------------
// Functions --------------------------------------------------------------------
/**
* Function : lib_cs32_delay_init
* Description : 初始化systick
* Input :
*
* Output :
* Return :
* Auther :
* Others :
**/
void lib_cs32_delay_init(void)
{
// HAL_SYSTICK_Config(SystemCoreClock);
// SysTick->CTRL = ~SysTick_CTRL_CLKSOURCE_Msk ;//必须使用内核时钟HCLK/4,否则nms * fac_ms - 1会溢出
//毫秒的重装载值为:每秒跳动的次数为24000000,ms就为24000,同理us为24
fac_us = 6;
fac_ms = fac_us * 1000;
}
/**
* Function : lib_cs32_delay_us
* Description : 延时Nus
* Input :
*
* Output :
* Return :
* Auther :
* Others :
**/
void lib_cs32_delay_us(uint16_t nus)
{
uint32_t temp = 0;
SysTick->LOAD = nus * fac_us - 1;//时间加载
SysTick->VAL = 0;//清空计数器
SysTick->CTRL = 1;//使能,开始计数
do
{
temp = SysTick->CTRL;
}
while(temp & 0x01 && !(temp & (1 << 16)));//等待时间到达
SysTick->CTRL = 0;//关闭计数器
SysTick->VAL = 0;//清空计数器
}
/**
* Function : lib_cs32_delay_ms
* Description : 延时Nms
* Input :
*
* Output :
* Return :
* Auther : zhaoning
* Others :
**/
void lib_cs32_delay_ms(uint16_t nms)
{
uint32_t temp = 0;
SysTick->LOAD = (uint16_t)nms * fac_ms - 1;//时间加载(SysTick->LOAD为24bit)
SysTick->VAL = 0;//清空计数器
SysTick->CTRL = 1;//使能,开始计数
do
{
temp = SysTick->CTRL;
}
while(temp & 0x01 && !(temp & (1 << 16)));//等待时间到达
SysTick->CTRL = 0;//关闭计数器
SysTick->VAL = 0;//清空计数器
}