铁头山羊stm32 (2)LED闪灯实验 、GPIO4种输入模式

#include "stm32f10x.h"
#include "delay.h"

int main(void)
{
	//1.开启GPIO时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//固定写法
	
	//2.初始化IO引脚,pc13通用输出开漏模式2MHZ
	//GPIO_Init 初始化IO引脚
	
	GPIO_InitTypeDef GPIO_InitStruct ={0};
	
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_OD;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
	
	GPIO_Init(GPIOC,&GPIO_InitStruct);
	
//	GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_SET);
//	GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_RESET);

	while(1)
	{
		GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_RESET);//亮
		
		Delay(100);//延迟100ms
		
		GPIO_WriteBit(GPIOC,GPIO_Pin_13,Bit_SET);//灭
		
		Delay(100);//延迟100ms
		
	}
}

2.4 GPIO的4种输入模式

1.保护二极管:防静电,人接触的时候,可能会产生很强的静电,会将芯片烧坏

手上为正电,电从VDD流走;手上为负电,电从VSS溜走

2.上/下拉电阻:稳定作用

当LED引脚处于悬空的时候:

上拉电阻:提供默认高电压;下拉电阻:提供默认低电压

3.施密特触发器:电阻无限大

将输入的电压转换成1/0

2.5 GPIO按钮实验

推挽接法:写0熄灭,写1点亮

开漏接法:写0点亮,写1熄灭

LED二极管:长的阳极 短的阴极

LED灯是否亮代码

#include "stm32f10x.h"

int main(void)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//选择推挽模式
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);

	while(1)
	{
		
	}
}

当按钮没有按下:PA1处于悬空的,上拉电阻为3.3v,此时输入寄存器中读到的为1

当按钮按下时:PA1此时接地,此时输入寄存器中读到的为0

从输入寄存器中判断读的数是0/1,由此可得按钮是否按下-->按钮工作电路工作原理

#include "stm32f10x.h"

int main(void)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//选择推挽模式
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;//使用的是PA1
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//选择输入上拉模式
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	while(1)
	{
		if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)==Bit_RESET)//按钮按下
		{
			GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);//灯亮
		}
		else
		{
			GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值