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);
	}
} 
相关推荐
曜华激光4 分钟前
光伏电池串性能测试仪检测精度极高
单片机·嵌入式硬件
电化学仪器白超15 分钟前
20251209Ver8(精密电流源温漂特性测试报告)
python·单片机·嵌入式硬件·自动化
炸膛坦客31 分钟前
Cortex-M3-STM32F1 开发:(三十六)APB1 和 APB2 总线的内部构成
stm32·单片机·嵌入式硬件
创思通信1 小时前
STM32F103C8T6采 DS18B20,通过A7680C 4G模块不断发送短信到手机
javascript·stm32·智能手机
钿驰科技1 小时前
TC - BL2840 三相有感直流无刷电机驱动板
单片机·嵌入式硬件
小刘爱玩单片机1 小时前
【stm32简单外设篇】- LCD1602A
c语言·stm32·单片机·嵌入式硬件
意法半导体STM321 小时前
【官方原创】在H563上使用RTX5 RTOS LAT1584
stm32·单片机·嵌入式硬件·mcu
mastercoder--2 小时前
速通51单片机————矩阵键盘及其应用
嵌入式硬件·计算机外设·51单片机
d111111111d2 小时前
STM32 I2C通信详解:从机地址与寄存器地址的作用
笔记·stm32·单片机·嵌入式硬件·学习
普中科技2 小时前
【普中51单片机开发攻略--基于普中-2&普中-3&普中-4】-- 第 14 章 矩阵按键实验
单片机·嵌入式硬件·51单片机·开发板·按键检测·矩阵按键·普中科技