2023-08-25 11:20 PM - last edited on 2023-08-30 09:30 AM by Amel NASRI
Hi there i m a student working on STM32F723 the first time, I encountered an register setting example as follows:
GPIOC->MODER |= ((2<<(6*2)) \ (2<<(7*2))); i konw its setting the GPIO register but whats the meaning of * and 2<< at the beginning sentence? and what does bit does it set?
GPIOC-> AFR[0] |= ((8<<(6*4)) | (8<<(7*4))); whats the meaning of 8 and 4 means? and whats bits it set?
thanks in advance.
Stanley
2023-08-25 11:44 PM - edited 2023-08-25 11:47 PM
Hello,
This is related to the syntax of the C language.
https://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/
8 is a representation of 1000 binary. And << shifts the bits left. (6*4) = 24.
You will find the details in a good C course; knowledge of C is essential if you want to work with complex devices as STM32 microcontrollers.
2023-08-26 01:45 AM
The *2 is the bit width of the MODE option, the 6 is the pin number or index, the 2 is the MODE for the pin.
The code will be folded and optimized by the compiler, it's done verbosely so you can see how it was arrived at without some unexplained constant.
The AFR options are 4 bits wide taking a value between 0 and 15, determining what functional connection is mapped to a pin.
The 8 is AF8, the part Data Sheet/Manual should have a list of tables showing what peripheral maps at that slot.
2023-08-26 07:37 AM
wouldn't it be more simple to write this:
GPIOC->MODER |= (1<<6) | (1 << 7); ??
2023-08-26 07:43 AM
thx u so much !
2023-08-26 11:59 PM
(1 << 6) is not the same as (2 << (2 * 6)). Try it with calculator, or better, with pencil and paper.
JW
2023-08-27 04:15 AM
There's a lot of junk code written by ..er... budding electronic engineers. If you don't understand it and think it could be clearer/laid out better, fix it (but test your understanding first!). Don't skip your C lectures ))
2023-08-27 05:53 AM
Yes, and ORing them on does make an assumption that the current bits are zero.
Coding like this does require a full awareness of the register definitions in the Reference Manual
2023-08-27 03:02 PM
> Coding like this does require a full awareness of the register definitions in the Reference Manual
Yes, but everybody reads the RM thoroughly before them starting programming, right?
Right?
JW