cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f103 button interrupt not working

Don david
Associate II
Posted on March 23, 2017 at 09:22

Hi All,

I am working on button press interrupt on stm32f103 . I am not able to get it into work. I am not at all receiving any interrupt when the button which is connected to PORT B 12 13 14 is pressed

Below is my code snippet. Please Help

void init_exti()

{

    EXTI_InitTypeDef   EXTI_InitStructure;

    GPIO_InitTypeDef   GPIO_InitStructure;

    NVIC_InitTypeDef   NVIC_InitStructure;

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

      /* Configure PA.00 pin as input floating */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12

      | GPIO_Pin_13 |GPIO_Pin_14;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//input pull up

      GPIO_Init(GPIOC, &GPIO_InitStructure);

       /* Enable AFIO clock */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

        /* Connect EXTI0 Line to PA.00 pin */

      GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12);

      GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13);

      GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);

      

      /* Configure EXTI line */

    EXTI_InitStructure.EXTI_Line = EXTI_Line12;

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  

    EXTI_InitStructure.EXTI_LineCmd = ENABLE;

    EXTI_Init(&EXTI_InitStructure);

    EXTI_InitStructure.EXTI_Line = EXTI_Line13;

    EXTI_Init(&EXTI_InitStructure);

    EXTI_InitStructure.EXTI_Line = EXTI_Line14;

    EXTI_Init(&EXTI_InitStructure);

    

    /* Enable and set EXTI0 Interrupt to the lowest priority */

    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQChannel;

    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQChannel;

    NVIC_Init(&NVIC_InitStructure);

}

void EXTI2_IRQHandler(void)

{

  

        

if(EXTI_GetITStatus(EXTI_Line14) != RESET)

 {

     GPRS_TX_Bytes('INTERRUPT 3-');

     adc_read=1;

    

 }

  EXTI_ClearITPendingBit(EXTI_Line14);

    

    

    

}

/*******************************************************************************

* Function Name  : EXTI1_IRQHandler

* Description    : This function handles External interrupt Line 1 request.

* Input          : None

* Output         : None

* Return         : None

*******************************************************************************/

void EXTI0_IRQHandler(void)

{

  

if(EXTI_GetITStatus(EXTI_Line12) != RESET)    

 {

    

  

     adc_read=1;

    

 }

  EXTI_ClearITPendingBit(EXTI_Line12);

    

}

/*******************************************************************************

* Function Name  : EXTI2_IRQHandler

* Description    : This function handles External interrupt Line 2 request.

* Input          : None

* Output         : None

* Return         : None

*******************************************************************************/

void EXTI1_IRQHandler(void)

{

  

      if(EXTI_GetITStatus(EXTI_Line13) != RESET)

 {

    

   

     adc_read=1;

    

 }

  EXTI_ClearITPendingBit(EXTI_Line13);

    

}

Kind Regards

Don David

3 REPLIES 3
Nesrine M_O
Lead II
Posted on March 23, 2017 at 10:11

Hi

David.Don

,

I recommend you to have a look to this example it may be helpful for you :

STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Examples\EXTI\EXTI_Config

-Nesrine-

AvaTar
Lead
Posted on March 23, 2017 at 11:18

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO

B

, ENABLE);

      /* Configure

PA.00

pin as input floating */

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12

      | GPIO_Pin_13 |GPIO_Pin_14;

      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//input pull up

      GPIO_Init(GPIO

C

, &GPIO_InitStructure);

       /* Enable AFIO clock */

      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

        /* Connect EXTI0 Line to PA.00 pin */

      GPIO_EXTILineConfig(GPIO_PortSourceGPIO

B

, GPIO_PinSource12);

Looks like a mismatch to me ...

Posted on April 28, 2017 at 17:00

 ,

 ,

Hi Nesrine, Don David, I'm also struggling with my interrupts (and alas I can't get the EXTI_Config example to work on my board, however the code below is modelled on it).

Every time the interrupt fires, the MCU crashes.

I've seemingly tried every possible variant for initialising the NVIC and EXTI lines - what am I doing wrong?

This is for the NUCLEO-F103RB, but also I've tried it on a STM32F103RC.

♯ pragma GCC diagnostic push

 ,

♯ pragma GCC diagnostic ignored '-Wunused-parameter'

 ,

♯ pragma GCC diagnostic ignored '-Wmissing-declarations'

 ,

♯ pragma GCC diagnostic ignored '-Wreturn-type'

// ♯ define USE_STDPERIPH_DRIVER

 ,

♯ include <,stdio.h>,

 ,

♯ include <,stdlib.h>,

 ,

♯ include 'diag/Trace.h'

♯ include 'stm32f10x.h'

 ,

♯ include 'stm32f10x_rcc.h'

 ,

// ♯ include 'system_stm32f10x.c'

void setup_nucleo_gpio(void),

 ,

void power_up_modules(void),

 ,

void configure_nucleo_interrupt(),

 ,

void EXTI15_10_IRQHandler(),

 ,

int nucleo_button(),

 ,

void nucleo_led_off(),

 ,

void nucleo_led_on(),

void nucleo_led_on(void){

 ,

 , , ,GPIO_SetBits(GPIOA,GPIO_Pin_5),

 ,

}

 ,

void nucleo_led_off(void){

 ,

 , , ,GPIO_ResetBits(GPIOA,GPIO_Pin_5),

 ,

}

 ,

void EXTI15_10_IRQHandler(void){

 ,

 , , ,nucleo_led_on(),

 ,

 , , ,if (EXTI_GetITStatus(EXTI_Line13) != RESET){

 ,

 , , , , , ,EXTI_ClearITPendingBit(EXTI_Line13),

 ,

 , , ,}

 ,

}

 ,

void configure_nucleo_interrupt(void){

 ,

 , , ,trace_printf('interrupt config start\n'),

 ,

 , , ,GPIO_EXTILineConfig(GPIO_PortSourceGPIOC,GPIO_PinSource13),

 , , ,EXTI_InitTypeDef EXTI_InitStructure, , , ,

 ,

 , , ,EXTI_InitStructure.EXTI_Line = EXTI_Line13, , , ,

 ,

 , , ,EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt, , , ,

 ,

 , , ,EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling, , , ,

 ,

 , , ,EXTI_InitStructure.EXTI_LineCmd = ENABLE, , , ,

 ,

 , , ,EXTI_Init(&,EXTI_InitStructure),

 , , ,NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4),

 , , ,NVIC_InitTypeDef NVIC_InitStruct,

 ,

 , , ,NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE,

 ,

 , , ,NVIC_InitStruct.NVIC_IRQChannel = EXTI15_10_IRQn,

 ,

 , , ,NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0F, // lowest priority

 ,

 , , ,NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x0F,

 ,

 , , ,NVIC_Init(&,NVIC_InitStruct),

 ,

 , , ,NVIC_SetPriority(EXTI15_10_IRQn,15),

 ,

 , , ,NVIC_SetPriority(EXTI9_5_IRQn,14),

 ,

 , , ,NVIC_SetPriority(SysTick_IRQn,0),

 , , ,trace_printf('interrupt config finish\n'),

 ,

 ,}

 , , ,

 ,

void setup_nucleo_gpio(void){

 ,

 , , ,GPIO_InitTypeDef GPIO_InitStruct,

 ,

 , , ,GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5, //led

 ,

 , , ,GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP,

 ,

 , , ,GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz,

 ,

 , , ,GPIO_Init(GPIOA,&,GPIO_InitStruct),

 , , ,GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13, //button PC13

 ,

 , , ,GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU,

 ,

 , , ,GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz,

 ,

 , , ,GPIO_Init(GPIOC,&,GPIO_InitStruct),

 , , ,trace_printf('gpio config finished\n'),

 ,}

void power_up_modules(void){

 ,

 , , ,// power up modules

 ,

 , , ,RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE),

 ,

 , , RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE), //alternate function i/o

 ,

 , ,

}

 ,

int ,main(int argc, char* argv[])

 ,

{

 , , ,

 ,

 , , ,power_up_modules(),

 ,

 , , ,trace_printf('powered up\n'),

 ,

 , , ,setup_nucleo_gpio(),

 ,

 , , ,trace_printf('gpio read\n'),

 ,

 , , ,//configure_nucleo_interrupt(),

 ,

 , , ,trace_printf('interrupts ready\n'),

 , , ,nucleo_led_off(),

 ,

 , , ,trace_printf('go go go\n'),

 , , ,

 , , ,

 ,

 , , ,while (1)

 ,

 , , ,{

 ,

 , , ,}

}