模块化编程,将每个硬件的驱动函数单独提取出来,封装在 .c 和 .h 的文件里,有利简化主函数的逻辑,硬件驱动提取出来有利于移植程序
.c 文件为源文件,包含程序代码
.h 文件头文件,包含程序中的函数、变量和类型声明
头文件提供接口给其他源文件共享代码,只需要在源文件引入头文件标签就可以使用开放的接口函数
#include "stm32f10x.h" // Device header
void LED_Init(void)
{
// 打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
// 定义结构体变量
GPIO_InitTypeDef GPIO_Initstructure;
// 设置引脚编号,Pin0~Pin_All
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
// 设置输出速度 最大 50MHZ
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
// 设置推挽输出
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_Out_PP;
// 初始化 GPIOA.0
GPIO_Init(GPIOA,&GPIO_Initstructure);
// 输出高电平使输出关闭
GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}
// 构造LED1亮的函数
void LED1_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
// 构造LED1灭的函数
void LED1_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
// 构造LED1按键取反的函数
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);
}
}
// 构造LED2亮的函数
void LED2_ON(void)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
// 构造LED2灭的函数
void LED2_OFF(void)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
// 构造LED2按键取反的函数
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 __LED_H
#define __LED_H
void 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
#include "Delay.h"
void Key_Init(void)
{
// 打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
// 定义结构体变量
GPIO_InitTypeDef GPIO_Initstructure;
// 设置引脚编号,Pin0~Pin_All
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
// 设置输出速度 最大 50MHZ
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
// 设置推挽输出
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_IPU;
// 初始化 GPIOA.0
GPIO_Init(GPIOB,&GPIO_Initstructure);
}
// 构造读取输入的函数
uint8_t Key_GetNum(void)
{
// 默认返回 0
uint8_t KeyNum = 0;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
{
// 消抖
Delay_ms(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
Delay_ms(20);
KeyNum = 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;
}
#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
#include "stm32f10x.h" // Device header
void LDR_Init(void)
{
// 打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
// 定义结构体变量
GPIO_InitTypeDef GPIO_Initstructure;
// 设置引脚编号,Pin0~Pin_All
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_13;
// 设置输出速度 最大 50MHZ
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
// 设置推挽输出
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_IPU;
// 初始化 GPIOA.0
GPIO_Init(GPIOB,&GPIO_Initstructure);
}
uint8_t LDR_ON(void)
{
// 直接返回函数的值
return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);
}
#ifndef __LDR_H
#define __LDR_H
void LDR_Init(void);
uint8_t LDR_ON(void);
#endif
#include "stm32f10x.h" // Device header
void Buzzer_Init(void)
{
// 打开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
// 定义结构体变量
GPIO_InitTypeDef GPIO_Initstructure;
// 设置引脚编号,Pin0~Pin_All
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_12;
// 设置输出速度 最大 50MHZ
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
// 设置推挽输出
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_Out_PP;
// 初始化 GPIOA.0
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);
}
#ifndef __Buzzer_H
#define __Buzzer_H
void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
#endif
调用函数到主函数
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "LDR.h"
uint8_t KeyNum;
int main(void)
{
LED_Init();
Key_Init();
Buzzer_Init();
LDR_Init();
while(1)
{
KeyNum = Key_GetNum();
// 不断扫描判断按钮状态
if(KeyNum == 1)
{
// 如果按钮等于1就打开灯反之关闭
LED1_Turn();
}
// 不断扫描判断按钮状态
if(KeyNum == 2)
{
// 如果按钮等于2就打开灯反之关闭
LED2_Turn();
}
// 不断扫描判断光敏模块状态
if(LDR_ON() != 1)
{
// 如果光敏模块不等于1就启用蜂鸣器
Buzzer_ON();
LED1_Turn();
}
else
{
//// 如果光敏模块等于1就禁用蜂鸣器
LED1_OFF();
Buzzer_OFF();
LED2_Turn();
}
/*LED1_ON();
LED2_OFF();
Delay_ms(500);
LED1_OFF();
LED2_ON();
Delay_ms(500);*/
}
}