当前位置:   article > 正文

STM32学习历程(day6)

STM32学习历程(day6)

EXTI外部中断使用教程

首先先看下EXTI的框图

看这个框图就能知道要先初始化GPIO外设 那么和前面一样

1、先RCC使能时钟

2、配置GPIO 选择端口为输入模式, 

3、配置AFIO,选择我们用的GPIO连接到后面的EXTI

4、配置EXTI,选择边沿触发方式 eg:上升沿 下降沿 双边沿。选择触发事件或者中断

5、配置NVIC,给中断配置优先级

AFIO函数

void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);

函数可以用来引脚重映射,参数1是要选择的重映射的方式,参数2是新的状态

void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);

这个函数可以配置AFIO的数据选择器,来选择我们想要的中断引脚

EXTI配置

可以去exti的头文件的最下面 查看有什么函数 我们这里只用到了exti的初始化函数

  1. void EXTI_DeInit(void);
  2. void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
  3. void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct);
  4. void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line);
  5. FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line);
  6. void EXTI_ClearFlag(uint32_t EXTI_Line);
  7. ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);
  8. void EXTI_ClearITPendingBit(uint32_t EXTI_Line);

 配置示范

  1. //配置EXTI 这里配置成上升沿触发中断,引脚为14
  2. EXTI_InitTypeDef EXTI_InitStruct;
  3. EXTI_InitStruct.EXTI_Line = EXTI_Line14;
  4. EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  5. EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  6. EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
  7. EXTI_Init(&EXTI_InitStruct);

NVIC配置 

nvic的函数定义在misc.h里面

  1. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
  2. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
  3. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset);
  4. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState);
  5. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource);

配置示范 

  1. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  2. NVIC_InitTypeDef NVIC_InitStruct;
  3. NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
  4. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  5. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
  6. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  7. NVIC_Init(&NVIC_InitStruct);

中断函数

在Start\startup_stm32f10x_md.s里有汇编代码 能查看到每个模式下的中断函数 

  1. ; External Interrupts
  2. DCD WWDG_IRQHandler ; Window Watchdog
  3. DCD PVD_IRQHandler ; PVD through EXTI Line detect
  4. DCD TAMPER_IRQHandler ; Tamper
  5. DCD RTC_IRQHandler ; RTC
  6. DCD FLASH_IRQHandler ; Flash
  7. DCD RCC_IRQHandler ; RCC
  8. DCD EXTI0_IRQHandler ; EXTI Line 0
  9. DCD EXTI1_IRQHandler ; EXTI Line 1
  10. DCD EXTI2_IRQHandler ; EXTI Line 2
  11. DCD EXTI3_IRQHandler ; EXTI Line 3
  12. DCD EXTI4_IRQHandler ; EXTI Line 4
  13. DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
  14. DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
  15. DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
  16. DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
  17. DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
  18. DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
  19. DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
  20. DCD ADC1_2_IRQHandler ; ADC1_2
  21. DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX
  22. DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0
  23. DCD CAN1_RX1_IRQHandler ; CAN1 RX1
  24. DCD CAN1_SCE_IRQHandler ; CAN1 SCE
  25. DCD EXTI9_5_IRQHandler ; EXTI Line 9..5
  26. DCD TIM1_BRK_IRQHandler ; TIM1 Break
  27. DCD TIM1_UP_IRQHandler ; TIM1 Update
  28. DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
  29. DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
  30. DCD TIM2_IRQHandler ; TIM2
  31. DCD TIM3_IRQHandler ; TIM3
  32. DCD TIM4_IRQHandler ; TIM4
  33. DCD I2C1_EV_IRQHandler ; I2C1 Event
  34. DCD I2C1_ER_IRQHandler ; I2C1 Error
  35. DCD I2C2_EV_IRQHandler ; I2C2 Event
  36. DCD I2C2_ER_IRQHandler ; I2C2 Error
  37. DCD SPI1_IRQHandler ; SPI1
  38. DCD SPI2_IRQHandler ; SPI2
  39. DCD USART1_IRQHandler ; USART1
  40. DCD USART2_IRQHandler ; USART2
  41. DCD USART3_IRQHandler ; USART3
  42. DCD EXTI15_10_IRQHandler ; EXTI Line 15..10
  43. DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line
  44. DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend

 这里我们用到的是14引脚 所以用这个中断函数

  1. void EXTI15_10_IRQHandler(void)
  2. {
  3. if (EXTI_GetITStatus(EXTI_Line14) == SET)
  4. {
  5. COUNTSENSOR_COUNT++;
  6. EXTI_ClearITPendingBit(EXTI_Line14);
  7. }
  8. }

下面是EXTI中断示范代码,头文件需要自己添加,能够实现计次加数

  1. #include "stm32f10x.h" // Device header
  2. uint16_t COUNTSENSOR_COUNT;
  3. void CountSensor_Init(void)
  4. {
  5. //使能GPIO和AFIO的时钟
  6. //EXTI和NVIC的时钟是一直打开的,所以不用使能,NVIC不归RCC管
  7. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  8. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  9. GPIO_InitTypeDef GPIO_InitStruct;
  10. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  11. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14;
  12. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  13. GPIO_Init(GPIOB, &GPIO_InitStruct);
  14. //配置AFIO
  15. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
  16. //配置EXTI 这里配置成上升沿触发中断,引脚为14
  17. EXTI_InitTypeDef EXTI_InitStruct;
  18. EXTI_InitStruct.EXTI_Line = EXTI_Line14;
  19. EXTI_InitStruct.EXTI_LineCmd = ENABLE;
  20. EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
  21. EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
  22. EXTI_Init(&EXTI_InitStruct);
  23. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  24. NVIC_InitTypeDef NVIC_InitStruct;
  25. NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn;
  26. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  27. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
  28. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  29. NVIC_Init(&NVIC_InitStruct);
  30. }
  31. uint16_t CountSensor_Get(void)
  32. {
  33. return COUNTSENSOR_COUNT;
  34. }
  35. void EXTI15_10_IRQHandler(void)
  36. {
  37. if (EXTI_GetITStatus(EXTI_Line14) == SET)
  38. {
  39. COUNTSENSOR_COUNT++;
  40. EXTI_ClearITPendingBit(EXTI_Line14);
  41. }
  42. }

main.c

  1. #include "stm32f10x.h" // Device header
  2. #include "Delay.h"
  3. #include "OLED.h"
  4. #include "CountSensor.h"
  5. int main()
  6. {
  7. OLED_Init();
  8. CountSensor_Init();
  9. OLED_ShowString(1, 1, "Count");
  10. while (1)
  11. {
  12. OLED_ShowNum(1, 7, CountSensor_Get(), 5);
  13. }
  14. }

总结

学好GPIO外设的使用方法 后面的这些外设使用起来都大差不差 而且ARM公司提供的库函数的函数注释也很详细 感觉最主要的就是要学会看框图 知道自己的工程需要哪些外设 然后他的电路图如何走 跟着一步步初始化和使能就可以 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/848368
推荐阅读
相关标签
  

闽ICP备14008679号