cancel
Showing results for 
Search instead for 
Did you mean: 

New to STM32f4 Discovery and having some teething Issues

fredmelon
Associate
Posted on November 17, 2012 at 13:08

Hi All,

I'vebeen playing around with the STM32f4 Discovery board in Keils uVision.I'vebeen trying to get the ''user Button'' to turn on an LED when pressed. Its my understanding that afterivinitiated theperipheralsclocks for port A and D, This allows me to use the GPIOs on those ports. So iam trying to read from from the Input Data Register on GPIOA to see the status off the Button. Its my understanding that the code below should light up an LED when button is pressed, for some reason i get no response, can anyone point out my errors?

#include <
stm32f4xx.h
>
int main (void)
{
RCC->AHB1ENR = 0x9; //IO PortD clock enable / port A clock enable
//setup Port A ports
GPIOA->MODER = 0xA8000000; // Set Port a Bit 0 Active input (the user button is on PA0)
GPIOA->OTYPER = 0; 
GPIOA->OSPEEDR = 0; 
GPIOA->PUPDR = 0; 
// Port D setup 
GPIOD->MODER = 0x55000000; //Set GPIOD 12/13/14/15 as outputs PIN
GPIOD->OTYPER = 0; //Set output as Push-Pull mode
GPIOD->OSPEEDR = 0; //Set output speed 2MHz low speed
GPIOD->PUPDR = 0; //Set no pull up and pull down
GPIOD->ODR = 0x0000; // Turn off all LEDs
while(1)
{
if(GPIOA->IDR == 0x1)
{ 
GPIOD->ODR = 0x8000;
}
} 
}

Many Thanks, Steve #stm32f4
2 REPLIES 2
Posted on November 17, 2012 at 13:54

Unless you want to spend hours of your time coding and debugging register level minutia I'd recommend using the library to set things up.

int main(void)
{ 
RCC->AHB1ENR |= 0x9; //IO PortD clock enable / port A clock enable
//setup Port A ports
GPIOA->MODER &= ~3; // Set Port a Bit 0 Active input (the user button is on PA0)
GPIOA->PUPDR &= ~3; // step lightly 
// Port D setup
GPIOD->MODER = (GPIOD->MODER & ~0xFF000000) | 0x55000000; //Set GPIOD 12/13/14/15 as outputs PIN
GPIOD->OTYPER &= ~0xF000; //Set output as Push-Pull mode
GPIOD->OSPEEDR &= ~0xFF000000; //Set output speed 2MHz low speed
GPIOD->PUPDR &= ~0xFF000000; //Set no pull up and pull down
GPIOD->ODR = 0x0000; // Turn off all LEDs
while(1)
{
if(GPIOA->IDR & 0x1)
GPIOD->ODR |= 0x8000;
else 
GPIOD->ODR &= ~0x8000;
} 
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
fredmelon
Associate
Posted on November 17, 2012 at 14:09

Thank you Clive for the quick response, your solution works great !

I think ill be spending the rest of the day looking at the bitwise operators you have used to control the register and how to utilize the libraries to make my life easier.