/****************************************************************************
|
* Copyright: (C)2018 Î人ÁèÔÆÎïÍøÖÇ¿ÆÊµÑéÊÒ www.iot-yun.com
|
* Author: GuoWenxue<guowenxue@gmail.com> QQ: 281143292
|
* Description: STM32L151C8T6 CubeMX ¿ª·¢°åÖ»ÓÐÒ»¸ö°´¼ü°´Å¥Á¬µ½PA0,ÁíÍâÒ»¸öÊǸ´Î»°´¼ü¡£
|
*
|
* ChangeLog:
|
* °æ±¾ºÅ ÈÕÆÚ ×÷Õß ËµÃ÷
|
* V1.0.0 2018.11.05 GuoWenxue ·¢²¼¸Ã°æ±¾
|
****************************************************************************/
|
#include <stdio.h>
|
#include "stm32l1xx.h"
|
|
void init_key1_gpio_interrupt(void);
|
|
/* º¯Êý˵Ã÷: ÅäÖÃËùÓеİ´¼üµÄ GPIO¿Ú¡¢Ê±ÖÓºÍÖÐ¶Ï */
|
void init_keys_interrupt(void)
|
{
|
/* STM32L151C8T6 CubeMX ¿ª·¢°åÖ»ÓÐÒ»¸ö°´¼ü°´Å¥Á¬µ½PA0ÉÏ */
|
init_key1_gpio_interrupt();
|
}
|
|
|
void init_key1_gpio_interrupt(void)
|
{
|
GPIO_InitTypeDef GPIO_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
EXTI_InitTypeDef EXTI_InitStructure;
|
|
/* ʹÄÜGPIOAµÄʱÖÓ */
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
|
/* ³õʼ»¯ÖжÏÓÅÏȼ¶ */
|
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
|
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
NVIC_Init(&NVIC_InitStructure);
|
|
/* °´¼üÉ϶Ëͨ¹ýÉÏÀµç×èÁ¬µ½VCCÉÏ, ËùÒÔÉèÖÃÖжÏΪÉÏÉýÑØ´¥·¢ */
|
EXTI_DeInit();
|
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
|
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
EXTI_Init(&EXTI_InitStructure);
|
|
/* ĬÈϽ«GPIO¿ÚÅäÖóÉÏÂÀµÍµçƽģʽ, ÕâÑùµ±°´¼ü°´Ïº󽫻á±ä³ÉVCC²úÉúÉÏÉýÑØ */
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
}
|
|
|
/* Ïû³ý°´¼ü¶¶¶¯ÑÓʱº¯Êý */
|
static __inline void key_dejitter_delay(void)
|
{
|
uint32_t times=6000;
|
while( times--) ;
|
}
|
|
|
/* µ±°´¼ü°´ÏÂʱ»á²úÉúÖжϣ¬CPUÌø×ªµ½startup_stm32l1xx_md.sÖвéѯÖжÏÏòÁ¿±í,²¢µ÷ÓÃÏàÓ¦µÄÖжϷþÎñ´¦Àí³EXTI0_IRQHandler() */
|
void EXTI0_IRQHandler(void)
|
{
|
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
|
{
|
/* È¥¶¶£º ÑÓʱһ¶Îʱ¼ä²é¿´GPIOµÄ״̬£¬Èç¹û»¹ÊÇµÍµçÆ½ËµÃ÷ÊÇÓÐЧ°´¼ü */
|
key_dejitter_delay();
|
if( Bit_SET == GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) )
|
{
|
printf("Detect Key Pressed and turn LED ON/off.\n");
|
GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_1))));
|
EXTI_ClearITPendingBit(EXTI_Line0);
|
}
|
}
|
}
|