对于stm32的设置首先是对时钟进行启动
要求:key0控制LED0和LED1的亮
key1控制LED0和LED1的亮
kw_up控制闪灯
led.c
#include "led.h"
#include "delay.h"
/*初始化led所在口的时钟以及一些输入输出的相关设置*/
void Led_Init()
{
GPIO_InitTypeDef GPIO_Initstructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);//使能GPIOF口的时钟
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;//设置输入输出引脚
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_OUT;//设置为输出
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;//输出上拉
GPIO_Initstructure.GPIO_Speed =GPIO_Fast_Speed;//输出速度为高速
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;//输出模式为推挽
GPIO_Init(GPIOF,&GPIO_Initstructure);//GPIO的初始化
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
}
/*****************************************
函数: void Led_flash()
作用: led闪烁
返回值:void
参数:void
作者:马伟
********************************************/
void Led_flash()
{
for(int i=0;i<5;i++)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置高,灯灭
delay_ms(300);
GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);//GPIOF9,F10设置低,灯亮
delay_ms(300);
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
void Led_Init(void);//led初始化函数
void Led_flash(void);//LED闪烁函数
#endif
key.c
#include "key.h"
#include "delay.h"
#include "sys.h"
void Key_Init()
{
GPIO_InitTypeDef GPIO_Initstructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOA,ENABLE);//使能GPIOB和GPIOA口的时钟
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_8;//设置输入输出引脚
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_IN;//设置为输入
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;//输入上拉
GPIO_Initstructure.GPIO_Speed =GPIO_Fast_Speed;//速度为高速
GPIO_Init(GPIOB,&GPIO_Initstructure);//GPIO的初始化
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_0;//设置输入输入引脚
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_IN;//设置为输入
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_DOWN;//输入下拉
GPIO_Initstructure.GPIO_Speed =GPIO_Fast_Speed;//速度为高速
GPIO_Init(GPIOA,&GPIO_Initstructure);
}
/*****************************************
函数: int Check_key( int mode)
作用: 按键检测
返回值:int
参数:int
作者:马伟
********************************************/
int Check_key( int mode)
{
static int key_mark=1 ;
if(mode==1) key_mark=1;
if(key_mark&&(KEY0==0||KEY1==0||WK_UP==1))
{
delay_ms(10);
key_mark=0;
if(KEY0==0) return 1;
else if(KEY1==0) return 2;
else if(WK_UP==1) return 3;
}
else if(KEY0==1&&KEY1==1&&WK_UP==0) key_mark=1 ;
return 0;
}
key.h
#ifndef __KEY_H
#define __KEY_H
#include "key.h"
#include "sys.h"
#define KEY0 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9) //PE4
#define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8) //PE3
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) //PA0
int Check_key( int mode);
void Key_Init(void);
#endif
main.c
#include "stm32f4xx.h"
#include "key.h"
#include "delay.h"
#include "led.h"
#include "sys.h"
int main(void)
{
int key;
delay_init(168); //延时初始化
Led_Init();//led初始化函数
Led_flash();//LED闪烁函数
Key_Init();
while(1)
{
key= Check_key(0);
switch (key)
{
case 1: Led_flash(); break;//LED闪烁函数
case 2: GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);break;//GPIOF9,F10设置高,灯灭
case 3: GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);break;//GPIOF9,F10设置高,灯灭
default:;break;
}
}
return 0;
}