stm32标准库入门程序(笔记一GPIO)

操作stm32的GPIO总共需要4个步骤。
1.使用RCC开启GPIO的时钟。(在stm32中,所有的gpio都是挂载在APB2外设总线上)

2.GPIO结构体定义。

3.使用GPIO_Init函数初始化GPIO。

4.使用使用输出或输入的函数控制GPIO口。

(1)led闪烁

led.h

#ifndef __LED_H
#define __LED_H
void led(void);
#endif

led.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
void led(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	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_s(100);
		GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
		Delay_s(100);
	}
}

main.c

#include "stm32f10x.h"                  // Device header
#include "Led.h"
#include "delay.h"
int main(void)
{
	
	while (1)
	{
		led();

		
	}
}

(2)led流水灯

led.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
void led(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	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){
		for(u8 i=0;i<8;i++){
		GPIO_Write(GPIOA,~(0x0001<<i));
		Delay_s(100);
		}
	}
}

(3)按键控制led

main.c

#include "stm32f10x.h"                  // Device header
#include "Led.h"
#include "delay.h"
#include "Key.h"
uint8_t num=0;
int main(void)
{
	
	Led_Init();
	Key_Init();	
	while (1)
	{	
		num=Key_Getnum();
		if(num==1)
		{
		Led_Turn(GPIOA,GPIO_Pin_0);
		}
		if(num==2)
		{
		Led_Turn(GPIOA,GPIO_Pin_1);		
		}
	}
}

led.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
void Led_Init(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1);
}
void Led_On(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin){
	GPIO_ResetBits(GPIOX,GPIO_Pin);
}
void Led_Off(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin){
	GPIO_SetBits(GPIOX,GPIO_Pin);
}
void Led_Turn(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin)
{
	if(GPIO_ReadOutputDataBit(GPIOX,GPIO_Pin)==0){
	
		GPIO_SetBits(GPIOX,GPIO_Pin);
	}
	else{
		GPIO_ResetBits(GPIOX,GPIO_Pin);	
	}
}

key.c

#include "stm32f10x.h"                  // Device header
#include "delay.h"
void Key_Init(void)                    //按键初始化
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_Initstructure;
	GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
	GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_Initstructure);
}
uint8_t Key_Getnum(void)
{
	uint8_t num;
	if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0)	/判断引脚输入电平
	{
		Delay_s(20);		
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0);//判断按键是否松开(消抖)
		Delay_s(20);
		num=1;
		
	}
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)	
	{
		Delay_s(20);	
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);/
		Delay_s(20);
		num=2;
	}
	return num;
}

        通过引脚高低电平,获取返回值在主函数中根据返回值判断哪个灯亮灭。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值