stm学习第3天

简介: stm学习第3天

一.嵌套中断向量控制器NVIC

1.NVIC介绍:管理中断嵌套,管理中断优先级

向量表在汇编文件中

1)选择哪一组响应优先级;

void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
@arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  *                                4 bits for subpriority
  *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  *                                3 bits for subpriority
  *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  *                                2 bits for subpriority
  *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  *                                1 bits for subpriority
  *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  *                                0 bits for subpriority

2)NVIC初始化:

void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);

结构体指针:因此要先定义结构体;

typedef struct
{
  uint8_t NVIC_IRQChannel;     //NVIC中断通道             
  uint8_t NVIC_IRQChannelPreemptionPriority;   //抢占优先级  最大值15
  uint8_t NVIC_IRQChannelSubPriority;          //响应优先级   最大值15
  FunctionalState NVIC_IRQChannelCmd;          //ENABLE或DISABLE  使能通道
} NVIC_InitTypeDef;

(1)uint8_t NVIC_IRQChannel;     //NVIC中断通道

EXTI9_5_IRQn 为5到9通用中断

typedef enum IRQn
{
/******  Cortex-M3 Processor Exceptions Numbers ***************************************************/
  NonMaskableInt_IRQn         = -14,    /*!< 2 Non Maskable Interrupt                             */
  MemoryManagement_IRQn       = -12,    /*!< 4 Cortex-M3 Memory Management Interrupt              */
  BusFault_IRQn               = -11,    /*!< 5 Cortex-M3 Bus Fault Interrupt                      */
  UsageFault_IRQn             = -10,    /*!< 6 Cortex-M3 Usage Fault Interrupt                    */
  SVCall_IRQn                 = -5,     /*!< 11 Cortex-M3 SV Call Interrupt                       */
  DebugMonitor_IRQn           = -4,     /*!< 12 Cortex-M3 Debug Monitor Interrupt                 */
  PendSV_IRQn                 = -2,     /*!< 14 Cortex-M3 Pend SV Interrupt                       */
  SysTick_IRQn                = -1,     /*!< 15 Cortex-M3 System Tick Interrupt                   */
/******  STM32 specific Interrupt Numbers *********************************************************/
  WWDG_IRQn                   = 0,      /*!< Window WatchDog Interrupt                            */
  PVD_IRQn                    = 1,      /*!< PVD through EXTI Line detection Interrupt            */
  TAMPER_IRQn                 = 2,      /*!< Tamper Interrupt                                     */
  RTC_IRQn                    = 3,      /*!< RTC global Interrupt                                 */
  FLASH_IRQn                  = 4,      /*!< FLASH global Interrupt                               */
  RCC_IRQn                    = 5,      /*!< RCC global Interrupt                                 */
  EXTI0_IRQn                  = 6,      /*!< EXTI Line0 Interrupt                                 */
  EXTI1_IRQn                  = 7,      /*!< EXTI Line1 Interrupt                                 */
  EXTI2_IRQn                  = 8,      /*!< EXTI Line2 Interrupt                                 */
  EXTI3_IRQn                  = 9,      /*!< EXTI Line3 Interrupt                                 */
  EXTI4_IRQn                  = 10,     /*!< EXTI Line4 Interrupt                                 */
  DMA1_Channel1_IRQn          = 11,     /*!< DMA1 Channel 1 global Interrupt                      */
  DMA1_Channel2_IRQn          = 12,     /*!< DMA1 Channel 2 global Interrupt                      */
  DMA1_Channel3_IRQn          = 13,     /*!< DMA1 Channel 3 global Interrupt                      */
  DMA1_Channel4_IRQn          = 14,     /*!< DMA1 Channel 4 global Interrupt                      */
  DMA1_Channel5_IRQn          = 15,     /*!< DMA1 Channel 5 global Interrupt                      */
  DMA1_Channel6_IRQn          = 16,     /*!< DMA1 Channel 6 global Interrupt                      */
  DMA1_Channel7_IRQn          = 17,     /*!< DMA1 Channel 7 global Interrupt                      */
#ifdef STM32F10X_LD
  ADC1_2_IRQn                 = 18,     /*!< ADC1 and ADC2 global Interrupt                       */
  USB_HP_CAN1_TX_IRQn         = 19,     /*!< USB Device High Priority or CAN1 TX Interrupts       */
  USB_LP_CAN1_RX0_IRQn        = 20,     /*!< USB Device Low Priority or CAN1 RX0 Interrupts       */
  CAN1_RX1_IRQn               = 21,     /*!< CAN1 RX1 Interrupt                                   */
  CAN1_SCE_IRQn               = 22,     /*!< CAN1 SCE Interrupt                                   */
  EXTI9_5_IRQn                = 23,     /*!< External Line[9:5] Interrupts                        */
  TIM1_BRK_IRQn               = 24,     /*!< TIM1 Break Interrupt                                 */
  TIM1_UP_IRQn                = 25,     /*!< TIM1 Update Interrupt                                */
  TIM1_TRG_COM_IRQn           = 26,     /*!< TIM1 Trigger and Commutation Interrupt               */
  TIM1_CC_IRQn                = 27,     /*!< TIM1 Capture Compare Interrupt                       */
  TIM2_IRQn                   = 28,     /*!< TIM2 global Interrupt                                */
  TIM3_IRQn                   = 29,     /*!< TIM3 global Interrupt                                */
  I2C1_EV_IRQn                = 31,     /*!< I2C1 Event Interrupt                                 */
  I2C1_ER_IRQn                = 32,     /*!< I2C1 Error Interrupt                                 */
  SPI1_IRQn                   = 35,     /*!< SPI1 global Interrupt                                */
  USART1_IRQn                 = 37,     /*!< USART1 global Interrupt                              */
  USART2_IRQn                 = 38,     /*!< USART2 global Interrupt                              */
  EXTI15_10_IRQn              = 40,     /*!< External Line[15:10] Interrupts                      */
  RTCAlarm_IRQn               = 41,     /*!< RTC Alarm through EXTI Line Interrupt                */
  USBWakeUp_IRQn              = 42      /*!< USB Device WakeUp from suspend through EXTI 
#endif /* STM32F10X_LD */  
......

二.外部中断

外部中断线路映像:

EXTI相关数据结构与函数说明:

初始化函数:

void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct);
typedef struct
{
  uint32_t EXTI_Line;        //外部中断线       
  EXTIMode_TypeDef EXTI_Mode; // 外部中断模式
  //EXTI_Mode_Interrupt = 0x00   //中断模式
  //EXTI_Mode_Event = 0x04        //事件模式
  EXTITrigger_TypeDef EXTI_Trigger; //触发方式
    //EXTI_Trigger_Rising = 0x08, 上升沿
    //EXTI_Trigger_Falling = 0x0C,  下降沿
    //EXTI_Trigger_Rising_Falling = 0x10   双边沿
  FunctionalState EXTI_LineCmd;  //中断使能  
//ENABLE   
}EXTI_InitTypeDef;
EXTI_GetITStatus(EXTI_Line5)   //返回中断状态
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);     //中断线映射,每个引脚都要加入
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);
TIM_GetITStatus(TIM3,TIM_IT_Update)  //定时器中断
EXTI_ClearITPendingBit(EXTI_Line5)  //清空中断标志位
TStatus EXTI_GetITStatus(uint32_t EXTI_Line)
{
  ITStatus bitstatus = RESET;
  uint32_t enablestatus = 0;
  /* Check the parameters */
  assert_param(IS_GET_EXTI_LINE(EXTI_Line));
  enablestatus =  EXTI->IMR & EXTI_Line;
  if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return bitstatus;
}
//产生中断返回SET,不产生中断返回RESET

中断回调函数比较特殊,名字不能改用的哪一个NVIC中断通道 (不是外部中断线)寻找函数名;

TIM_GetITStatus(TIM3,TIM_IT_Update)  //定时器中断
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);清空中断标志位
EXTI_GetITStatus(EXTI_Line5)   //返回中断状态  ,产生中断返回SET,不产生中断返回RESET
EXTI_ClearITPendingBit(EXTI_Line5)  //清空中断标志位
/一 GPIO初始化
  GPIO_InitTypeDef GPIO_InitStructure;
  //2 打开时钟有两个
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;  //浮空输入
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOB,&GPIO_InitStructure);
  //二 中断优先级初始化
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   //三 外部中断初始化
    EXTI_InitTypeDef EXTI_Init_Structrue;
  EXTI_Init_Structrue.EXTI_Line = EXTI_Line5|EXTI_Line6|EXTI_Line7;
  EXTI_Init_Structrue.EXTI_Mode=EXTI_Mode_Interrupt;
  EXTI_Init_Structrue.EXTI_Trigger =EXTI_Trigger_Falling;
      //EXTI_Trigger_Rising = 0x08, 上升沿
      //EXTI_Trigger_Falling = 0x0C,  下降沿
  EXTI_Init_Structrue.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_Init_Structrue);
  //四 中断线映射
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource5);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);

三.DMA:直接存储器存取

相关文章
|
7月前
|
传感器 IDE 开发工具
STM32
STM32
73 0
STM32串口编程基础知识讲解
STM32串口编程基础知识讲解
106 0
|
7月前
|
芯片 内存技术
|
7月前
|
传感器 数据采集 算法
STM32应用效果
STM32应用效果
67 0
|
7月前
|
数据采集 存储 传感器
STM32 ADC基础知识讲解
STM32 ADC基础知识讲解
157 0
STM32速成笔记(十)—IWDG
本文详细介绍了什么是IWDG,STM32的IWDG特性,框图和配置步骤。此外,给出了STM32的IWDG配置程序。通过一个简单的应用实例,展示了IWDG的配置和使用方法。
141 0
STM32速成笔记(十)—IWDG
|
存储 传感器
STM32速成笔记(七)—ADC
本文介绍了ADC的概念,用途,针对STM32的ADC做出了详细介绍,给出了配置步骤,配置程序。通过一个简单的小项目展示了ADC的配置和使用方法。此外,还针对如何利用定时器触发AD转换,如何采集交流信号,如何计算交流信号有效值进行了介绍,并给出了程序设计。
299 0
STM32速成笔记(七)—ADC
|
7月前
|
存储 编译器 API
大神们分享STM32的学习方法
大神们分享STM32的学习方法
85 0
|
存储 安全 Linux
STM32F0单片机快速入门一 ARM架构与STM32F0
STM32F0单片机快速入门一 ARM架构与STM32F0
|
存储 API C语言
从0学习stm32
从0学习stm32
138 0