系统时钟SysTick(查询方式)

main.c

#include "stm32f10x.h"   // 相当于51单片机中的  #include <reg51.h>

#include "bsp_led.h"
#include "bsp_systick.h"

int main(void)
{
	LED_GPIO_Config();		//LED灯初始配置
	while(1)
	{
		SysTick_Delay_ms(500);
		LED_G_ON;
		SysTick_Delay_ms(500);
		LED_G_OFF;
	}
}

bsp_systick.h

#ifndef __BSP_SYSTICK_H
#define __BSP_SYSTICK_H

#include "stm32f10x.h"		//必须先于core_cm3.h,否则IRQn未定义。
#include "core_cm3.h"

void SysTick_Delay_us(uint32_t us);
void SysTick_Delay_ms(uint32_t ms);

#endif /* __BSP_SYSTICK_H */

bsp_systick.c

#include "bsp_systick.h"

#if 0
static __INLINE uint32_t SysTick_Config(uint32_t ticks)
{ 
	//判断tick的值是否大于2^24,如果大于,则不符合规则
	if (ticks > SysTick_LOAD_RELOAD_Msk)  return (1);            /* Reload value impossible */
	
	//初始化reload寄存器的值
	SysTick->LOAD  = (ticks & SysTick_LOAD_RELOAD_Msk) - 1;      /* set reload register */
	
	//配置中断优先级,配置为15,默认为最低的优先级
	NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  /* set Priority for Cortex-M0 System Interrupts */
	
	//初始化counter的值为0
	SysTick->VAL   = 0;                                          /* Load the SysTick Counter Value */
	
	//配置systick的时钟为72M
	//使能中断
	//使能systick
	SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk | 
									 SysTick_CTRL_TICKINT_Msk   | 
									 SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
	return (0);                                                  /* Function successful */
}
#endif

void SysTick_Delay_us(uint32_t us)
{
	uint32_t i;
	SysTick_Config(72);		//1us
	
	for(i=0; i<us; i++)
	{
		while( !((SysTick->CTRL) & (1<<16)) );		//检测是否计时结束
	}
	
	SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
}

void SysTick_Delay_ms(uint32_t ms)
{
	uint32_t i;
	SysTick_Config(72000);		//1ms
	
	for(i=0; i<ms; i++)
	{
		while( !((SysTick->CTRL) & (1<<16)) );		//检测是否计时结束
	}
	
	SysTick->CTRL &= ~ SysTick_CTRL_ENABLE_Msk;
}


bsp_led.c 及 bsp_led.h

点亮LED灯(固件库版)

代码参考野火教程,有所修改