2024-01-09 12:05 PM
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~
2024-01-11 05:37 AM
But as result my control led PC13 LED is not glowing, in look to the code earlier, not in this context...
2024-01-11 05:45 AM
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".
2024-01-11 05:54 AM
> 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
2024-01-11 06:05 AM
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.
2024-01-11 06:30 AM
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);
}
2024-01-11 06:57 AM
Is it not possible to toggle the ODR register?
2024-01-11 06:59 AM
Sure it's possible. Using BSRR avoids RMW access.
2024-01-11 07:01 AM
Great answer guru TDK -
RMW access - but what means that?
2024-01-11 08:13 AM - edited 2024-01-11 08:14 AM
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
2024-01-11 08:16 AM
Excuse me, I do not understand it.