Call to stm32 port, interrupt initialization and interrupt function
Port initialization
First, let's look at the officially provided port initialization library function (take stm32f4xx as an example)
ps: it's very difficult to see the functions provided by the official. Therefore, based on the examples, some of the official codes can't be displayed. You can right-click in keil5 to view them.
//**I. enable clock function configuration** void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState); //The FunctionalState NewState parameter in the RCC? Ahb1periphclockcmd function typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; //**II. GPIO initialization definition** typedef struct { uint32_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured. This parameter can be any value of @ref GPIO_pins_define */ GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins. This parameter can be a value of @ref GPIOMode_TypeDef */ GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins. This parameter can be a value of @ref GPIOSpeed_TypeDef */ GPIOOType_TypeDef GPIO_OType; /*!< Specifies the operating output type for the selected pins. This parameter can be a value of @ref GPIOOType_TypeDef */ GPIOPuPd_TypeDef GPIO_PuPd; /*!< Specifies the operating Pull-up/Pull down for the selected pins. This parameter can be a value of @ref GPIOPuPd_TypeDef */ }GPIO_InitTypeDef; //**3. Port configuration function** void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
Port initialization example
#include "stm32f4xx.h" //stm32f4xx header file static GPIO_InitTypeDef GPIO_InitStructure; //Define the structure of GPIO inittypedef type GPIO initstructure int main() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Clock of enable port A RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); //Clock of enable port B GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; //Pin 12 (bit can be used or multiple pins can be added) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //Output mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Input or output processing frequency 100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push pull mode to increase output current GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //No pull-up resistance required GPIO_Init(GPIOA, &GPIO_InitStructure); //**Configure port A** GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; //3rd and 4th pin GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN //Input mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Input or output processing frequency 100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push pull mode to increase the output current GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //No pull-up resistance required GPIO_Init(GPIOB, &GPIO_InitStructure); //**Configure port B** }
Interrupt initialization
Next, take a look at the officially provided interrupt initialization library function (take stm32f4xx as an example)
//**I. enable clock function configuration** void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState); //**II. Configure interrupt pin** void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex); //**3. Configure interrupt mode** typedef struct { uint32_t EXTI_Line; /*!< Specifies the EXTI lines to be enabled or disabled. This parameter can be any combination value of @ref EXTI_Lines */ EXTIMode_TypeDef EXTI_Mode; /*!< Specifies the mode for the EXTI lines. This parameter can be a value of @ref EXTIMode_TypeDef */ EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines. This parameter can be a value of @ref EXTITrigger_TypeDef */ FunctionalState EXTI_LineCmd; /*!< Specifies the new state of the selected EXTI lines. This parameter can be set either to ENABLE or DISABLE */ }EXTI_InitTypeDef; //**IV. effective interruption mode** void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct); //**V. set interrupt priority group** void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup); //**Vi. configure interrupt priority** typedef struct { uint8_t NVIC_IRQChannel; /* !< Specifies the IRQ channel to be enabled or disabled. This parameter can be an enumerator of @ref IRQn_Type enumeration (For the complete STM32 Devices IRQ Channels list, please refer to stm32f4xx.h file) */ uint8_t NVIC_IRQChannelPreemptionPriority; /*!< Specifies the pre-emption priority for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table A lower priority value indicates a higher priority */ uint8_t NVIC_IRQChannelSubPriority; /*!< Specifies the subpriority level for the IRQ channel specified in NVIC_IRQChannel. This parameter can be a value between 0 and 15 as described in the table @ref MISC_NVIC_Priority_Table A lower priority value indicates a higher priority */ FunctionalState NVIC_IRQChannelCmd; /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel will be enabled or disabled. This parameter can be set either to ENABLE or DISABLE */ } NVIC_InitTypeDef; //**VII. Effective interruption mode** void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
Interrupt initialization example
#include "stm32f4xx.h" static EXTI_InitTypeDef EXTI_InitStructure; //Define exti inittypedef type structure exti initstructure static NVIC_InitTypeDef NVIC_InitStructure; //Define NVIC inittypedef type structure NVIC initstructure int main(void) { /*Enable SYSCFG to configure clock*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /*Configure an input pin, pin is PA0, connect PA0 pin to external interrupt line 0*/ SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0); /* Configure the external interrupt line 0 as the interrupt mode, and the trigger mode is rising edge trigger. */ EXTI_InitStructure.EXTI_Line = EXTI_Line0; //External break line 0 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //Interrupt mode EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //When the rising edge is triggered, only the release state of the key can be detected. EXTI_InitStructure.EXTI_LineCmd = ENABLE; //Enable external interrupt line 0 to be valid EXTI_Init(&EXTI_InitStructure); //Configure the interrupt mode /*Set interrupt priority group 2*/ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /*Configure external interrupt line 0 priority*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //Set interrupt number NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //Preemption priority 0x00 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //Sub (response) priority 0x01 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt line 0 priority to be valid NVIC_Init(&NVIC_InitStructure); //Configure the interrupt priority }
Call example of interrupt function
//External interrupt 0 interrupt service function void EXTI0_IRQHandler(void) //Interrupt service function exti0? Irqhandler exti1? Irqhandler exti2? Irqhandler exti3? Irqhandler... { //Check whether the medium and short line 0 is triggered by interrupt if(EXTI_GetITStatus(EXTI_Line0) = SET) { void fun(void);//User defined content //Complete the interrupt request, end the interrupt, and process the new external interrupt. EXTI_ClearITPendingBit(EXTI_Line0); } }