main.c
#include "led.h"
#include "key.h"
#include "beep.h"
#include "delay.h"
int main()
{
u8 key;
delay_init();
LED_Init();
KEY_Init();
BEEP_Init();
while(1)
{
key = KEY_Scan(0);
if (key)
{
switch(key)
{
case WKUP_PRES:
BEEP = !BEEP;
break;
case KEY1_PRES:
LED1 = !LED1;
break;
case KEY0_PRES:
LED0 = !LED0;
break;
}
}
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
#define LED0 PBout(5)
#define LED1 PEout(5)
void LED_Init(void);
#endif
led.c
#include "led.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
// LED0-->PB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_5); // PB.5 输出高电平;
// LED1-->PE.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_5); // PE.5 输出高电平;
}
beep.h
#ifndef __BEEP_H
#define __BEEP_H
#include "sys.h"
#define BEEP PBout(8)
void BEEP_Init(void);
#endif
beep.c
#include "beep.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
void BEEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// BEEP-->GPIOB.8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_8); // 输出低电平;
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "sys.h"
#include "stm32f10x_gpio.h"
//#define KEY0 PEin(4)
//#define KEY1 PEin(3)
//#define WK_UP PAin(0)
#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4) // 读取按键0;
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3) // 读取按键1;
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)// 读取按键3;
#define KEY0_PRES 1 // KEY0按下
#define KEY1_PRES 2 // KEY1按下
#define WKUP_PRES 3 // KEY_UP按下
void KEY_Init(void);
u8 KEY_Scan(u8); // 按键扫描函数
#endif
key.c
#include "key.h"
#include "stm32f10x_rcc.h"
#include "delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
// KEY_UP-->PA.0 默认输出低电平;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; // PA.0 下拉输入;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// KYE0 KEY1-->PE.4 PE.3 默认输出高电平;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // PE.4 上拉输入;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
static u8 key_up = 1; // 按键松开标志;
// if (mode) key_up = 1; // 无论按键是否松开,都认为是松开;
// 注意响应优先级 KEY0 > KEY1 > WK_UP;
if (key_up && (KEY0 == 0 || KEY1 == 0 || WK_UP == 1))
{
delay_ms(10); // 去抖动;
key_up = 0;
if (WK_UP == 1)
return WKUP_PRES;
else if (KEY0 == 0)
return KEY0_PRES;
else if (KEY1 == 0)
return KEY1_PRES;
}
else if (KEY0==1 && KEY1==1 && WK_UP==0)
{
key_up = 1;
}
return 0; // 无按键按下;
}
编译时发现了错误
原因是在宏定义的末尾加了分号,去掉分号就可以编译通过了。