Jump to content

STM32F4xx programming

Anyone ever worked with an STM32F401C-DISCO micro controller? i'm currently testing one but i'm having difficulty to make it do one application: when pressing the user button start led toggle and when pressing it again turn off the led;

so far i've managed to link the button to an EXTI and make it start the LED toggle when pressed, but i can't figure out how to make it stop. STM manuals seem to worthless as any manual can be and seems like this board doesn't have an active community like Arduino does. Any suggestions are welcome.

/**  ******************************************************************************  * @[member=File]    main.c  * @author  Ac6  * @[member=versions] V1.0  * @[member=Date]    01-December-2013  * @brief   Default main function.  *******************************************************************************/#include "stm32f4xx.h"#include "stm32f401_discovery.h"			void LedSetup(void)	{		GPIO_InitTypeDef GPIO_InitStructure;		/* Enable the GPIO LED clock */		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);		/* Configure the GPIO_LED pin */		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;		GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;		GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;		GPIO_Init(GPIOD, &GPIO_InitStructure);	}void ButtonSetup(void)	{		GPIO_InitTypeDef GPIO_InitStructure;		NVIC_InitTypeDef NVIC_InitStructure;		EXTI_InitTypeDef EXTI_InitStructure;		/* Enable GPIOA clock */		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);		/* Configure PA0 pin as input floating */		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;		GPIO_Init(GPIOA, &GPIO_InitStructure);		/* Enable GPIOA clock */		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);		/* Configure EXTI line0 */		EXTI_InitStructure.EXTI_Line = EXTI_Line0;		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;		EXTI_InitStructure.EXTI_LineCmd = ENABLE;		EXTI_Init(&EXTI_InitStructure);		/*Connect EXTI Line0 to PA0 pin*/		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);		/* Enable and set EXTI Line0 Interrupt to the lowest priority */		NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		NVIC_Init(&NVIC_InitStructure);	}int main(void){	/* LED initialization */	LedSetup();	/* Initialize button and interrupt */	ButtonSetup();	for(;;}/**  * @brief This function handles External line 0 interrupt request  * @param None  * @retval None */void EXTI0_IRQHandler(void){	int i, y;	y =1;	if(EXTI_GetITStatus(EXTI_Line0) != RESET)	{		if(y = 1)		{			while(1)			{				/* Empty cycles for led toggle */				for(i = 0; i < 5000000; i++);				STM_EVAL_LEDToggle(LED4);				y = 0;			}		}		if(y = 0)		{			while(1)			{				GPIO_ResetBits(GPIOD, GPIO_Pin_12);				y = 1;			}		}		/* Clear the EXTI line 0 pending bit */		EXTI_ClearITPendingBit(EXTI_Line0);	}}void HardFault_Handler(void){	while(1);}

One day I will be able to play Monster Hunter Frontier in French/Italian/English on my PC, it's just a matter of time... 4 5 6 7 8 9 years later: It's finally coming!!!

Phones: iPhone 4S/SE | LG V10 | Lumia 920 | Samsung S24 Ultra

Laptops: Macbook Pro 15" (mid-2012) | Compaq Presario V6000

Other: Steam Deck

<>EVs are bad, they kill the planet and remove freedoms too some/<>

Link to comment
Share on other sites

Link to post
Share on other sites

if(y = 1)

Will always return true. It is not a check, but an assignment. This is, I think, the major flaw in your code.

Same for if(y = 0)

 

I see you're using the std peripherals, which is good. Makes stuff more readable.

Are you sure you want the button to have no pulldown or pullup? Unless your board has these discretely, of course, this can cause undefined behavior.

 

You're also using while(1) loops without breaks. This is a bad idea because now the interrupt will never exit. It won't exit, and so it cannot be reentered anymore either. That's why it won't stop.

Link to comment
Share on other sites

Link to post
Share on other sites

 

if(y = 1)

Will always return true. It is not a check, but an assignment. This is, I think, the major flaw in your code.

Same for if(y = 0)

 

I see you're using the std peripherals, which is good. Makes stuff more readable.

Are you sure you want the button to have no pulldown or pullup? Unless your board has these discretely, of course, this can cause undefined behavior.

 

You're also using while(1) loops without breaks. This is a bad idea because now the interrupt will never exit. It won't exit, and so it cannot be reentered anymore either. That's why it won't stop.

 

 

ok, try to solved it this way:

/**  ******************************************************************************  * @[member=File]    main.c  * @author  Ac6  * @[member=versions] V1.0  * @[member=Date]    01-December-2013  * @brief   Default main function.  *******************************************************************************/#include "stm32f4xx.h"#include "stm32f401_discovery.h"short state = 1;int i;void LedSetup(void)	{		GPIO_InitTypeDef GPIO_InitStructure;		/* Enable the GPIO LED clock */		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);		/* Configure the GPIO_LED pin */		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;		GPIO_InitStructure.GPIO_Mode= GPIO_Mode_OUT;		GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;		GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;		GPIO_Init(GPIOD, &GPIO_InitStructure);	}void ButtonSetup(void)	{		GPIO_InitTypeDef GPIO_InitStructure;		NVIC_InitTypeDef NVIC_InitStructure;		EXTI_InitTypeDef EXTI_InitStructure;		/* Enable GPIOA clock */		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);		/* Configure PA0 pin as input floating */		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;		GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;		GPIO_Init(GPIOA, &GPIO_InitStructure);		/* Enable GPIOA clock */		RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);		/* Configure EXTI line0 */		EXTI_InitStructure.EXTI_Line = EXTI_Line0;		EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;		EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;		EXTI_InitStructure.EXTI_LineCmd = ENABLE;		EXTI_Init(&EXTI_InitStructure);		/*Connect EXTI Line0 to PA0 pin*/		SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);		/* Enable and set EXTI Line0 Interrupt to the lowest priority */		NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;		NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;		NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;		NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;		NVIC_Init(&NVIC_InitStructure);	}void LED(void){	if(state == 1)	{		for(i = 0; i < 5000000; i++);		STM_EVAL_LEDToggle(LED4);	}	else	{		GPIO_ResetBits(GPIOD, GPIO_Pin_12);	}}int main(void){	/* LED initialization */	LedSetup();	/* Initialize button and interrupt */	ButtonSetup();	for(;		{		/*LED Toggle on or off*/		LED();		}}/**  * @brief This function handles External line 0 interrupt request  * @param None  * @retval None */void EXTI0_IRQHandler(void){	if(EXTI_GetITStatus(EXTI_Line0) != RESET)	{		state = !state;		/* Clear the EXTI line 0 pending bit */		EXTI_ClearITPendingBit(EXTI_Line0);	}}void HardFault_Handler(void){	while(1);}

it works now; still feel uneasy on how i set it up;

the reason why i set up the button as nopull was i because, i think it was on the manual or the library, this way no values are hold on the pin of the button and it won't create errors on the interrupt

One day I will be able to play Monster Hunter Frontier in French/Italian/English on my PC, it's just a matter of time... 4 5 6 7 8 9 years later: It's finally coming!!!

Phones: iPhone 4S/SE | LG V10 | Lumia 920 | Samsung S24 Ultra

Laptops: Macbook Pro 15" (mid-2012) | Compaq Presario V6000

Other: Steam Deck

<>EVs are bad, they kill the planet and remove freedoms too some/<>

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×