cancel
Showing results for 
Search instead for 
Did you mean: 

How to turn on a LED modyfing registers directly?

FGiai.1
Associate II

Hi to all,

I'm trying to use the Nucleo F446RE board to switch on an external LED. by using directly the registers

IN particular, I have connected the pin PA5 in series with a resistor, a LED and finally with the GND of the board.

The code uses is shown before. Initially I have activated the AHB1 internal pheripheral. Then, I enabled and forced to HIGH the pin PA5 of the board.

int main(int argc, char* argv[])
{
	/** AHB1 **/
	volatile uint32_t *RCC_AHB1ENR = 0x0;
	RCC_AHB1ENR = (uint32_t*)(0x40023800 + 0x30);
	*RCC_AHB1ENR = *RCC_AHB1ENR | 0x1;
 
	/** GPIOA **/
	volatile uint32_t *GPIOA_MODER = 0x0, *GPIOA_ODR = 0x0;
	GPIOA_MODER = (uint32_t*)0x40020000;
	*GPIOA_MODER = *GPIOA_MODER | 0x400;
	GPIOA_ODR = (uint32_t*)(0x40020000 + 0x14);
	*GPIOA_ODR = *GPIOA_ODR | 0x20;
	
	/** Infinite loop **/
	while (1);
}

The code is correctly compiled and loaded on the board, bu the problem is that the LED doesn't turn on. Can someome suggest how to modify the code? There are some other registers to modify? Something else to do?

Thank you very much in advance.

13 REPLIES 13

Code looks good.

Don't you have some debugger installed, so that you can observe the registers?

Isn't the LED reverse polarized? Can you measure the pin's voltage using multimeter?

JW

KnarfB
Principal III

Pin PA5 is already connected to the green LED on that board. You should not need to add another LED. Your code works on my copy of that board.

I have no a debugger installed yet. I'm just I'm just studying and Learning the basics, so I wanted to start coding. THe LED is not reverse polarized, but I cannot verify because I have no a multimiter.

Thank you for the answer. I think the code is correct, I have to verify why it doesn't work in some way.

FGiai.1
Associate II

Yes I know that PA5 is connected to the green led of the board, but in this case I'm using as a general purpose output, just to try to code a very simple program.

What IDE do you use? There is some trick that can make the code not working during the flashing?

Yes I know that PA5 is connected to the green led of the board, but in this case I'm using as a general purpose output, just to try to code a very simple program.

I already tried to change Pin but nothing changed. What IDE do you use? There is some trick that can make the code not working during the flashing?

I'm using STMCubeIDE. Maybe you start with a C code example. Code can be generated by STM32CubeIDE. You only add few lines. There is a short intro video https://youtu.be/eumKLXNlM0U. Here are some more examples, but for a different board: https://gitlab.com/stm32mcu/wiki/-/wikis/home

@FGiai.1​ 

How exactly do you compile the program? And how exactly do you "burn" the binary into the board?

@KnarfB​ ,

Can you please post the proven working binary/hex?

JW

Good idea, Jan.

@FGiai.1​ The file must be unzipped first. it is a .bin file which I cannot upload directly. You can drag the .bin file onto the "USB Drive" that shows up when the NUCLEO board is connected. That will flash the µC and switch the LED on.

Here is the complete project. It uses @FGiai.1​ code as main() in a cube generated project. The only difference to HAL code (not used here) I see is, that HAL code reads *RCC_AHB1ENR back to a volatile dummy variable after the write. Didn't find a hint in the ref man that this is needed.