stm32 检测到按键按下 灯亮 否则灯不亮
/****************************************************************************
* File Name????????? : key_led.c.c
* Author?????????????? : MCD Application Team
* FW Version????????: V2.0.1
* Date???????????????? ?: 06/11/2012
* Description??????? : This file provides all the RCC firmware functions.
*开发板????????????????? :神舟三号
* 现象??????????????????? :如程序执行前按住按键USER2 LED亮起,没按则灯不亮
*****************************************************************************
*/
?
#include "stm32f10x_lib.h"
/////////////?? define LED?? ///////////////
#define? RCC_GPIO_LED?????????????? RCC_APB2Periph_GPIOF
#define? GPIO_LED???????????????????? GPIOF
#define? GPIO_LED_PIN???????? GPIO_Pin_6
?
/////////////?? define KEY?? ///////////////
#define? RCC_GPIO_KEY ????? RCC_APB2Periph_GPIOD
#define? GPIO_KEY????? GPIOD
#define? GPIO_KEY_PIN??????????????????? GPIO_Pin_3
?
/////////////?? define Beep? ///////////////
#define? RCC_GPIO_BEEP??????????????? RCC_APB2Periph_GPIOB
#define? GPIO_BEEP??????????????????? GPIOB
#define? GPIO_BEEP_PIN??????????????? GPIO_Pin_10
?
/////////////?? config LED?? ///////////////
void LED_Config(void)
{
?GPIO_InitTypeDef GPIO_InitStructure;
//?RCC_ABP2PeriphClockCmd(RCC_GPIO_LED,ENABLE);
?GPIO_InitStructure.GPIO_Pin =? GPIO_LED_PIN;
?GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
?GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
//?GPIO_SetBits(GPIO_LED,GPIO_LED_PIN); //初始化完成才能使用输出功能
?GPIO_Init(GPIO_LED, &GPIO_InitStructure);
?GPIO_SetBits(GPIO_LED,GPIO_LED_PIN);
}
void LED_Turn_on(void)
{
?GPIO_ResetBits(GPIO_LED,GPIO_LED_PIN);
}
void LED_Turn_off(void)
{
?GPIO_SetBits(GPIO_LED,GPIO_LED_PIN);
}
?
?
/////////////?? config KEY?? ///////////////
void KEY_Config(void)
{
?GPIO_InitTypeDef GPIO_InitStructure;
//?RCC_ABP2PeriphClockCmd(RCC_GPIO_KEY,ENABLE);
?GPIO_InitStructure.GPIO_Pin =? GPIO_KEY_PIN;
?GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//需要配置速率
?GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
?GPIO_Init(GPIO_KEY, &GPIO_InitStructure);
}
int Is_KeyDown(void)
{
?if( !GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY_PIN) )
?{
??return ?1 ;
?}
?else
???? return 0? ;
}
?
/////// RccConfig///////
void RccConfiguration(void)
{
? ErrorStatus HSEStartUpStatus;
? /* 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预取址缓存模式
??? /* Flash 2 wait state */
??? FLASH_SetLatency(FLASH_Latency_2);?//设置flash存储器延时时钟周期数
?
??? /* HCLK = SYSCLK */
??? RCC_HCLKConfig(RCC_SYSCLK_Div1);? //设置HCLK相对SYSCLK的分频比例? AHB时钟
?
??? /* PCLK2 = HCLK */
??? RCC_PCLK2Config(RCC_HCLK_Div1);?? //设置PCLK2相对SYSCLK的分频比例? 设置高速AHB时钟
??? /* 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);//启动PLL
??? /* 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)?
??? {
??? }
? }
????
? RCC_APB2PeriphClockCmd(RCC_GPIO_LED,ENABLE);
? RCC_APB2PeriphClockCmd(RCC_GPIO_KEY,ENABLE);
? RCC_APB2PeriphClockCmd(RCC_GPIO_BEEP,ENABLE);
}
?
?
//////// NvicConfig//////////
void NvicConfiguration(void)
{
? NVIC_InitTypeDef NVIC_InitStructure;
#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
}
void ConfigureInterfaces(void)
{?????
??? /* System Clocks Configuration */
??? RccConfiguration();????????? //配置系统时钟???
??????
??? /* NVIC configuration */
??? NvicConfiguration();?? //设置向量入口
}
???????????????????
int main(void)
{
?ConfigureInterfaces();
?LED_Config();
?KEY_Config();
?while(1)
?{
?? ?if(Is_KeyDown()==1)
?? ?{
??????? ?LED_Turn_on();?
?? ?}
? ??else
?? ?{
??????? ?LED_Turn_off();?
?? ?}
?}
}
?