新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 EXTI的配置

        STM32 EXTI的配置

        作者: 時間:2016-11-18 來源:網絡 收藏
        按鍵的硬件結構有一點一定要注意,要在GPIO段上拉電阻,否則GPIO設置成浮空輸入后,會造成端口電平不穩定,中斷效果不理想。

        另外EXTI的映射關系可以看下圖,是和管腳號對應的,比較好記

        本文引用地址:http://www.104case.com/article/201611/315977.htm

        (1)Main


        C語言:Codee#14817
        /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        + 實驗平臺 : ST 官方三合一套件
        + 硬件 : STM32F103C8T6
        + 開發平臺 : IAR For ARM 5.40
        + 仿真器 : J-Link
        + 日期 : 2010-11-4
        + 頻率 :HSE = 8MHz ,主頻 = 72MHz
        ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
        #include "includes.h"
        /*******************************************************************************
        == Main 函數 ==
        *******************************************************************************/
        int main(void)
        {
        RCC_Configuration(); //配置系統時鐘
        NVIC_Configuration(); //配置 NVIC 和 Vector Table
        SysTick_Config(); //配置SysTick的精確延時
        GPIO_Configuration();
        EXTI_Configuration();
        LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; // 初始化讓燈全滅
        while (1)
        {

        if( KEY_UP == 0 )
        { LED1_LOW ;}
        if( KEY_DOWN == 0 )
        { LED2_LOW ;}
        if( KEY_LEFT == 0 )
        { LED3_LOW ;}
        if( KEY_RIGHT == 0 )
        { LED4_LOW ;}
        }
        }

        (2)Unique_Device_ID.c


        C語言:Codee#14816
        #include "includes.h"
        /*******************************************************************************
        == 全局變量 ==
        *******************************************************************************/
        vu32 TimingDelay; // 精確延時在SysTick中斷里用的計數變量
        /*******************************************************************************
        * Function Name : RCC_Configuration 配置時鐘
        * Description : Configures the different system clocks.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void RCC_Configuration(void)
        {
        ErrorStatus HSEStartUpStatus;
        //將外設 RCC寄存器重設為缺省值
        RCC_DeInit();
        //設置外部高速晶振(HSE)
        RCC_HSEConfig(RCC_HSE_ON);
        //等待 HSE 起振
        HSEStartUpStatus = RCC_WaitForHSEStartUp();
        if(HSEStartUpStatus == SUCCESS)
        {
        //預取指緩存使能
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
        //設置代碼延時值
        //FLASH_Latency_2 2 延時周期
        FLASH_SetLatency(FLASH_Latency_2);
        //設置 AHB 時鐘(HCLK)
        //RCC_SYSCLK_Div1 AHB 時鐘 = 系統時鐘
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
        //設置高速 AHB 時鐘(PCLK2)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK2Config(RCC_HCLK_Div2);
        //設置低速 AHB 時鐘(PCLK1)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK1Config(RCC_HCLK_Div2);
        // PLLCLK = 8MHz * 9 = 72 MHz
        //設置 PLL 時鐘源及倍頻系數
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
        //使能或者失能 PLL
        RCC_PLLCmd(ENABLE);
        //等待指定的 RCC 標志位設置成功 等待PLL初始化成功
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }
        //設置系統時鐘(SYSCLK) 設置PLL為系統時鐘源
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
        //等待PLL成功用作于系統時鐘的時鐘源
        // 0x00:HSI 作為系統時鐘
        // 0x04:HSE作為系統時鐘
        // 0x08:PLL作為系統時鐘
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
        }
        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);
        //使能或者失能 APB2 外設時鐘
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
        }
        /*******************************************************************************
        * Function Name : NVIC_Configuration 配置中斷優先級
        * Description : Configures NVIC and Vector Table base location.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void NVIC_Configuration(void)
        {
        NVIC_InitTypeDef NVIC_InitStructure_EXTI_LINE7;

        #ifdef VECT_TAB_RAM
        /* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
        #else /* VECT_TAB_FLASH */
        /* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
        #endif
        //===== NVIC_EXTI_PB7 ===================================================
        /* Configure the NVIC Preemption Priority Bits */
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
        /* Enable the EXTI PB7 Interrupt */
        NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannel = EXTI9_5_IRQChannel; // 配置使能指定的IRQ(Interrupt ReQuest中斷請求)通道
        NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelPreemptionPriority = 0; // 配置IRQ的 組 優先級
        NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelSubPriority = 0; // 配置IRQ的 從 優先級
        NVIC_InitStructure_EXTI_LINE7.NVIC_IRQChannelCmd = ENABLE; // 配置IRQ 使能
        NVIC_Init(&NVIC_InitStructure_EXTI_LINE7); // 初始化 UART1_IRQ
        }
        /*******************************************************************************
        * Function Name : GPIO_Configuration 配置管教
        * Description : Configures the different GPIO ports.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure_LED_PORTB;
        GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTA;
        GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTB;
        GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTC;
        //==== LED =======================================================
        GPIO_InitStructure_LED_PORTB.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
        GPIO_InitStructure_LED_PORTB.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure_LED_PORTB.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
        GPIO_Init(GPIOB, &GPIO_InitStructure_LED_PORTB);
        //==== KEY =======================================================
        GPIO_InitStructure_KEY_PORTA.GPIO_Pin = GPIO_Pin_0 ;
        GPIO_InitStructure_KEY_PORTA.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTA.GPIO_Mode = GPIO_Mode_IPU ; //上拉輸入
        GPIO_Init(GPIOA, &GPIO_InitStructure_KEY_PORTA);
        GPIO_InitStructure_KEY_PORTB.GPIO_Pin = GPIO_Pin_7 ;
        GPIO_InitStructure_KEY_PORTB.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTB.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入 ,外部中斷按鍵
        GPIO_Init(GPIOB, &GPIO_InitStructure_KEY_PORTB);
        GPIO_InitStructure_KEY_PORTC.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
        GPIO_InitStructure_KEY_PORTC.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTC.GPIO_Mode = GPIO_Mode_IPU; //上拉輸入
        GPIO_Init(GPIOC, &GPIO_InitStructure_KEY_PORTC);
        }
        /*******************************************************************************
        * Function Name : EXTI_Configuration 配置外部中斷管教
        * Description : Configures the EXTI
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void EXTI_Configuration(void)
        {
        EXTI_InitTypeDef EXTI_InitStructure_EXTI_LINE7;
        /* Connect EXTI Line7 to PB7 */
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource7); // 配置 管腳PB7用作外部中斷線路
        /* Configure EXTI Line7 to generate an interrupt on falling edge */
        EXTI_InitStructure_EXTI_LINE7.EXTI_Line = EXTI_Line7; //配置 使能或失能的外部線路
        EXTI_InitStructure_EXTI_LINE7.EXTI_Mode = EXTI_Mode_Interrupt; //配置 EXTI線路為中斷請求 (或者是事件請求)
        EXTI_InitStructure_EXTI_LINE7.EXTI_Trigger = EXTI_Trigger_Falling; //配置 使能線路的觸發邊沿 -- 下降沿觸發中斷
        EXTI_InitStructure_EXTI_LINE7.EXTI_LineCmd = ENABLE; //配置 狀態為使能
        EXTI_Init(&EXTI_InitStructure_EXTI_LINE7); // 初始化外部中斷線路7
        /* Generate software interrupt: simulate a falling edge applied on EXTI line 7 */
        EXTI_GenerateSWInterrupt(EXTI_Line7); //線路7產生一個軟件中斷
        }
        /*******************************************************************************
        * Function Name : SysTick_Config SysTick設置
        * Description : Configures SysTick
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        void SysTick_Config(void)
        {
        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);
        /* Disable the SysTick Interrupt */
        SysTick_ITConfig(DISABLE);
        /* Configure HCLK clock as SysTick clock source */
        SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
        /* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
        SysTick_SetReload(9000);
        /* Enable the SysTick Interrupt */
        SysTick_ITConfig(ENABLE);
        }
        /*******************************************************************************
        * Function Name : 精確延時函數
        *******************************************************************************/
        void Delay_Ms(u32 nTime)
        {
        /* Enable the SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Enable);
        TimingDelay = nTime;
        while(TimingDelay != 0);
        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);
        /* Clear SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Clear);
        }

        (4)stm32f10x_it.c


        C語言:Codee#14818
        /* Includes ------------------------------------------------------------------*/
        #include "includes.h"
        //#include "stm32f10x_it.h"
        // ... ...
        /*******************************************************************************
        * Function Name : EXTI9_5_IRQHandler
        * Description : This function handles External lines 9 to 5 interrupt request.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        // LINE : 407
        void EXTI9_5_IRQHandler(void)
        {
        if(EXTI_GetITStatus(EXTI_Line7) == SET) // 讀取中斷狀態
        {

        LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; // 燈全滅

        EXTI_ClearITPendingBit(EXTI_Line7); // 清除標志位
        }

        }


        關鍵詞: STM32EXT

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 神农架林区| 玉山县| 禹州市| 江华| 柘城县| 区。| 孙吴县| 白银市| 大港区| 泾阳县| 南溪县| 平顺县| 邹城市| 富宁县| 伽师县| 张家界市| 台东市| 玉树县| 陵川县| 涞水县| 尚义县| 云林县| 玉屏| 得荣县| 凤凰县| 本溪市| 肥乡县| 谢通门县| 岳阳市| 安达市| 曲靖市| 衡阳市| 无极县| 宁明县| 翁牛特旗| 江津市| 陇川县| 凌源市| 田林县| 融水| 巴楚县|