Microcontroller Embedded C Programming Lecture 115| LED on exercise using bitwise shift operators

  • Post author:
  • Post category:Blog

 

LED on exercise using bitwise shift operators

 

 

In this article, let’s modify the LED_on exercise using bitwise shift operators. 

Let’s modify these below codes by using bitwise shift operators.

#include<stdint.h>

int main(void)
{
    uint32_t *pClkCtrlReg = (uint32_t*)0x40023830;
    uint32_t *pPortDModeReg = (uint32_t*)0x40020C00;
    uint32_t *pPortDOutReg = (uint32_t*)0x40020C14;

    //1. enable the clock for GPOID peripheral in the AHB1ENR
    *pClkCtrlReg |= 0x08;

    //2. configure the mode of the IO pin as output
    //a. clear the 24th and 25th bit positions (CLEAR)
    *pPortDModeReg &= 0xFCFFFFFF;
    //b. make 24th bit position as 1 (SET)
    *pPortDModeReg |= 0x01000000;

    //3.SET 12th bit of the output data register to make I/O pin-12 as HIGH
    *pPortDOutReg |= 0x1000;

    while(1);
}

 

Let’s check the first step.  Enabling the clock for the GPIOD peripheral in the AHB1ENR register. 

So, on the AHB1ENR we modify the 3rd-bit position.

Figure 2. RCC AHB1 peripheral clock enable register
Figure 1. RCC AHB1 peripheral clock enable register

 

Here in the first comment line, we can write like this ‘SET the 3rd bit position’.

And change the code like *pClkCtrlReg |= (1 << 3);  as shown below.

//1. enable the clock for GPOID peripheral in the AHB1ENR (SET the 3rd bit position)
*pClkCtrlReg |= ( 1 << 3);

SET the 3rd bit position

 

Then do the second step. Here, we have to clear the 24th and 25th-bit positions. 

So, we can do *pPortDModeReg &= ~(1 << 24); //You have to negate this(1<<24). 

Negating is very important. Otherwise, you will 0 out of all other bit positions. So, negation is very important during clearing. 

And for the 25th you can do *pPortDModeReg &= ~(1 <<25);

//2. configure the mode of the IO pin as output
//a. clear the 24th and 25th bit positions (CLEAR)
*pPortDModeReg &= ~( 1 << 24);
*pPortDModeReg &= ~( 1 << 25);

Clear the 24th and 25th bit positions

 

But instead of using 2 lines, you can do it in 1 line.

What you can do is, instead of using 1, here you can use 3. The binary value of three is 11. So, it’s like pushing 11 to the 24th and 25th-bit positions. 

//2. configure the mode of the IO pin as output
//a. clear the 24th and 25th bit positions (CLEAR)
*pPortDModeReg &= ~( 3 << 24);

 

LED on exercise using bitwise shift operators
Figure 2. GPIO port mode register

 

In the mode register, you have to clear the 24th and 25th. So, what you do is, take the number 3 and left shift this by 24. When you do that, what happens? 3 will come and settle here(MODER12). 

What is 3?  3 means in binary it is 11 and then you are negating this, it will become 00 and all others become 11. That’s why you can use 3 here.

Next, make the 24th bit position as 1(SET). 

//b. make 24th bit position as 1 (SET)
*pPortDModeReg |= ( 1 << 24);

 

The third step is to SET the 12th bit of the output data register to make I/O pin-12 as HIGH. 

//3.SET 12th bit of the output data register to make I/O pin-12 as HIGH
*pPortDOutReg |= ( 1 << 12);

 

After the modification just rechecks and compile this code. I just checked this code, yes, the LED is indeed turning on.

That’s about the applicability of bitwise shift operators, and we also need to understand one more thing, which is bit extraction. I will cover the Bit extraction in the following article.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.