cancel
Showing results for 
Search instead for 
Did you mean: 

Can't get simple blinkylights to work

johnjohn9105
Associate III
Posted on September 20, 2012 at 01:42

After figuring out how to configure ARM GCC and Eclipse, I wrote the shortest demo program I could come up with. My program is supposed to light one LED when the user button is pressed, and the other LED when the button is not pressed.

You can probably figure that since I'm posting here, my code isn't working. Neither LED lights up. I'm probably missing something very simple but I can't figure it out. My code is below; I simplified it as much as possible. I've verified in the debugger that I'm hitting one of the GREEN_LED_PORT->BSRR = lines when the button is pressed, and the other when the button is not pressed, yet neither LED illuminates. Any assistance would be greatly appreciated.


/* Includes ------------------------------------------------------------------*/

#include ''stm32f0xx.h''


/* Private define ------------------------------------------------------------*/

#define GREEN_LED_PIN GPIO_Pin_9

#define GREEN_LED_PORT GPIOC

#define BLUE_LED_PIN GPIO_Pin_8

#define BLUE_LED_PORT GPIOC

#define USER_BUTTON_PIN GPIO_Pin_0

#define USER_BUTTON_PORT GPIOA

#define GREEN_LED_PERIPH_CLOCK RCC_AHBPeriph_GPIOC

#define BLUE_LED_PERIPH_CLOCK RCC_AHBPeriph_GPIOC

#define USER_BUTTON_PERIPH_CLOCK RCC_AHBPeriph_GPIOA


#define USER_BUTTON_IS_PRESSED (USER_BUTTON_PORT->IDR & USER_BUTTON_PIN)


int
main(
void
)

{

//Set up the pins for the LEDs and user button

GPIO_InitTypeDef GPIOInit;

GPIOInit.GPIO_Mode = GPIO_Mode_OUT;

GPIOInit.GPIO_OType = GPIO_OType_PP;

GPIOInit.GPIO_Pin = GREEN_LED_PIN;

GPIOInit.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIOInit.GPIO_Speed = GPIO_Speed_Level_3;

GPIO_Init(GREEN_LED_PORT, &GPIOInit);


GPIOInit.GPIO_Pin = BLUE_LED_PIN;

GPIO_Init(BLUE_LED_PORT, &GPIOInit);


GPIOInit.GPIO_Mode = GPIO_Mode_IN;

GPIOInit.GPIO_Pin = USER_BUTTON_PIN;

GPIO_Init(USER_BUTTON_PORT, &GPIOInit);


//enable clocking to the GPIO ports

RCC_AHBPeriphClockCmd(GREEN_LED_PERIPH_CLOCK, ENABLE);

RCC_AHBPeriphClockCmd(BLUE_LED_PERIPH_CLOCK, ENABLE);

RCC_AHBPeriphClockCmd(USER_BUTTON_PERIPH_CLOCK, ENABLE);


//loop forever

while
(1)

{

if
(USER_BUTTON_IS_PRESSED)

{

GREEN_LED_PORT->BSRR = (GREEN_LED_PIN << 16) + BLUE_LED_PIN;

}

else

{

GREEN_LED_PORT->BSRR = (GREEN_LED_PIN << 16) + BLUE_LED_PIN;

}

}

}

3 REPLIES 3
johnjohn9105
Associate III
Posted on September 20, 2012 at 02:09

I found the cause of the problem. It appears that I need to enable the clock to the GPIO peripherals before I initialize the GPIO pins. Moving the code around solved the problem.

Posted on September 20, 2012 at 02:10

Enable clocks for GPIO units before initializing the pins.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on September 20, 2012 at 02:17

Crossed on the wire there

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..