38 lines
750 B
Plaintext
38 lines
750 B
Plaintext
/**
|
|||
|
|
* @file systick.cpp
|
||
|
|
* @brief SysTick 时基配置、中断处理与毫秒延时实现。
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "../main.h"
|
||
|
|
#include "systick.h"
|
||
|
|
|
||
|
|
/** @brief 自启动后累计的系统节拍毫秒数。 */
|
||
|
|
static volatile uint32_t systick_ms = 0U;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief SysTick 中断服务例程,每毫秒递增一次节拍计数器。
|
||
|
|
*/
|
||
|
|
void SysTick_Handler(void)
|
||
|
|
{
|
||
|
|
systick_ms++;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 执行阻塞式毫秒延时。
|
||
|
|
* @param[in] ms 需要延时的毫秒数。
|
||
|
|
*/
|
||
|
|
void delay_ms(uint32_t ms)
|
||
|
|
{
|
||
|
|
uint32_t start = systick_ms;
|
||
|
|
while((systick_ms - start) < ms) {
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 配置 SysTick 为 1ms 系统节拍。
|
||
|
|
*/
|
||
|
|
void systick_config_ms(void)
|
||
|
|
{
|
||
|
|
SystemCoreClockUpdate();
|
||
|
|
SysTick_Config(SystemCoreClock / 1000U);
|
||
|
|
}
|