cancel
Showing results for 
Search instead for 
Did you mean: 

Bitwise shift left STM32F723

StanJerm
Associate II

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

8 REPLIES 8
Johi
Senior III

 

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.

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.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

wouldn't it be more simple to write this:

GPIOC->MODER |= (1<<6) | (1 << 7);  ??

thx u so much ! 

(1 << 6) is not the same as (2 << (2 * 6)). Try it with calculator, or better, with pencil and paper.

JW

Pavel A.
Evangelist III

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 ))

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

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

> 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