cancel
Showing results for 
Search instead for 
Did you mean: 

Why the led 'PA1' turns on after switching 'on' a register "*GPIOA_MODER" and turns 'off' after the switching 'on' a register "*GPIOA_ODR"?

DK.7
Senior

I'm studying a registers...

I have stm32407vg and the LED is on the 'PA1' and I need that the LED turned 'On' after command witch It should turn it 'ON'. In may case after line № 18.

But 'LED' turns 'ON' after line №16 and turns 'OFF' after line № 18

Could you tell me why it happens?

Below my code.

#include <stdint.h>
/*
#if !defined(__SOFT_FP__) && defined(__ARM_FP)
  #warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
*/
int main(void)
{
	uint32_t *RCC_AHB1ENR = (uint32_t*)0x40023830; // Base address + Adress ofset=0x30 from table "RCC AHB1 peripheral clock register (RCC_AHB1ENR)"
	uint32_t *GPIOA_MODER = (uint32_t*)0x40020000;// Base address + Adress ofset=0x00 from table "GPIO port mode register (GPIOx_MODER) (x = A..I/J/K)"
	uint32_t *GPIOA_ODR = (uint32_t*)0x40020014;// Base address + Adress ofset=0x14 from table "GPIO port output data register (GPIOx_ODR) (x = A..I/J/K)"
 
	// 1. Enable the clock for GPIOA peripheral in the AHB1ENR
	*RCC_AHB1ENR |= 1<<0; 
	// 2. Configure the mode of the IO pin a output
	*GPIOA_MODER |= 0<<3 | 1<<2;  
	// 3. SET 1th bit of the uotput data register to make I/O pin-1 a HIGH
	*GPIOA_ODR |= 1<<1;  
	/* Loop forever */
	while(1){
	       // for(uint32_t i=0; i < 300000; i++ );
	       // *GPIOA_ODR |= 1<<0;
	       // for(uint32_t i=0; i < 300000; i++ );
	       //*GPIOA_ODR &= ~(1<<0);
	    }
}

5 REPLIES 5

Is the LED connected cathode to PA1 and anode through resistor to VCC?

JW

DK.7
Senior

How can I find out it?

Here is a link to my stm32f407vg https://stm32-base.org/boards/STM32F407VGT6-STM32F4XX-M.html#User-LED

TDK
Guru

You should use the CMSIS header files rather than recreating your own. It will make your program more readable and avoid mistakes.

One mistake here is that you do not quality the address as volatile, which means the compiler is allowed to optimize out the access if there are no other consequences.

PA1 needs to be low in order to turn on the LED.

0693W00000D1PJWQA3.png

If you feel a post has answered your question, please click "Accept as Solution".

Thank you answer! Of course I will use the CMSIS header files...

Question about "volatile" Is it correct in my case?

uint32_t *RCC_AHB1ENR = (*(volatile*)0x40023830);

No.

volatile uint32_t * RCC_AHB1ENR = (volatile uint32_t *) 0x40023830;

Just use the header files instead.

If you feel a post has answered your question, please click "Accept as Solution".