[stm32-1]LED闪烁&LED流水灯&蜂鸣器

1、LED闪烁&LED流水灯&蜂鸣器

1.使用RCC开启GPIO时钟

2.使用GPIO_Init函数初始化GPIO

3.使用输入或输出函数控制GPIO口

RCC常用的3个库函数:

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);

void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
void GPIO_DeInit(GPIO_TypeDef* GPIOx); //所指定的GPIO外设会被复位

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); //用结构体的参数来初始化GPIO口

void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct); //结构体变量赋一个默认值

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); //GPIO读取函数

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); //GPIO写入函数
GPIO_Mode_AIN //模拟输入

GPIO_Mode_IN_FLOATING = 0x04, //浮空输入

GPIO_Mode_IPD = 0x28, //下拉输入

GPIO_Mode_IPU = 0x48, //上拉输入

GPIO_Mode_Out_OD = 0x14, //开漏输出

GPIO_Mode_Out_PP = 0x10, //推挽输出

GPIO_Mode_AF_OD = 0x1C, //复用开漏

GPIO_Mode_AF_PP = 0x18 //复用推挽
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); //指定端口设置为高电平

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); //低电平

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

复制代码
#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructer;
	GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructer.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructer);
	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
	//GPIO_SetBits(GPIOA,GPIO_Pin_0);
	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
	while(1)
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_0);
		Delay_ms(500);
		GPIO_SetBits(GPIOA,GPIO_Pin_0);
		Delay_ms(500);
//		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
//		Delay_ms(500);
//		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
//		Delay_ms(500);
	}
}

把LED长脚接A0,短脚接-,LED闪烁:推挽输出,高低电平均可驱动。

复制代码
#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructer;
	GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructer.GPIO_Pin=GPIO_Pin_All;//GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
	GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructer);
//	//GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//	//GPIO_SetBits(GPIOA,GPIO_Pin_0);
//	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
	while(1)
	{
		GPIO_Write(GPIOA,~0x0001);//0000000000000001
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0002);//0000000000000010
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0004);//0000000000000100
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0008);//0000000000001000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0010);//0000000000010000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0020);//0000000000100000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0040);//0000000001000000
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0080);//0000000010000000
		Delay_ms(500);
	}
}
复制代码
#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructer;
	GPIO_InitStructer.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructer.GPIO_Pin=GPIO_Pin_12;
	GPIO_InitStructer.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructer);
	
	while(1)
	{
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_ResetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(100);
		GPIO_SetBits(GPIOB,GPIO_Pin_12);
		Delay_ms(700);
	}
}
相关推荐
景彡先生26 分钟前
STM32 USB开发详解:CDC虚拟串口与HID键盘鼠标(基于CubeUSB库)
stm32·嵌入式硬件·计算机外设
weixin_580382069 小时前
UART寄存器介绍
stm32·单片机·嵌入式硬件
wind_one113 小时前
STM32小实验1--点亮LED
stm32·单片机·嵌入式硬件
szxinmai主板定制专家14 小时前
基于光栅传感器+FPGA+ARM的测量控制解决方案
arm开发·人工智能·嵌入式硬件·fpga开发
Ronin-Lotus16 小时前
嵌入式硬件篇---单稳态&多谐&施密特电路
单片机·嵌入式硬件
逼子格16 小时前
权电阻网络DAC实现电压输出型数模转换Multisim电路仿真——硬件工程师笔记
笔记·嵌入式硬件·硬件工程·硬件工程师·adc·硬件工程师真题·权电阻网络dac
Cyrus_柯18 小时前
单片机(STM32-串口通信)
stm32·单片机·嵌入式硬件·串口通信协议
Ronin-Lotus20 小时前
嵌入式硬件篇---晶体管的分类
嵌入式硬件·晶体管·bjt(双级结性晶体管)·fet(场效应晶体管)
熬夜的猪仔1 天前
第二章 基于新版Onenet搭建云服务(stm32物联网)
stm32·物联网·freertos
会编程的小孩1 天前
STM32用PWM驱动步进电机
stm32·单片机·嵌入式硬件