Jump to content
43oh

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...