1
\$\begingroup\$

I have written this program for GPIO External Interrupt but it doesn't work. look at the program:

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"


/* Private functions ---------------------------------------------------------*/

GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

void GPIO_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);


/*******************************************************************************
* Function Name  : main
* Description    : Main Programme
* Input          : None
* Output         : None
* Return         : None
* Attention      : None
*******************************************************************************/
int main(void)
{
    GPIO_Configuration();
    NVIC_Configuration();
    EXTI_Configuration();


    /* Infinite loop */
    while (1)
    {

  }
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configure GPIO Pin
* Input          : None
* Output         : None
* Return         : None
* Attention      : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE);                        
/**
 *  LED1 -> PB0
 */                  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
  GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* PB2-> Input */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
  GPIO_Init(GPIOB, &GPIO_InitStructure);

}

void EXTI_Configuration(void)
{

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 

  GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource2);
  EXTI_ClearITPendingBit(EXTI_Line2);

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_Line = EXTI_Line2;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

}

void NVIC_Configuration(void)
{

  NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;   
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;      
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure); 
}

void EXTI0_IRQHandler(void)
{
    if ( EXTI_GetITStatus(EXTI_Line2) != RESET ) {
        EXTI_ClearITPendingBit(EXTI_Line2);
      GPIO_SetBits(GPIOB , GPIO_Pin_0);
    }

}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/******************************************************************************************
      END FILE
******************************************************************************************/

and this my circuit:

Image

I have even pulled-down the PB2 but doesn't work, again. the program correctly build and download (without any error or warning).

What's the problem? any idea?

\$\endgroup\$
  • \$\begingroup\$ Add a pull down resistor to the input pin, i.e. \$10k\Omega\$ between PB2 and ground. And please add a resistor for that LED, \$1k\Omega\$ is ok, if it turns on it will probably fry. Maybe your circuit does not work because you fried it already... \$\endgroup\$ – Vladimir Cravero Jul 9 '14 at 16:28
  • \$\begingroup\$ Moreover, your interrupt routine turns on the led but it's never turned off. The odds you burnt it are quite high I'd say. \$\endgroup\$ – Vladimir Cravero Jul 9 '14 at 16:29
  • \$\begingroup\$ @VladimirCravero As I said, I have already pulled-down it but... No, The LED is ok. yeah, I know but it doesn't work for one time even. \$\endgroup\$ – Roh Jul 9 '14 at 16:30
  • 4
    \$\begingroup\$ I believe your code is wrong. Your interrupt is connected to EXTI2 although the vector for the handler is looking for EXTI0. Change " EXTI0_IRQHandler" to " EXTI2_IRQHandler" Let me know if this fixes it \$\endgroup\$ – Funkyguy Jul 9 '14 at 17:50
  • 2
    \$\begingroup\$ @ShannonStrutz Greeeaaaattttzz! absolutely you are right! I changed it and now it's working! put your answer because I want to select your answer and +1 \$\endgroup\$ – Roh Jul 10 '14 at 3:46
2
\$\begingroup\$

Your interrupt is connected to EXTI2 although the vector for the handler is looking for EXTI0. Change EXTI0_IRQHandler to EXTI2_IRQHandler

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.