Stuck with basic programming of STM32L-Discovery

1.1k Views Asked by At

I've got a STM32L-Discovery Board, which has got a STM32L152R8 microprocessor. I'm quite stuck trying to make basic things work.

I've looked the examples given by ST (the current consumption touch sensor and the temperature sensor), and I think they aren't user-friendly, with so many libraries, sub-processes and interrupts, that make the code really difficult to understand.

I've tried to turn on the blue LED (GPIO PB6), but I can't manage to do that.

My code compiles correctly but does nothing to the board. This is the code of "main.c".

RCC->AHBRSTR = 0x00000002;
RCC->AHBRSTR = 0x00000000;
RCC->AHBENR = 0x00000002;
GPIOB->MODER  = 0x00001000;
GPIOB->OTYPER   = 0x00000040;
GPIOB->OSPEEDR = 0x00001000;
GPIOB->PUPDR = 0x00000000;
GPIOB->ODR   = 0x00000040;
while(1) {}

Am I missing something? Could I find really basic examples somewhere?

Thanks in advance!

2

There are 2 best solutions below

1
On

The standard peripheral library that ST supplies on their website is a good starting point. It has examples on programming a GPIO. Note that their code is absolutely horrible, but at least it works and is something to start with.

What compiler/debugger are you using? If you are using IAR, then you can view the GPIO registers while stepping thru the code. Please post the values of the GPIO registers to your question and maybe we can help.

0
On
  1. RCC->AHBENR = 0x00000002;

Change to "RCC->AHBENR |= 0x00000002;" This will ensure you enable GPIOB without disabling everything else. The existing code will disabled important things like the flash memory controller and all the other GPIOs.

  1. GPIOB->MODER = 0x00001000; // This will set pin 6 as output, and all other pins as input. Was this your intent?

Change to "GPIOB->MODER = (GPIOB->MODER & 0xFFFFDFFF ) | 0x00001000;" This will set pin 6 as an output without changing the configuration of any other pins.

  1. GPIOB->OTYPER = 0x00000040; // This will set the output type as open drain, meaning you can only pull the line down.

Change to "GPIOB->OTYPER |= 0x00000040;" Set output as push-pull instead of open drain. You later code attempts to set this line high which will not work as an open drain output will pull to ground or allow the line to float. A push-pull output will allow you to set the line high or low.