AnsweredAssumed Answered

Can't get simple blinkylights to work

Question asked by mucker.matthew on Sep 20, 2012
Latest reply on Sep 20, 2012 by Clive Two.Zero
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.

01./* Includes ------------------------------------------------------------------*/
02.#include "stm32f0xx.h"
03. 
04./* Private define ------------------------------------------------------------*/
05.#define GREEN_LED_PIN             GPIO_Pin_9
06.#define GREEN_LED_PORT            GPIOC
07.#define BLUE_LED_PIN              GPIO_Pin_8
08.#define BLUE_LED_PORT             GPIOC
09.#define USER_BUTTON_PIN           GPIO_Pin_0
10.#define USER_BUTTON_PORT          GPIOA
11.#define GREEN_LED_PERIPH_CLOCK    RCC_AHBPeriph_GPIOC
12.#define BLUE_LED_PERIPH_CLOCK     RCC_AHBPeriph_GPIOC
13.#define USER_BUTTON_PERIPH_CLOCK  RCC_AHBPeriph_GPIOA
14. 
15.#define USER_BUTTON_IS_PRESSED (USER_BUTTON_PORT->IDR & USER_BUTTON_PIN)
16. 
17.int main(void)
18.{
19.    //Set up the pins for the LEDs and user button
20.    GPIO_InitTypeDef    GPIOInit;
21.    GPIOInit.GPIO_Mode = GPIO_Mode_OUT;
22.    GPIOInit.GPIO_OType = GPIO_OType_PP;
23.    GPIOInit.GPIO_Pin = GREEN_LED_PIN;
24.    GPIOInit.GPIO_PuPd = GPIO_PuPd_NOPULL;
25.    GPIOInit.GPIO_Speed = GPIO_Speed_Level_3;
26.    GPIO_Init(GREEN_LED_PORT, &GPIOInit);
27. 
28.    GPIOInit.GPIO_Pin = BLUE_LED_PIN;
29.    GPIO_Init(BLUE_LED_PORT, &GPIOInit);
30. 
31.    GPIOInit.GPIO_Mode = GPIO_Mode_IN;
32.    GPIOInit.GPIO_Pin = USER_BUTTON_PIN;
33.    GPIO_Init(USER_BUTTON_PORT, &GPIOInit);
34. 
35.    //enable clocking to the GPIO ports
36.    RCC_AHBPeriphClockCmd(GREEN_LED_PERIPH_CLOCK, ENABLE);
37.    RCC_AHBPeriphClockCmd(BLUE_LED_PERIPH_CLOCK, ENABLE);
38.    RCC_AHBPeriphClockCmd(USER_BUTTON_PERIPH_CLOCK, ENABLE);
39. 
40.    //loop forever
41.    while (1)
42.    {
43.        if (USER_BUTTON_IS_PRESSED)
44.        {
45.            GREEN_LED_PORT->BSRR = (GREEN_LED_PIN << 16) + BLUE_LED_PIN;
46.        }
47.        else
48.        {
49.            GREEN_LED_PORT->BSRR = (GREEN_LED_PIN << 16) + BLUE_LED_PIN;
50.        }
51.    }
52.}

Outcomes