STM32 LED闪烁&LED流水灯&蜂鸣器


本文是STM32学习笔记,方便后续复习,学习视频是b站江科大STM32学习教程 江科大LED闪烁&LED流水灯&蜂鸣器

操作GPIO

使用RCC开启GPIO时钟

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);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);			//开启GPIOA时钟

找到需要使用的GPIO挂载在哪一根总线上,打开对应的时钟

使用GPIO_Init函数初始化GPIO

注意不要使用PA15,PB3,PB4,这三个端口是JTAG的调试端口,如果要用作普通端口的话,还需要再进行一些其他配置

GPIO_InitTypeDef GPIO_InitStructure;						//定义GPIO初始化结构体
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;			//推挽输出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;					//选择引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			//目前随便给一个,一般给50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);						//PA0初始化

使用输入/输出函数控制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); 		//可以同时对16个端口进行写入操作

LED闪烁

只需要不断地改变端口电平即可
延时的长短就是LED闪烁的快慢

while (1)
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_0);	//端口置低电平
		Delay_ms(500);						//延时
		GPIO_SetBits(GPIOA, GPIO_Pin_0);	//端口置高电平
		Delay_ms(500);						//延时
	}

LED流水灯

只需要循环交替改变各个端口的电平
可以定义一个数组花式点灯

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

//初始化GPIOA所有引脚	
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
	
while (1)
{
	//低电平有效
	GPIO_Write(GPIOA, ~0x0001);	//0000 0000 0000 0001
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0002);	//0000 0000 0000 0010
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0004);	//0000 0000 0000 0100
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0008);	//0000 0000 0000 1000
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0010);	//0000 0000 0001 0000
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0020);	//0000 0000 0010 0000
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0040);	//0000 0000 0100 0000
	Delay_ms(100);
	GPIO_Write(GPIOA, ~0x0080);	//0000 0000 1000 0000
	Delay_ms(100);
}

蜂鸣器

用的是有源蜂鸣器,其控制类似LED灯

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

//使用PB12
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值