#include "stm32f10x.h"
#include "uart.h"
#define LED GPIO_Pin_8
#define KEY_1_GPIO_CLK RCC_APB2Periph_GPIOA
#define KEY_1_GPIO_PORT GPIOA
#define KEY_1_GPIO_PIN GPIO_Pin_15
#define KEY_1_EXTI_PORTSOURCE GPIO_PortSourceGPIOA
#define KEY_1_EXTI_PINSOURCE GPIO_PinSource15
#define KEY_1_EXTI_LINE EXTI_Line15
#define KEY_1_EXTI_IRQ EXTI15_10_IRQn
#define KEY_2_GPIO_CLK RCC_APB2Periph_GPIOC
#define KEY_2_GPIO_PORT GPIOC
#define KEY_2_GPIO_PIN GPIO_Pin_5
#define KEY_2_EXTI_PORTSOURCE GPIO_PortSourceGPIOC
#define KEY_2_EXTI_PINSOURCE GPIO_PinSource5
#define KEY_2_EXTI_LINE EXTI_Line5
#define KEY_2_EXTI_IRQ EXTI9_5_IRQn
#define KEY_1_IRQHandler EXTI15_10_IRQHandler
#define KEY_2_IRQHandler EXTI9_5_IRQHandler
void delay(void)
{
int i;
int j;
for(i=0;i<100;i++)
{
for(j=0;j<100;j++)
{}
}
}
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// ÆôÓÃGPIO¶ÔÓ¦µÄʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
// ÕÒµ½¶ÔÓ¦Òý½Å
// GPIOA 8
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
// ÅäÖÃΪÊä³öOUTPUT
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
// SPEED
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
// µÍµçƽ
GPIO_ResetBits(GPIOA, GPIO_Pin_8);
// while(1)
//return 0;
}
uint8_t Key_Scan(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==1)
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==1);
return 1;
}
else
{
return 0;
}
}
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure2;
NVIC_InitStructure.NVIC_IRQChannel = KEY_1_EXTI_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure2.NVIC_IRQChannel=KEY_2_EXTI_IRQ;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure2.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure2);
}
void KEY_CPIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure5;
RCC_APB2PeriphClockCmd(KEY_1_GPIO_CLK|KEY_2_GPIO_CLK,ENABLE);
NVIC_Configuration();
GPIO_InitStructure.GPIO_Pin=KEY_1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY_1_GPIO_PORT,&GPIO_InitStructure);
GPIO_EXTILineConfig(KEY_1_EXTI_PORTSOURCE,KEY_1_EXTI_PINSOURCE);
EXTI_InitStructure.EXTI_Line= KEY_1_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_InitStructure.GPIO_Pin=KEY_2_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY_2_GPIO_PORT,&GPIO_InitStructure);
GPIO_EXTILineConfig(KEY_2_EXTI_PORTSOURCE,KEY_2_EXTI_PINSOURCE);
EXTI_InitStructure5.EXTI_Line =KEY_2_EXTI_LINE;
EXTI_InitStructure5.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure5.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure5.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure5);
}
void KEY_2_IRQHandler(void)//pc5
{
delay();
if(EXTI_GetITStatus(KEY_1_EXTI_LINE)!=RESET)
{
GPIO_SetBits(GPIOA,GPIO_Pin_8);
EXTI_ClearITPendingBit(KEY_1_EXTI_LINE);
}
}
void KEY_1_IRQHandler(void)//pa15
{
delay();
if(EXTI_GetITStatus(KEY_2_EXTI_LINE)!=RESET)
{
Usart_SendString( USART1,"key1.2\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
EXTI_ClearITPendingBit(KEY_2_EXTI_LINE);
}
}
int main(void)
{
LED_Config();
KEY_CPIOConfig();
while(1)
{
}
return 0;
}
stm32之用按钮中断点亮led灯
最新推荐文章于 2024-07-21 20:12:42 发布