一、初始化SysTick
按1ms来设置systick,也可以除以1000000。按1us来设置
-
SysTick_Config(SystemCoreClock / 1000); //SysTick开启系统tick定时器并初始化其中断,1ms
二、systick的函数
-
// cycles per microsecond
-
static volatile uint32_t usTicks = 0;
-
// current uptime for 1kHz systick timer. will rollover after 49 days. hopefully we won't care.
-
volatile uint32_t sysTickUptime = 0;
-
-
// SysTick
-
void SysTick_Handler(void)
-
{
-
sysTickUptime++;
-
}
-
-
//delay ms
-
void DelayMs(uint16_t nms)
-
{
-
uint32_t t0=micros();
-
while(micros() - t0 < nms * 1000);
-
}
-
-
// Return system uptime in microseconds (rollover in 70minutes)
-
//返回 us
-
uint32_t micros(void)
-
{
-
register uint32_t ms, cycle_cnt;
-
do {
-
ms = sysTickUptime;
-
cycle_cnt = SysTick->VAL;
-
} while (ms != sysTickUptime);
-
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
-
}
-
-
// Return system uptime in milliseconds (rollover in 49 days)
-
uint32_t millis(void)
-
{
-
return sysTickUptime;
-
}
systick启动后会进入SysTick_Handler()的中断,
三、少了重要的一部,哭~T.T
开启RCC时钟!!!!!
-
void cycleCounterInit(void)
-
{
-
RCC_ClocksTypeDef clocks;
-
RCC_GetClocksFreq(&clocks);
-
usTicks = clocks.SYSCLK_Frequency / 1000000;
-
}
遇到的错误:我在初始化MPU6050时,写入数据特别慢。明明计时都准的,不知道为什么特别慢。其他还都能顺利运行。
最后发现时RCC的时钟有问题。
阅读(1503) | 评论(0) | 转发(0) |