STM32 - LED流水灯

主要功能:两个OLED的闪烁(PE5和PB5),间隔500ms。

可以继续增加更多的OLED灯。

下面为主要代码main.c:

cs 复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    //推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOE,&GPIO_InitStructure);
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	while(1)
	{
     GPIO_WriteBit(GPIOE, GPIO_Pin_5,Bit_RESET);
		 Delay_ms(500);
		 GPIO_WriteBit(GPIOE, GPIO_Pin_5,Bit_SET);
		 Delay_ms(500);
		
		 GPIO_WriteBit(GPIOB, GPIO_Pin_5,Bit_RESET);
		 Delay_ms(500);
		 GPIO_WriteBit(GPIOB, GPIO_Pin_5,Bit_SET);
		 Delay_ms(500);
	}
	
}
 

下面为延时函数,可以添加到项目中直接调用,有三个等级的延时:Delay_us(微秒级别)、Delay_ms(毫秒级别)和Delay_s(秒级别)。

Delay.h:

cs 复制代码
#ifndef __DELAY_H
#define __DELAY_H

void Delay_us(uint32_t us);
void Delay_ms(uint32_t ms);
void Delay_s(uint32_t s);

#endif

Delay.c:

cs 复制代码
#include "stm32f10x.h"

/**
  * @brief  微秒级延时
  * @param  xus 延时时长,范围:0~233015
  * @retval 无
  */
void Delay_us(uint32_t xus)
{
	SysTick->LOAD = 72 * xus;				//设置定时器重装值
	SysTick->VAL = 0x00;					//清空当前计数值
	SysTick->CTRL = 0x00000005;				//设置时钟源为HCLK,启动定时器
	while(!(SysTick->CTRL & 0x00010000));	//等待计数到0
	SysTick->CTRL = 0x00000004;				//关闭定时器
}

/**
  * @brief  毫秒级延时
  * @param  xms 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_ms(uint32_t xms)
{
	while(xms--)
	{
		Delay_us(1000);
	}
}
 
/**
  * @brief  秒级延时
  * @param  xs 延时时长,范围:0~4294967295
  * @retval 无
  */
void Delay_s(uint32_t xs)
{
	while(xs--)
	{
		Delay_ms(1000);
	}
} 
相关推荐
生涯にわたる学び2 小时前
ARM 实操 流水灯 按键控制 day53
arm开发·嵌入式硬件
whaosoft-1432 小时前
w嵌入式分享合集68
嵌入式硬件
竹照煜_ysn4 小时前
STM32——软硬件I2C
stm32·嵌入式硬件·mongodb
Ronin-Lotus5 小时前
嵌入式硬件篇---电感串并联
嵌入式硬件
Wallace Zhang6 小时前
STM32 - Embedded IDE - GCC - 显著减少固件的体积
stm32·单片机·嵌入式硬件
fengfuyao98516 小时前
STM32如何定位HardFault错误,一种实用方法
stm32·单片机·嵌入式硬件
爱学习的颖颖17 小时前
EXTI外部中断的执行逻辑|以对射式红外传感器计次为例
单片机·嵌入式硬件·exti中断
keer_zu18 小时前
STM32L051 RTC闹钟配置详解
stm32·嵌入式硬件
AI精钢18 小时前
H20芯片与中国的科技自立:一场隐形的博弈
人工智能·科技·stm32·单片机·物联网
etcix21 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机