/** ****************************************************************************** * @file main.c * @author iuc * @version version 1.0 * @date 2015-5-19 19:37:52 * @brief 流水灯闪烁 ****************************************************************************** * @attention * 实现流水灯以间隔500ms的时间闪烁(系统定时器SysTick实现的精确延时) * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u32 temp = 0; /* Private function prototypes -----------------------------------------------*/ void Delay_SysTick_Init(void); void Led_Init(void); void Delay_us(unsigned n); /* Private functions ---------------------------------------------------------*/ /** * @brief * @param * @retval */ int main(void) { Led_Init(); Delay_SysTick_Init(); while(1) { GPIO_SetBits(GPIOD,GPIO_Pin_1); Delay_us(500); //延时500毫秒 GPIO_ResetBits(GPIOD,GPIO_Pin_1); Delay_us(500); } } void Led_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); // 开启时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); } void Delay_SysTick_Init(void) { if(SysTick_Config(72000)) // 设置为计数72000次进入中断一次,就是1毫秒进入中断一次 { while(1); } SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk; // 关闭滴答定时器,待需要的时候再打开 } void Delay_us(unsigned n) // 延时n毫秒 { temp = n; SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; // 开启滴答定时器 while(temp != 0); }
转载于:https://www.cnblogs.com/iucforever/p/4515382.html