78 lines
1.6 KiB
C
78 lines
1.6 KiB
C
#include "includes.h"
|
|
|
|
volatile u32 Systick_TickCount = 0;
|
|
|
|
void SysTick_Handler(void)
|
|
{
|
|
Systick_TickCount++;
|
|
// if(Systick_TickCount % 10 == 0)
|
|
// printf("#");
|
|
}
|
|
|
|
u32 GetDelayTick(u32 ms)
|
|
{
|
|
return GetTickCount() + ms;
|
|
}
|
|
|
|
// 考虑了u32溢出翻转的情况
|
|
u8 IsTickOut(u32 OutTick)
|
|
{
|
|
u32 tick = GetTickCount();
|
|
|
|
if(tick > OutTick)
|
|
{
|
|
if(tick - OutTick > 0x80000000UL)
|
|
return 0;
|
|
return 1;
|
|
}
|
|
if(OutTick - tick > 0x80000000UL)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
// 考虑了u32溢出翻转的情况
|
|
u32 GetTickElapse(u32 start_tick, u32 end_tick)
|
|
{
|
|
if(end_tick >= start_tick)
|
|
return (end_tick - start_tick);
|
|
return (0xFFFFFFFFUL - start_tick + end_tick);
|
|
}
|
|
|
|
// 这个函数必须在初始化完成,中断开启以后才有效
|
|
void delay_ms(u32 ms)
|
|
{
|
|
u32 tick = GetDelayTick(ms);
|
|
|
|
do
|
|
{
|
|
} while(!IsTickOut(tick));
|
|
}
|
|
|
|
// 通过TMR2来产生1ms中断
|
|
void Clock_Init()
|
|
{
|
|
u32 us = CyclesPerUs * 1000; // 1ms
|
|
|
|
if ((us - 1UL) > SysTick_LOAD_RELOAD_Msk)
|
|
return; /* Reload value impossible */
|
|
|
|
SysTick->LOAD = (uint32_t)(us - 1UL); /* set reload register */
|
|
NVIC_SetPriority (SysTick_IRQn, 0); /* set Priority for Systick Interrupt */
|
|
SysTick->VAL = 0UL; /* 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 */
|
|
}
|
|
|
|
void delay_us(uint16_t us)
|
|
{
|
|
uint32_t j;
|
|
|
|
while(us--)
|
|
{
|
|
j = SystemCoreClock / 1000000;
|
|
while(j--);
|
|
}
|
|
}
|
|
|