cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F411 - common short questions ...

timbo2023
Associate III

Good Evening Ladies and Gentlemen,

 

A short introducing purpose of mine:

 

int main(void)
{
RCC->AHB1ENR |= 0xFC;             // ---> Here I would like to open the Port clock
RCC->APB1ENR |= 0xFC;             // ---> Here I would like to open the USART2 clock - in this context not relevant

GPIOA->MODER |= 0x55555555;       // ---> Here I define Port A as output
GPIOC->MODER |= 0x55555555;       // ---> Here I define Port C as output

GPIOA->ODR |= (1<<2);             // ---> Here I would like to set PA2 "high"

GPIOC->ODR |= (1<<13);            // ---> Here I would like to set the onboard LED (PC13) "high"

while(1) {
GPIOA->ODR |= (1<<0);             // Here I would like to "toggle" PA0 and PA6  
GPIOA->ODR |= (1<<6);
delayMs(50);                      // It only is a not relevant "delay" for this purpose

GPIOA->ODR &= ~(1<<0);
GPIOA->ODR &= ~(1<<6);
delayMs(50);
}
}

 

 

But something went wrong - it does not work - Do you have any suggestions especially regarding a simple "toggling"?

It should be a pre work for a stepper motor controlling.

 

Best regards

~TIMBO~

35 REPLIES 35

But as result my control led PC13 LED is not glowing, in look to the code earlier, not in this context...

gbm
Lead III

Your code above sets the PC13 output high, so the LED is off. There is no code toggling PC13 in the loop.

Using magic numbers is usually called "a complete mess", not "conventional way".

> when I set A6 - it is the 5th entry of the mode register, therefore pin 10

I don't know what is A6 and pin 10.

Pins in ports are numbered from 0 (i.e. PA0, PA1, ... PA6). In GPIOA_MODER, the bitfield for PA6 are bits 12 and 13.

JW

 

 

Attached the output situation -

my aim is only to toggle PA0 - and to set the other ones.

 

It sounds easy, the reality looks in another way...

But PC13 is glowing now ... thank you for this discussion input.

 

Back to PA0, I would like to toggle this output Pin for a stepper motor control.

 

 

TDK
Guru

This initializes as output and toggles PA0:

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
GPIOA->MODER &= ~(0b11 << 0);
GPIOA->MODER |= 0b01 << 0;
while (1) {
    GPIOA->BSRR = 1 << 0;
    HAL_Delay(50);
    GPIOA->BSRR = 1 << 16;
    HAL_Delay(50);
}

 

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

Is it not possible to toggle the ODR register?

 

 

Sure it's possible. Using BSRR avoids RMW access.

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

Great answer guru TDK -

RMW access - but what means that?

RMW=Read-Modify-Write, for example

 

GPIOA->ODR |= (1<<0);

 

means "read ODR, perform OR with (1<<9) (i.e. modify), write result to ODR".

Why does it matter? Because it makes time for an interrupt to kick in between read and write, and if that interrupt modifies the same register, then returns, and the subsequent write destroys what the interrupt wrote.

JW

Excuse me, I do not understand it.