Skip to content

After blinking the LEDs. I started with the chapter register and on the first page itself I got some issues. I read about most of them and find out that the address is const and to use that fixed value we used raw pointer GPIOE_BSRR as *mut u32 which is making a copy of the address(referencing) variable and allowing us to change. #353

Answered by adamgreig
NitinSaxenait asked this question in Q&A
Discussion options

You must be logged in to vote

but why 1 in every shift?

It's a convenient way to express the full binary number we want to write to the register. The way the BSRR register works is you write a '1' to any of the 'set' fields (each 1 bit wide, bits 0-15) to turn on that GPIO, and write a '1' to any of the 'reset' fields (bits 16-31) to turn it off.

So, to turn on pin 2, you write a 1 to bit 2, which means writing 0b0000_0000_0000_0000_0000_0000_0000_0100 to the register. Instead of typing out the long number, we can write 1 << 2, which is the same number but easier to understand. We're shifting a 1 to the left by 2 places. Because all the fields can only take a '1', we never need any other number. To turn it off we'd …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@NitinSaxenait
Comment options

@NitinSaxenait
Comment options

Answer selected by NitinSaxenait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #351 on June 21, 2021 10:49.