这是小弟用库函数写的按键输入,实现的功能与不完全手册中的按键输入功能相同,我用的是Keil4.14,现在遇到的问题是松手检测出现问题,如果不加松手检测,消抖效果不好,但是加了程序又出错,具体表现为:按下第一个键有反应,之后的按键全部失灵。可是我检查不出松手检测的代码有神马问题,求原子哥帮小弟检查一下有木有问题呀(用了两种松手检测的代码都不行啊都不行,凌乱中~~~,可是不用库函数,用Keil3.8直接操作寄存器写又是能正常工作的,之前有发帖请教过原子大神,后来解决了,现在如法炮制居然不行了,彻底凌乱了。。。)代码如下: fficeffice" />
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : Vffice:smarttags" />2.0.1
* Date : 06/13/2008
* Description : Main program body.
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void Delay_Ms(u16 time);
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
u8 KEY0,KEY1,KEY_WAK;
u8 i;//(用于松手检测,方法一)
u16 j;//(用于松手检测,方法二)
u8 LED0,LED1;
/* System Clocks Configuration **********************************************/
RCC_Configuration();
/* NVIC Configuration *******************************************************/
NVIC_Configuration();
/* GPIO Configuration *******************************************************/
GPIO_Configuration();
while (1)
{
LED0 = GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_8);
LED1 = GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_2);
KEY0 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_13);
KEY1 = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15);
KEY_WAK = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
i=1;//(用于松手检测,方法一)
if(KEY0==0||KEY1==0||KEY_WAK==1)
{
Delay_Ms(10);
if(KEY0==0||KEY1==0||KEY_WAK==1)
{
if(KEY0 == 0)
{
if(LED0 == 0)
GPIO_SetBits(GPIOA, GPIO_Pin_8);//PA8 DS0 点亮
else
GPIO_ResetBits(GPIOA, GPIO_Pin_8);//PA8 DS0 熄灭
}
if(KEY1 == 0)
{
if(LED1 == 0)
GPIO_SetBits(GPIOD, GPIO_Pin_2);//PD2 DS1 点亮
else
GPIO_ResetBits(GPIOD, GPIO_Pin_2);//PD2 DS1 熄灭
}
if(KEY_WAK == 1)
{
if(LED0 == 0)
GPIO_SetBits(GPIOA, GPIO_Pin_8);//PA8 DS0 点亮
else
GPIO_ResetBits(GPIOA, GPIO_Pin_8);//PA8 DS0 熄灭
if(LED1 == 0)
GPIO_SetBits(GPIOD, GPIO_Pin_2);//PD2 DS1 点亮
else
GPIO_ResetBits(GPIOD, GPIO_Pin_2);//PD2 DS1 熄灭
}
/******************************松手检测,方法一**********************************/
while(i) //(用于松手检测,方法一)
{ //(用于松手检测,方法一)
if(KEY0==1&&KEY1==1&&KEY_WAK==0) //(用于松手检测,方法一)
i=0; //(用于松手检测,方法一)
} //(用于松手检测,方法一)
/*****************************************************************************/
/*----------------------------松手检测,方法二----------------------------------
或者把上面第一种松手检测的方法注销掉,用第二种松手检测的方法,代码如下:
for(j=0;j<10000;j++)
{
Delay_Ms(10);
if(KEY0==1&&KEY1==1&&KEY_WAK==0)
break;
}
------------------------------------------------------------------------------*/
}
}
else
Delay_Ms(10);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
/*------------------------------------------------------------------------------
Configure all unused GPIO port pins in Analog Input mode (floating input
trigger OFF), this will reduce the power consumption and increase the device
immunity against EMI/EMC(举例说明)
------------------------------------------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_Init(GPIOE, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, DISABLE);
#ifdef USE_STM3210E_EVAL
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_Init(GPIOG, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, DISABLE);
#endif /* USE_STM3210E_EVAL */
/*------------------------------------------------------------------------------
Configure IO connected to DS0 and DS1 leds
------------------------------------------------------------------------------*/
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //PA8 推挽输出 DS0
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure); //PD2 推挽输出 DS1
/*------------------------------------------------------------------------------
Configure IO connected to KEY0 、KEY1 and KEY_WAK
------------------------------------------------------------------------------*/
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //PA13 上拉输入 KEY0
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //PA15 上拉输入 KEY1
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //PA0 下来输入 KEY_WAK
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
* Function Name : Delay_Ms
* Description : delay 1 ms.
* Input : time (ms)
* Output : None
* Return : None
*******************************************************************************/
void Delay_Ms(u16 time) //延时函数
{
u16 i,j;
for(i=0;i<time;i++)
for(j=0;j<10260;j++)
;
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ |