点亮一颗LED灯

TOC


LED0

c 复制代码
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能APB2的外设时钟
	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(GPIOB,&GPIO_Initstructure);//配置端口模式
	GPIO_SetBits(GPIOB,GPIO_Pin_5);//关闭LED灯

LED1

c 复制代码
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能APB2的外设时钟
	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_SetBits(GPIOE,GPIO_Pin_5);//关闭LED灯

Periph ---------Peripherals---pəˈrɪfərəlz---外设

Hardware中添加LED.C和LED.h文件

LED.h文件中,添加一段防止头文件重复的代码

LED.h

c 复制代码
#ifndef __LED_H
#define __LED_H
void LED1_Init(void);
void LED0_Init(void);
void LED1_On(void);
void LED1_Off(void);
void LED1_Turn(void);
void LED0_On(void);
void LED0_Off(void);
void LED0_Turn(void);
#endif

注意最后一行空

LED.c

LED.C 文件中,右键添加"stm32f10x.h"头文件

c 复制代码
#include "stm32f10x.h"                  // Device header
void LED1_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,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_SetBits(GPIOE,GPIO_Pin_5);
}
void LED0_Init(void)
{
	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(GPIOB,&GPIO_Initstructure);
	GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
void LED1_On(void)
{
	GPIO_ResetBits(GPIOE,GPIO_Pin_5);
}
void LED1_Off(void)
{
	GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
void LED1_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_5) == 1)
	{
		GPIO_ResetBits(GPIOE,GPIO_Pin_5);
	}
	else
	{
		GPIO_SetBits(GPIOE,GPIO_Pin_5);
	}	
}
void LED0_On(void)
{
	GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
void LED0_Off(void)
{
	GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
void LED0_Turn(void)
{
	if(GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_5) == 1)
	{
		GPIO_ResetBits(GPIOB,GPIO_Pin_5);
	}
	else
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_5);
	}	
}
相关推荐
不悔哥4 天前
开源OV-Watch:怎么做一个智能手表
单片机·开源·嵌入式
XiHongShi20164 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
优信电子4 天前
AIT1001 非接触式红外测温模块 STM32 驱动开发实战:从引脚到代码全解析
stm32·嵌入式·温度传感器·红外测温·非接触式测温·ait1001
深圳老胡4 天前
STM32F407 控制 L6470 步进电机驱动 —— 控制过程简介
笔记·stm32·单片机·嵌入式硬件·代码规范
wuyk5554 天前
《WiFi 嵌入式物联网开发全套实战》| 第 16 章 ESP32 AP+STA 双模共存原理与工程坑点
网络·stm32·物联网
女神下凡4 天前
嵌入式设计的各种存储芯片的硬件/软件设计规范,非常全面。
arm开发·单片机·嵌入式硬件
梅梅9664 天前
一款mipi转lvds的lcd调试(LT8912B)
单片机·计算机外设·电脑·显示器·pd芯片
开发笔记-阿牛4 天前
蓝牙音乐灯拆解,又一次拆到蓝牙音乐灯芯片CK6865L
人工智能·单片机·嵌入式硬件·音视频·音频
从零开始的嵌入式之旅4 天前
day47
arm开发·经验分享·笔记·嵌入式硬件
恒锐丰科技林技术员4 天前
EG1164 高压大电流升压同步整流芯片解析
经验分享·嵌入式硬件·硬件工程