对于stm32的设置首先是对时钟进行启动
要求:key0控制LED0和LED1的亮
key1控制LED0和LED1的亮
kw_up控制闪灯
led.c
#include "led.h"
#include "delay.h"
void Led_Init()
{
GPIO_InitTypeDef GPIO_Initstructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
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_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
}
void Led_flash()
{
for(int i=0;i<5;i++)
{
GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
delay_ms(300);
GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);
delay_ms(300);
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
void Led_Init(void);
void Led_flash(void);
#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);
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_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)
{
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)
#define KEY1 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)
#define WK_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
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_flash();
Key_Init();
while(1)
{
key= Check_key(0);
switch (key)
{
case 1: Led_flash(); break;
case 2: GPIO_ResetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);break;
case 3: GPIO_SetBits(GPIOF,GPIO_Pin_9 | GPIO_Pin_10);break;
default:;break;
}
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)