按键控制LED灯
0.关于.c文件与.h文件
0.1c文件的作用
.c文件是C语言源代码文件,包含程序的实际功能实现。
主要存放函数定义、全局变量定义、初始化代码等可执行内容。
编译时.c文件会被转换为目标文件(.obj),最终链接为可执行程序。
0.2.h文件的作用
.h文件是头文件,主要用于声明接口和共享信息。
包含函数原型、宏定义、类型定义(如结构体)、外部变量声明等内容。
头文件不包含实际代码实现,仅提供模块间的交互规范。
.h文件通过#include被其他文件引用,确保声明的一致性。良好的头文件设计能降低耦合度,例如通过#ifndef防止重复包含,头文件也是多文件项目管理的关键,便于团队协作和模块复用。
两者协作关系
.c文件通过包含对应的.h文件获取外部声明,避免手动重复编写。
例如main.c引用module.h后,可直接调用module.c中实现的函数。
编译时,每个.c文件独立编译为目标文件,再通过链接器合并。
头文件不参与单独编译,仅作为.c文件的辅助参考。
若.h文件修改,所有包含它的.c文件需重新编译以保证一致性。
1.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_1 |GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
//调用这个函数,可以返回按键的键码
uint8_t Key_GetNum(void){
//按键键码默认给0,如果没有按下,就返回0
uint8_t KeyNum = 0;
//读取PB1端口的值
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) ==0 )
{
Delay_ms(200);
while(GPIOB,GPIO_Pin_1 ==0);
Delay_ms(200);
//返回键值的键码为1
KeyNum = 1;
}
//读取GPIO端口
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11) ==0 )
{
Delay_ms(200);
while(GPIOB,GPIO_Pin_11 ==0);
Delay_ms(200);
//返回键值的键码为1
KeyNum = 2;
}
return KeyNum;
}
2.Key.h按键
#ifndef _Key_H
#define _Key_H
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
3.LED.c灯
//封装LED的驱动程序
#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);
GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}
void LED1_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED1_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
//实现翻转功能
void LED1_Turn(void){
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1) == 0){
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
else
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED2_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
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);
}
4.LED.h灯
#ifndef _LED_H
#define _LED_H
//存放驱动程序可以对外提供的函数或变量声明
//文件必须以空行来结尾
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);
#endif
5.main.c
#include "stm32f10x.h" // Device header
#include "Delay.h" //使用延时函数
#include "LED.h"
#include "Key.h"
//定义全局化变量,用于存储键码的返回值
uint8_t KeyNum;
int main(void)
{
LED_Init();
//初始化按键
Key_Init();
while(1)
{
//LED1_ON();
//Delay_ms(200);
//LED1_OFF();
//Delay_ms(500);
//LED2_ON();
//Delay_ms(500);
//LED2_OFF();
//Delay_ms(500);
//不断读取变量值,放在KeyNum里面
KeyNum = Key_GetNum();
if(KeyNum == 1){
//LED1_ON();
LED1_Turn();
Delay_ms(200);
}
if(KeyNum == 2){
//LED1_OFF();
LED2_Turn();
Delay_ms(200);
}
}
}