When I use my understanding of the program and C language test cases after Cheng Fu moment, I found that I write out applications and cannot achieve with routine can achieve the effect, not only that, but I do not know the reason of fault, emerged in the programming bosses hope you kind to help me see where my program went wrong,
Because before has carved a buzzer and entertaining diversions program, so I will be in separate button engineering test routines in the led. The h/c and beep. The h/c file used to directly apply to my own writing project, so the problem should be written out in my own key. The c/h,
Below is the application of standard routine:
Key. H:
# # ifndef __KEY_H
# define __KEY_H
# include "sys. H"
//////////////////////////////////////////////////////////////////////////////////
//only for the use of learning, the program may be used for any other purpose without the permission of the
//ALIENTEK STM32F407 development board
//key input driver code
//the punctual atomic @ ALIENTEK
//technical BBS: www.openedv.com
//created date: 2014/5/3
//version: V1.0
//all rights reserved, piracy will investigate,
//Copyright (C) guangzhou star wing electronic technology co., LTD. 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
# define KEY0 PEin (4)//PE4
# define KEY1 PEin (3)//PE3
//# define KEY2 PEin (2) P32
# define WK_UP PAin (0)//PA0
1//# define KEY0_PRES KEY0 press
2//# define KEY1_PRES KEY1 press
3//# define KEY2_PRES KEY2 press
4//# define WKUP_PRES KEY_UP press (namely WK_UP)
Void KEY_Init (void);//IO initialization
U8 KEY_Scan (u8);//button scanning function
# endif
Key. C:
H # include "is the key."
# include "delay. H"
//////////////////////////////////////////////////////////////////////////////////
//only for the use of learning, the program may be used for any other purpose without the permission of the
//ALIENTEK STM32F407 development board
//key input driver code
//the punctual atomic @ ALIENTEK
//technical BBS: www.openedv.com
//created date: 2014/5/3
//version: V1.0
//all rights reserved, piracy will investigate,
//Copyright (C) guangzhou star wing electronic technology co., LTD. 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//initialization function keys
Void KEY_Init (void)
{
RCC - & gt; AHB1ENR |=1 & lt; <0;//can make PORTA clock
RCC - & gt; AHB1ENR |=1 & lt; <4.//can make PORTE clock
GPIO_Set (GPIOE, PIN2 | responsible | PIN4, GPIO_MODE_IN, 0, 0, GPIO_PUPD_PU); Pull up input//PE2 ~ 4 set
GPIO_Set (GPIOA, PIN0 GPIO_MODE_IN, 0, 0, GPIO_PUPD_PD);//PA0 drop-down as input
}
//button handler
//return key value
//mode: 0, does not support continuous press; 1, support continuous press;
//0, there is no any button press
//1, KEY0 press
//2, KEY1 press
//3, KEY2 press
//4 (KEY_UP press WK_UP
//note that this function has a priority response and KEY0 & gt; KEY1 & gt; KEY2 & gt; KEY_UP!!!!!
U8 KEY_Scan (u8 mode)
{
static u8 key_up=1;//button press release mark
if(mode)key_up=1;//support even press
If (key_up & amp; & (KEY0==0 | | KEY1==0 | | KEY2==0 | | WK_UP==1))
{
delay_ms(10);//to jitter
key_up=0;
If (KEY0==0) return 1;
Else if (KEY1==0) return 2;
Else if (KEY2==0) return 3;
Else if (WK_UP==1) return 4;
} else if (KEY0==1 & amp; & KEY1==1 & amp; & KEY2==1 & amp; & WK_UP==0) key_up=1;
return 0;//no buttons to press
}
The main program: # include "sys. H"
# include "delay. H"
# include "led. H"
H # include "beep."
H # include "is the key."
//ALIENTEK explorer STM32F407 development board experiment 3
//key input experimental
//technical support: www.openedv.com
//guangzhou star wing electronic technology co., LTD.
Int main (void)
{
u8 key;
Stm32_Clock_Init (336,8,2,7);//set the clock, 168 MHZ
Delay_init (168);//delay initialization
LED_Init();//initializes the hardware interface, which is connected with the LED
BEEP_Init ();//initialize the buzzer IO
KEY_Init ();//connect the hardware interface of initialization and buttons
LED0=0;//light red light first
While (1)
{
Key=KEY_Scan (1);//get keys
If (key)
{
The switch (key)
{
Case WKUP_PRES://control buzzer
BEEP=! BEEP.
break;
Case KEY2_PRES://control LED0 flip
LED0=! LED0;
break;
Case KEY1_PRES://control LED1 flip
LED1=! LED1.
break;
Case KEY0_PRES://control LED0 at the same time, turn LED1
LED0=! LED0;
LED1=! LED1.
break;
}
} the else delay_ms (10);
}
}
Then these are the problems of engineering I write your own program:
Key. H:
# # ifndef __KEY_H
# define __KEY_H
# include
# define KEY0 PEin (4)
# define KEY1 PEin (3)
# define KEY2 PEin (2)
# define WK_UP PEin (4)
# define KEY0PRES 1
# define KEY1PRES 2
# define KEY2PRES 3
# define WK_UPPRES 4
Void KEY_Init (void);
U8 KEY_Scan (u8);
# endif
C
key.:
# include
# include
Void KEY_Init (void)
{
RCC - & gt; AHB1ENR=1 & lt; <0;
RCC - & gt; AHB1ENR=1 & lt; <4.
GPIO_Set (GPIOE, PIN2 | responsible | PIN4, GPIO_MODE_IN, GPIO_OTYPE_PP, GPIO_SPEED_100M, GPIO_PUPD_PU);
GPIO_Set (GPIOA, PIN0 GPIO_MODE_IN GPIO_OTYPE_PP, GPIO_SPEED_100M, GPIO_PUPD_PD);
}
U8 KEY_Scan (u8 mode)
{
static u8 key_up=1;
If (mode)
{
Key_up=1;
}
If (key_up & amp; & (KEY0==0 | | KEY1==0 | | KEY2==0 | | WK_UP==1))
{
delay_ms(50);
key_up=0;
If (KEY0==0)
{
return 1;
}
If (KEY1==0)
{
return 2;
}
If (KEY2==0)
{
Return 3;
}
If (WK_UP==1)
{
return 4;
}
}
Else if (KEY0==1 & amp; & KEY1==1 & amp; & KEY2==1 & amp; & WK_UP==0)
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull