//Ƭͷļ
#include "stm32f10x.h"

//Ӳ
#include "key.h"
#include "delay.h"
#include "led.h"

/*
************************************************************
*	ƣ	Key_Init
*
*	ܣ	Keyʼ
*
*	ڲ	
*
*	ز	
*
*	˵		
************************************************************
*/

void Key_Init(void)
{
    GPIO_InitTypeDef gpio_initstruct;
    EXTI_InitTypeDef exti_initstruct;
    NVIC_InitTypeDef nvic_initstruct;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);     //GPIOAʱ
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    
    gpio_initstruct.GPIO_Mode = GPIO_Mode_IPU;                //Ϊģʽ
    gpio_initstruct.GPIO_Pin = GPIO_Pin_1;                    //ʼPinΪGPIO_Pin_1PA1
    gpio_initstruct.GPIO_Speed = GPIO_Speed_50MHz;            //IOڵٶΪ50MHz
    GPIO_Init(GPIOA, &gpio_initstruct);                       //ʼGPIOA
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);    //GPIOAPin1ӵⲿж1
    
    exti_initstruct.EXTI_Line = EXTI_Line1;                   //ѡⲿж1
    exti_initstruct.EXTI_Mode = EXTI_Mode_Interrupt;          //Ϊжģʽ
    exti_initstruct.EXTI_Trigger = EXTI_Trigger_Falling;      //ʽΪ½
    exti_initstruct.EXTI_LineCmd = ENABLE;                    //ʹⲿж1
    EXTI_Init(&exti_initstruct);                              //ʼⲿж
    
    nvic_initstruct.NVIC_IRQChannel = EXTI1_IRQn;             //ⲿж1жͨ
    nvic_initstruct.NVIC_IRQChannelPreemptionPriority = 2;    //ռȼΪ2
    nvic_initstruct.NVIC_IRQChannelSubPriority = 2;           //ȼΪ2
    nvic_initstruct.NVIC_IRQChannelCmd = ENABLE;              //ʹⲿж1ж
    NVIC_Init(&nvic_initstruct);                              //ʼⲿжж
}

void EXTI1_IRQHandler(void)
{
    DelayXms(10);   //ʱ10ms
    if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0)  //PA1Ϊ͵ƽ£
    {
        if(led_info.Led_Status == LED_ON)
            Led_Set(LED_OFF);   //LED״̬ΪLEDر
        else
            Led_Set(LED_ON);    //LED״̬ΪأLED
    }
    EXTI_ClearITPendingBit(EXTI_Line1);  //ⲿж1жϱ־λ
}

