1.GPIO 模块的原理图:


2.首先介绍,led模块,
led 模块的引脚:
LED1 ----PE8
LED2 ----PE9
LED3 ----PE10
代码:
led.h
-
- void delay(void);
- void ledinit(void);//初始化
- void select_led(int a);//假如a != 1 2 3 ,就关闭所有的灯
- void liushui_led(void);
- void close_led(void );//关闭所有的小灯
-
led.c
- #include "stm32f4xx.h" //头文件
- #include "led.h"
- void delay()//延时函数
- {
- int b=5000000;
- while(b--)//形成延时函数
- {
-
- }
- }
-
- void ledinit()
- {
- //led 低电平驱动
- //3 个led 可控制,一个不可控制
- //二极管单向导通,阳极固定高电平,阴极输出高电平,led 灭
- //二极管单向导通,阳极固定高电平,阴极输出低电平,led 亮
- GPIO_InitTypeDef GIOSTRUCT;
-
-
- //1.配置时钟: 给个心跳。 选择需要的时钟频率
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
-
- //2.配置功能: 功能太多。 选择GPIO功能;
- GIOSTRUCT.GPIO_Mode=GPIO_Mode_OUT ; //输出模式 //引脚输出信号
- GIOSTRUCT.GPIO_Pin=GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10;//led 灯的引脚
- GIOSTRUCT.GPIO_PuPd=GPIO_PuPd_UP; //上拉电阻
- GIOSTRUCT.GPIO_Speed=GPIO_Low_Speed;//配置速度
- //外部无上拉电阻选择推挽输出, 外部有上拉电阻选择开漏输出
- GIOSTRUCT.GPIO_OType=GPIO_OType_PP;
- GPIO_Init(GPIOE,&GIOSTRUCT);
-
- //GPIO_Init(GPIOE,&GIOSTRUCT);
-
-
- //3.操作引脚: 输出低电平 led 亮, 输出高电平 led灭
-
- //初始化
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
-
- //GPIO_ResetBits 输出低电平
- //GPIO_SetBits 输出高电平
-
- }
-
-
- void select_led(int a)//假如a != 1 2 3 ,就关闭所有的灯
- {
-
-
- if(a==1)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_8);//串口组, 引脚//打开第一个灯
- }
- if(a==2)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_9);//串口组, 引脚//打开第二个灯
- }
- if(a==3)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_10);//串口组, 引脚//打开第三个灯
- }
-
-
- }
-
- void close_led(void )//关闭所有的小灯
- {
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
- }
-
-
- void liushui_led()//流水灯 , 功能: 从第一个灯 轮流闪烁 一直到最后一个灯,最后把所有的灯关闭。
- {
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
- GPIO_ResetBits(GPIOE,GPIO_Pin_8);//串口组, 引脚//打开第一个灯
-
- delay();
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
- GPIO_ResetBits(GPIOE,GPIO_Pin_9);//串口组, 引脚//打开第二个灯
-
- delay();
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
- GPIO_ResetBits(GPIOE,GPIO_Pin_10);//串口组, 引脚//打开第三个灯
- delay();
-
- GPIO_SetBits(GPIOE,GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 );//把灯都关了
- }
3.蜂鸣器模块

蜂鸣器的引脚
beep ---- PB10
代码:
beep.h
- void beepinit(void);//初始化
- void openBeep(void);//打开蜂鸣器
- void closeBeep(void);//关闭蜂鸣器
- void shijianBeep(void);//响一下,停一下
-
-
beep.c
- #include "stm32f4xx.h" //头文件
- #include "led.h"
- #include "beep.h"
- void beepinit(void)
- {
-
- //1.时钟问题解决
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);//
- //2.引脚配置问题解决
- GPIO_InitTypeDef beepstruct;//设置变量
- beepstruct.GPIO_Mode= GPIO_Mode_OUT;//输出模式 //引脚输出信号
- beepstruct.GPIO_OType=GPIO_OType_PP;//外部无上拉电阻选择推挽输出, 外部有上拉电阻选择开漏输出
- beepstruct.GPIO_Pin=GPIO_Pin_10;//引脚的编号是哪个
- beepstruct.GPIO_PuPd=GPIO_PuPd_UP; //上拉电阻
- beepstruct.GPIO_Speed=GPIO_Low_Speed;配置速度
-
- GPIO_Init(GPIOB,&beepstruct);
- //3.初始化
- GPIO_ResetBits(GPIOB,GPIO_Pin_10);//设置输出低电平 ,开始的时候让他不响
- }
-
- void openBeep(void)//打开蜂鸣器
- {
- //GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_SET);//打开蜂鸣器
- GPIO_SetBits(GPIOB,GPIO_Pin_10);//打开蜂鸣器
- }
-
-
- void closeBeep(void)//关闭蜂鸣器
- {
- //GPIO_WriteBit(GPIOB,GPIO_Pin_10,Bit_RESET);//关闭蜂鸣器
- GPIO_ResetBits(GPIOB,GPIO_Pin_10);//设置输出低电平 ,开始的时候让他不响
- }
-
- void shijianBeep(void)//响一下,停一下
- {
- openBeep();
- int a=1;
- while(a--)
- {
- delay();
- }
- closeBeep();
- a=1;
- while(a--)
- {
- delay();
- }
- }
4.key 按键模块

按键的引脚:
key1 ------PE4
key2 ------PE5
key3 -------PE6
key4 -------PC13
代码:
key.h
- void key_init(void);//初始化key
- void key_youxi(void);//key 按键的游戏
key.c
- #include "stm32f4xx.h" //头文件
- #include "led.h"
- #include "beep.h"
- #include "key.h"
-
- void key_init(void)//初始化key
- {
-
- //初始key1 key2 key3
- //1.时钟问题解决
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
- GPIO_InitTypeDef keystruct;//设置变量
- keystruct.GPIO_Mode=GPIO_Mode_IN; //输入模式 //引脚输入信号
- keystruct.GPIO_Pin=GPIO_Pin_4 |GPIO_Pin_5 |GPIO_Pin_6;// 三个按键一起初始化
- //keystruct.GPIO_OType 输出类型不需要
- keystruct.GPIO_PuPd=GPIO_PuPd_UP;需要上拉电阻
- keystruct.GPIO_Speed=GPIO_Low_Speed;//速度
- GPIO_Init(GPIOE,&keystruct);
-
- //初始化
- GPIO_SetBits(GPIOE,GPIO_Pin_4|GPIO_Pin_5 |GPIO_Pin_6);//给这些按键高电平 key1 key2 key3
-
-
- //初始化 key4
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
- GPIO_InitTypeDef keystruct1;//设置变量
- keystruct1.GPIO_Mode=GPIO_Mode_IN; //输入模式 //引脚输入信号
- keystruct1.GPIO_Pin=GPIO_Pin_13;// 按键一起初始化
- //keystruct.GPIO_OType 输出类型不需要
- keystruct1.GPIO_PuPd=GPIO_PuPd_UP;需要上拉电阻
- keystruct1.GPIO_Speed=GPIO_Low_Speed;//速度
- GPIO_Init(GPIOC,&keystruct1);
-
- GPIO_SetBits(GPIOC,GPIO_Pin_13);//给这些按键高电平 key4
-
- }
- void key_youxi(void)//key 按键的游戏
- {
- //PE4 -key1 :打开三个灯
- //PE5 -key2 :关闭三个灯
- //PE6 key3 :打开蜂鸣器+寻个灯
- //PC13 -key4 :关闭蜂鸣器+一+三灯剩一个2灯 key2关掉
- if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4))//这个函数是高电平返回 1 低电平 返回 0
- {
- select_led(1);
- select_led(2);
- select_led(3);
- }
-
- if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5))//这个函数是高电平返回 1 低电平 返回 0
- {
- close_led();//关闭所有的灯
- }
-
- if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_6))//这个函数是高电平返回 1 低电平 返回 0
- {
- openBeep();//打开蜂鸣器
- select_led(1);
- select_led(2);
- select_led(3);
-
- }
-
- if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13))//这个函数是高电平返回 1 低电平 返回 0
- {
- closeBeep();
- close_led();
- select_led(2);
- }
- }