标签:
0.STM32F103上测试
#include <stm32f10x.h> void GPIO_Config(void); void NVIC_Config(void); void EXTI_Config(void); int main(void) { GPIO_Config(); NVIC_Config(); EXTI_Config(); while(1); } void GPIO_Config(void) { /*定义一个GPIO_InitTypeDef类型的结构体,本类型在stm32f10x_gpio.h中定义*/ GPIO_InitTypeDef GPIO_InitStructure; /*使能GPIOC和GPIOD时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG|RCC_APB2Periph_GPIOD, ENABLE); /*配置PC9管教作为推挽输出*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOG, &GPIO_InitStructure); /*配置PD3作为浮空输入,为外部中断做准备*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); } void EXTI_Config(void) { /*定义一个EXTI_InitTypeDef类型的结构体,该类型在stm32f10x_exti.h中定义*/ EXTI_InitTypeDef EXTI_InitStructure; /*使能AFIO时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); /*配置PD3管教作为EXTI线3*/ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource3); /*在EXTI中配置EXTI线3为中断模式,下降沿有效,并使能该配置*/ EXTI_InitStructure.EXTI_Line = EXTI_Line3; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void NVIC_Config(void) { /*定义一个NVIC_InitTypeDef类型的结构体,该类型在misc.h中定义*/ NVIC_InitTypeDef NVIC_InitStructure; /*在NVIC中配置中断通道为EXTI3,抢占优先级为7,从优先级为7,并使能该中断*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x07; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x07; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /*声明变量LED*/ uint8_t led = 1; /*EXTI3中断服务函数*/ void EXTI3_IRQHandler(void) { /*检测EXTI3是否有效*/ if(EXTI_GetITStatus(EXTI_Line3) == SET) { /*状态翻转*/ led = ~led; /*如果等于1则PC9置位,否则清零*/ if(led == 1) { GPIO_SetBits(GPIOG, GPIO_Pin_14); } else { GPIO_ResetBits(GPIOG, GPIO_Pin_14); } /*清除EXTI3的悬起标志位*/ EXTI_ClearITPendingBit(EXTI_Line3); } }
标签:
原文地址:http://www.cnblogs.com/nekorz/p/5380618.html