江科大stm32学习笔记——【3-2】GPIO输出:LED闪烁&LED流水灯&蜂鸣器

(一) 硬件连接

1.LED闪烁

LED灯正极连接面包板电源正极,LED负极连接单片机A0口

(也可以LED负极连面包板负极,LED正极连接单片机A0口)

跳线连接单片机3.3和面包板正极,连接单片机GND和面包板负极

2.LED流水灯

 

3.蜂鸣器

(二)代码

一.基础代码

以下所有库函数从gpio.h文件最下方查找,参数也是对函数定义查找后自己填写

1.使用RCC开启GPIO时钟: 

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

2.使用GPIO_Init函数初始化GPIO

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);

开漏输出的模式高电平没有驱动能力

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

GPIO_SetBits(GPIOA,GPIO_Pin_0);//将指定端口设置为高电平
GPIO_ResetBits(GPIOA,GPIO_Pin_0);//将指定端口设置为低电平
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);//将指定端口设置为高电平或低电平

二.LED闪烁

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){
	//使用RCC开启GPIO时钟:
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//使用GPIO_Init函数初始化GPIO:
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	while(1)
	{
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
		Delay_ms(500);
	}
}

延时函数要加头文件注明

三.LED流水灯

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){	
	//使用RCC开启GPIO时钟:
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	//使用GPIO_Init函数初始化GPIO:
	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 PA0
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0002);//对应二进制:0000 0000 0000 0010 PA1
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0004);//对应二进制:0000 0000 0000 0100 PA2
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0008);//对应二进制:0000 0000 0000 1000 PA3
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0010);//对应二进制:0000 0000 0001 0000 PA4
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0020);//对应二进制:0000 0000 0010 0000 PA5
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0040);//对应二进制:0000 0000 0100 0000 PA6
		Delay_ms(500);
		GPIO_Write(GPIOA,~0x0080);//对应二进制:0000 0000 1000 0000 PA7
		Delay_ms(500);
	}
}

 三.蜂鸣器

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void){
	//使用RCC开启GPIO时钟:
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	//使用GPIO_Init函数初始化GPIO:
	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_WriteBit(GPIOB,GPIO_Pin_12,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOB,GPIO_Pin_12,Bit_SET);
		Delay_ms(500);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值