Jump to content
sirbow2

Can't get button to turn on LED with STM32f0 :(

Recommended Posts

im finally starting to get into the programming of my STM32f0Discovery board and i cant quite get my button to work. code:

 

#include "stm32f0xx.h"

int main(void);

int main(void){
int i;

// Enable clock for GPIOC
RCC->AHBENR |= (1 << 19);
GPIOC->MODER |= (1 << 16);//Set pin C8 as output
GPIOC->OTYPER &= ~(1 << 8);//Set pin 8 output as internal push-pull


GPIOC->MODER |= (1 << 18);//Set pin C9 as output
GPIOC->OTYPER &= ~(1 << 9);//Set pin 9 output as internal push-pull

// enable user PA0 button
RCC->AHBENR |= (1 << 17); /* Enable GPIOA clock */
GPIOA->PUPDR |= (1 << 0); /* PA.0 is Pull up */

// GPIOC->BSRR |= (1 << 24);//Turn Pin C8 off
// GPIOC->BSRR |= (1 << 8);//Turn C8 on
//GPIOC->BSRR |= (1 << 9);//Turn C9 on
//GPIOC->BSRR |= (1 << 25);//Turn Pin C9 off
while(1)
{
//This code turns on and off LEDs via the button, cant get this to work. it is always LOW
if((GPIOA->IDR & (1 << 0))==(1 << 0)) //read pin state
{
//High
GPIOC->BSRR |= (1 << 8);//Turn C8 on
GPIOC->BSRR |= (1 << 25);//Turn Pin C9 off
}
else
{
//Low
GPIOC->BSRR |= (1 << 9);//Turn C9 on
GPIOC->BSRR |= (1 << 24);//Turn Pin C8 off
}

}
}

either if((GPIOA->IDR & (1 << 0))==(1 << 0)) isnt properly getting the button value or RCC->AHBENR |= (1 << 17); GPIOA->PUPDR |= (1 << 0); isn't setting up the input correctly.

 

I do know that the LEDs work this code:

#include "stm32f0xx.h"
int main(void);
int main(void){
int i;

// Enable clock for GPIOC
RCC->AHBENR |= (1 << 19);
GPIOC->MODER |= (1 << 16);//Set pin C8 as output
GPIOC->OTYPER &= ~(1 << 8);//Set pin 8 output as internal push-pull


GPIOC->MODER |= (1 << 18);//Set pin C9 as output
GPIOC->OTYPER &= ~(1 << 9);//Set pin 9 output as internal push-pull

// enable user PA0 button
RCC->AHBENR |= (1 << 17);	 /* Enable GPIOA clock	 */
GPIOA->PUPDR |= (1 << 0);	 /* PA.0 is Pull up	 */
// GPIOC->BSRR |= (1 << 24);//Turn Pin C8 off
// GPIOC->BSRR |= (1 << 8);//Turn C8 on
while(1)
{
//This is the LED blink code- blinks the two LEDs on and off----------------------------------------------------
for(i = 1000000; i >0; i--)
{
 i++;
 i--;
} //A short delay

GPIOC->BSRR |= (1 << 9);//Turn C9 on
GPIOC->BSRR |= (1 << 24);//Turn Pin C8 off

for(i = 1000000; i >0; i--)
{
 i++;
 i--;
} //A short delay

GPIOC->BSRR |= (1 << 8);//Turn C8 on
GPIOC->BSRR |= (1 << 25);//Turn Pin C9 off
}
}

 

code from page 129 + or - a few pages: http://www.st.com/in.../DM00031936.pdf

Share this post


Link to post
Share on other sites

Is there any reason you are not using the API that came with the board? Also you are turning on different LEDs in each of your conditions, not the same one.

 

Make the button detect code:

((GPIOA->IDR & 0x1) == 0)

 

I'll fire up mine to have a look.

Share this post


Link to post
Share on other sites

i figured it will be helpful in the future to know how to do this stuff, and not to use "libraries" of some sort.

 

ill try that, thanks

 

yes, i know, one will be on, one will be off when button is pressed, and the opposite if not pressed.

Share this post


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

×