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:
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?
Best Answer
Your interrupt is connected to
EXTI2
although the vector for the handler is looking forEXTI0
. ChangeEXTI0_IRQHandler
toEXTI2_IRQHandler