点亮一颗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);
	}	
}
相关推荐
玖別ԅ(¯﹃¯ԅ)2 小时前
ADC的实现(单通道,多通道,DMA)
stm32·单片机·嵌入式硬件
anghost1503 小时前
基于单片机的防酒驾系统设计
单片机·嵌入式硬件·毕业设计·流程图
lepton_yang4 小时前
Zephyr下控制ESP32S3的GPIO口
linux·嵌入式硬件·esp32·zephyr
AI+程序员在路上4 小时前
单片机驱动LCD显示模块LM6029BCW
c语言·单片机·嵌入式硬件
XINVRY-FPGA5 小时前
10CL016YF484C8G Altera FPGA Cyclone
嵌入式硬件·网络协议·fpga开发·云计算·硬件工程·信息与通信·fpga
Hero_11276 小时前
学习Stm32 的第一天
stm32·嵌入式硬件·学习
ye150127774559 小时前
DC6v-36V转3.2V1A恒流驱动芯片WT7017
单片机·嵌入式硬件·其他
scilwb19 小时前
RoboCon考核题——scilwb
单片机
点灯小铭20 小时前
基于STM32单片机智能RFID刷卡汽车位锁桩设计
stm32·单片机·汽车·毕业设计·课程设计
bai54593621 小时前
STM32 软件I2C读写MPU6050
stm32·单片机·嵌入式硬件