当前位置:网站首页>基于Cortex-M3、M4的精准延时(系统定时器SysTick延时,可用于STM32、ADuCM4050等)

基于Cortex-M3、M4的精准延时(系统定时器SysTick延时,可用于STM32、ADuCM4050等)

2022-06-25 04:01:00 网易独家音乐人Mike Zhou

基于Cortex-M3、M4的精准延时(系统定时器SysTick延时,可用于STM32、ADuCM4050等)

#include "ADuCM4050.h"
#include "stm32f4xx.h"

void delay_ms(unsigned int ms)
{
    
	while(ms--)
	{
    
		SysTick->CTRL = 0; // Disable SysTick ¹Ø±Õϵͳ¶¨Ê±Æ÷
		SysTick->LOAD = 52000000/1000-1; // Count from 255 to 0 (256 cycles) ÔØÈë¼ÆÊýÖµ ¶¨Ê±Æ÷´ÓÕâ¸öÖµ¿ªÊ¼¼ÆÊý
		SysTick->VAL = 0; // Clear current value as well as count flag Çå¿Õ¼ÆÊýÖµµ½´ï0ºóµÄ±ê¼Ç
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock ʹÄÜ52MHzµÄϵͳ¶¨Ê±Æ÷
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set µÈ´ý
	}
	SysTick->CTRL = 0; // Disable SysTick ¹Ø±Õϵͳ¶¨Ê±Æ÷
}
void delay_us(unsigned int us)
{
    
	while(us--)
	{
    
		SysTick->CTRL = 0; // Disable SysTick ¹Ø±Õϵͳ¶¨Ê±Æ÷
		SysTick->LOAD = 52000000/1000/1000-1; // Count from 255 to 0 (256 cycles) ÔØÈë¼ÆÊýÖµ ¶¨Ê±Æ÷´ÓÕâ¸öÖµ¿ªÊ¼¼ÆÊý
		SysTick->VAL = 0; // Clear current value as well as count flag Çå¿Õ¼ÆÊýÖµµ½´ï0ºóµÄ±ê¼Ç
		SysTick->CTRL = 5; // Enable SysTick timer with processor clock ʹÄÜ52MHzµÄϵͳ¶¨Ê±Æ÷
		while ((SysTick->CTRL & 0x00010000)==0);// Wait until count flag is set µÈ´ý
	}
	SysTick->CTRL = 0; // Disable SysTick ¹Ø±Õϵͳ¶¨Ê±Æ÷
}

其中的52000000表示芯片的系统定时器频率 32系列一般为外部定时器频率的两倍

原网站

版权声明
本文为[网易独家音乐人Mike Zhou]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_53403301/article/details/125420717