#include "key.h"
#include "led.h"
u8 led_stuta = 0;
void KEY_GPIO_init(void)//3õê¼»ˉ1ü½Å
{
/*********¶¨òåò»¸öGPIO_InitTypeDef ààDíμĽá11ìå**********/
GPIO_InitTypeDef GPIO_InitStructure;
/*********Ñ¡Ôñòa¿ØÖÆμÄGPIOxμÄòy½Å**********/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
/*********éèÖÃòy½ÅËùÂêÎa50MHZ**********/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*********éèÖÃòy½ÅÄ£ê½Îaí¨óÃíÆíêêä3ö**********/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
/*′ò¿aLEDê1óÃμÄGPIOμÄê±Öóê1Äü*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
/*3õê¼»ˉÏàó|μÄGPIO*/
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void EXTI_init(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI9_5_IRQHandler(void)
{
if(EXTI_GetFlagStatus(EXTI_Line6)!=RESET)
{
GPIO_SetBits(GPIOB, GPIO_Pin_8);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
}
EXTI_ClearITPendingBit(EXTI_Line6);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
// 按键输入实验
// -------------------------
#include "stm32f10x.h"
#include "stm32f10x_it.h"
void LED_Config();
void KEY_Config();
void NVIC_Config();
int main()
{
NVIC_Config();
LED_Config();
KEY_Config();
GPIO_ResetBits(GPIOC,GPIO_Pin_13);
while(1)
{
}
}
void LED_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
void KEY_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler()
{
if(EXTI_GetITStatus(EXTI_Line0)!= RESET)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_13,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_13)));
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
注:我用的开发板,芯片是STM32F103C8T6,按键两个,一个是RST,另一个是控制按钮,对应管脚PA0;LED两个,一个是电源指示灯D1,另一个D2可编程,对应管脚PC13。
一周热门 更多>