我们要实现按键控制led,我们需要完成LED和按键驱动代码,但如果把这两部分代码都混在主函数里面,那么代码显得过于杂乱,不容易管理和移植,所以对于这种驱动代码,我们一般采用模块化编程,把它封装起来,单独放在另外的.C和.h文件里。
//用来存放这个驱动程序可以对外提供的函数或变量声明
#ifndef __LED_H//统一模式
#define __LED_Hvoid LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED1_Turn(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED2_Turn(void);#endif
#include "stm32f10x.h" // Device header
//存放点灯主体代码
//LED初始化
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_1 | GPIO_Pin_2;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);//不操作LED LED熄灭GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);
}
//点亮LED1
void LED1_ON(void)
{GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
//熄灭LED1void LED1_OFF(void)
{GPIO_SetBits(GPIOA, GPIO_Pin_1);
}void LED1_Turn(void)//端口电平反转
{if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)//读取当前端口的输出状态,如果当前输出为0,那么置1{GPIO_SetBits(GPIOA, GPIO_Pin_1);}else{GPIO_ResetBits(GPIOA, GPIO_Pin_1);//否则就置0}
}
//点亮LED2
void LED2_ON(void)
{GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
//熄灭LED2
void LED2_OFF(void)
{GPIO_SetBits(GPIOA, GPIO_Pin_2);
}void LED2_Turn(void)
{if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0){GPIO_SetBits(GPIOA, GPIO_Pin_2);}else{GPIO_ResetBits(GPIOA, GPIO_Pin_2);}
}
#ifndef __KEY_H
#define __KEY_Hvoid Key_Init(void);
uint8_t Key_GetNum(void);#endif
#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_1 | GPIO_Pin_11;//按键接在PB1和PB11口上GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//读取按键函数
uint8_t Key_GetNum(void)
{uint8_t KeyNum = 0;if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)//如果读取PB1端口值==0,代表按下按键{Delay_ms(20);//按键消抖while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);//检测按键是否松手Delay_ms(20);KeyNum = 1;//传递出键码1}if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0){Delay_ms(20);while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);Delay_ms(20);KeyNum = 2;}return KeyNum;
}
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
uint8_t KeyNum;int main(void)
{LED_Init();//LED初始化Key_Init();while (1){KeyNum = Key_GetNum();if (KeyNum == 1)//按键1按下{LED1_Turn();}if (KeyNum == 2){LED2_Turn();}}
}
蜂鸣器:VCC GND接电源,控制脚为12号口。
光敏传感器:VCC GND 接电源,DO数字输出端,接P13号口。。
当我们遮蔽光线时,输出指示灯灭,输出高电平,此时蜂鸣器发出声响。接触光线时,输出指示灯亮,输出低电平,蜂鸣器不响。
#ifndef __BUZZER_H
#define __BUZZER_Hvoid Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_Turn(void);#endif
#include "stm32f10x.h" // Device headervoid Buzzer_Init(void)
{RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//P12口GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
//蜂鸣器打开
void Buzzer_ON(void)
{GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
//蜂鸣器关闭
void Buzzer_OFF(void)
{GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
//如果蜂鸣器时打开的,那么将关闭蜂鸣器,如果蜂鸣器关闭,那么将打开蜂鸣器
void Buzzer_Turn(void)
{if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0){GPIO_SetBits(GPIOB, GPIO_Pin_12);}else{GPIO_ResetBits(GPIOB, GPIO_Pin_12);}
}
#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_Hvoid LightSensor_Init(void);
uint8_t LightSensor_Get(void);#endif
#include "stm32f10x.h" // Device headervoid LightSensor_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_13;//P13口GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStructure);
}uint8_t LightSensor_Get(void)
{return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);//读取输入寄存器13号端口的输入值
}
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "Buzzer.h"
#include "LightSensor.h"int main(void)
{Buzzer_Init();LightSensor_Init();while (1){if (LightSensor_Get() == 1)//遮蔽光线{Buzzer_ON();//蜂鸣器鸣叫}else{Buzzer_OFF();//蜂鸣器关闭}}
}
① 初始化时钟
② 定义结构体
③ 赋值结构体
④ 使用GPIO_Init函数,将指定的GPIO外设初始化好