当前位置:网站首页>STM32F103四种方法实现的LED闪灯程序
STM32F103四种方法实现的LED闪灯程序
2022-07-21 00:06:00 【卖身买镜头】
本程序为点亮两个LED,分别为LED1和LED2,分别连接到几口GPIOB的0脚和1脚。
因此首先对GPIOB的0脚和1脚机型初始化。
先创建LED的驱动程序, LED.H和 LED.C
LED.H
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//#define LED1 PBout(0)// PB0
//#define LED2 PBout(1)// PB1
#define LEDPORT GPIOB //定义LED的IO接口
#define LED1 GPIO_Pin_0 //定义LED1 的IO接口
#define LED2 GPIO_Pin_1 //定义LED2 的IO接口
void LED_Init(void);//初始化
#endif
LED.C
#include "led.h"
void LED_Init(void){
//LED灯的接口初始化
GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE); //设置A,B,C三个端口使能时钟
GPIO_InitStructure.GPIO_Pin = LED1 | LED2; //选择端口号(0,1号端口)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置IO接口工作方式为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置IO接口速度50MHz
GPIO_Init(LEDPORT, &GPIO_InitStructure);
}
/* 选择IO接口工作方式: GPIO_Mode_AIN 模拟输入 GPIO_Mode_IN_FLOATING 浮空输入 GPIO_Mode_IPD 下拉输入 GPIO_Mode_IPU 上拉输入 GPIO_Mode_Out_PP 推挽输出 GPIO_Mode_Out_OD 开漏输出 GPIO_Mode_AF_PP 复用推挽输出 GPIO_Mode_AF_OD 复用开漏输出 */
创建MAIN程序
main.c
#include "stm32f10x.h" //STM32头文件
#include "sys.h"
#include "delay.h"
#include "led.h"
int main (void){
//主程序
RCC_Configuration(); //时钟设置
LED_Init();
while(1){
//方法1:
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1)); //LED1接口输出高电平1
delay_us(50000); //延时1秒
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(0)); //LED1接口输出低电平0
delay_us(50000); //延时1秒
//方法2:
// GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //取反LED1
// delay_ms(500); //延时1秒
//方法3:
// GPIO_SetBits(LEDPORT,LED1); //LED灯都为高电平(1)
// delay_s(1); //延时1秒
// GPIO_ResetBits(LEDPORT,LED1); //LED灯都为低电平(0)
// delay_s(1); //延时1秒
//方法4
// GPIO_Write(LEDPORT,0x0001); //直接数值操作将变量值写入LED
// delay_s(2); //延时1秒
// GPIO_Write(LEDPORT,0x0000); //直接数值操作将变量值写入LED
// delay_s(2); //延时1秒
}
}
边栏推荐
- Get file type
- ipset v7.10: Kernel error received: set type not supported
- 小程序的破局之道,数字化营销已然成为趋势
- [2023 approved in advance] Ruijie
- Charles 安装及配置,详细步骤
- ICML 2022 | tutorial validity, reliability and significance: a statistical method tutorial for reproducible machine learning
- XML parsing
- JMeter regular, XPath, JSON
- 【2023提前批 之 面经】~ 联发科
- Encapsulate the global input component and bind parent-child data through V-model
猜你喜欢
随机推荐
Yolov7 experiment test 1: remote sensing image detection application
Flutter | print 及 dio 打印不完整的问题
浅聊一波moveit2
勤于奋分享国外LEAD任务了
PHP FPM custom ZABBIX monitoring
Hands on moveit2 | introduction and installation
Code之位运算
被鱼粉直呼小鱼牛逼的代码,来看ROS2如何进行点云PCL处理(订阅、转换、保存)
C#(四十二)之线程同步、互锁
Let me introduce you to the partition automatic management of data warehouse
王者荣耀商城异地多活部署设计
vim 编辑器学习笔记
Particle system in Niagara - ue5
Module learning (V) - matrix keyboard
知识图谱赋能数字经济,第十六届全国知识图谱与语义计算大会(CCKS 2022)即将召开
【精选】Mysql B-Tree和B+Tree的结构?
Talk about a wave of moveit2
动手学moveit2|介绍和安装
C#(三十八)之StreamWriter StreamWriter使用方法及与FileStream类的区别
从日常小事看如何工作