本帖最后由 tim4146 于 2015-10-31 08:29 编辑
-
复制代码- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //关闭jtag
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //设为下拉输入
- GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化GPIO
-
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource5);
- EXTI_InitStructure.EXTI_Line=EXTI_Line5;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
- //GPIOD.5 中断线以及中断初始化配置 下降沿触发
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,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); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
- //GPIOD.5 中断线以及中断初始化配置 下降沿触发
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource7);
- EXTI_InitStructure.EXTI_Line=EXTI_Line7;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //使能按键WK_UP所在的外部中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure);
- }
复制代码
中断处理:
- //外部中断5服务程序 //超声波 6 7 8
- void EXTI9_5_IRQnHandler(void)
- {
- //bug警告 此处放入delay_us()函数将导致异常
- if(EXTI_GetITStatus(EXTI_Line5) !=RESET)
- {
- printf("123
");
- EXTI_ClearITPendingBit(EXTI_Line5); //清除LINE5上的中断标志位
- }
- if(EXTI_GetITStatus(EXTI_Line6) !=RESET)
- {
- // if(flag)//如果flag=1,说明外部中断是有效的
- // {
- // TIM_Cmd(TIM2, DISABLE); //定时器2失能
- // UltrasonicWave_Distance=(float)TIM_GetCounter(TIM2)*0.34/2; //计算距离
- // TIM_SetCounter(TIM2,0);
- // printf("Distance7:%d mm
",UltrasonicWave_Distance);
- // }
- // flag=0;//回到就绪状态
- EXTI_ClearITPendingBit(EXTI_Line6); //清除LINE6上的中断标志位
- }
- if(EXTI_GetITStatus(EXTI_Line7) !=RESET)
- {
- EXTI_ClearITPendingBit(EXTI_Line7); //清除LINE7上的中断标志位
- }
- }
复制代码
main函数
- #include "delay.h"
- #include "sys.h"
- #include "key.h"
- #include "beep.h"
- #include "usart.h"
- #include "exti.h"
- #include "UltrasonicWave.h"
- int main(void)
- {
- SystemInit();
- delay_init(72); //延时初始化
- NVIC_Configuration();
- uart_init(9600); //串口初始化
- BEEP_GPIO_Config();
- UltrasonicWave_Configuration(); //对超声波模块初始化
- while(1)
- {
- BEEP=1;
- delay_ms(500);
- BEEP=0;
- delay_ms(500);
- printf("OK
");
- }
- }
复制代码
小刘的奇怪stm32问题.rar
(5.33 MB, 下载次数: 11)
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>