Shift Operator example with ODR in STM32F4 StdPeriph

Shift Operator example with ODR in STM32F4 StdPeriph

Hello everyone, in this article we are going to talk about shift operators in STM32F4 and StdPeriph with C language. We are going to use ODR register of STM32F429ZI Nucleo board and make a floating LED application.

Let's get started.

In this example we are going to make a simple floating LED application. In here we are going to use the D port of the board. I did not want the pins to be connected to any other things on the board. After it I will manage the LED output with the shift register operator. I floated the ODR register value one by one so the LEDS.
Below image you can see the schematic of the example.
Below Schematic was created at Altium Designer.
STM32F4 Floating LED Example Schematic

As you can see above schematic I have made the connections. Now I will write some codes to perform.

First I need to activate required GPIOD periphal bus and I need to activate required pins.

Below code block will activate the required GPIO bus. I will enable the GPIO bus here.

And I need to activate GPIO with specified configurations.

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_OUT; 
GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; 
GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_100MHz; 
GPIO_Init(GPIOD, &GPIO_InitStructure); 
I set the PD0 PD7 pins as outputs. I will send the LOGIC HIGH or LOGIC LOW data. With these datas LEDs will be activated or deactivated.

Until here our configurations are ready. We are going to perform the register data shifting in here.

I will use below delay function to make the processor wait. You can use any methods to dela the cycle.

void delay(uint32_t sure)
	uint32_t counter;
	for(int i=0; i < sure; i++){

I strongly recommend to take a look at if you do not have any ideo ODR and IDR registers of STM32 GPIO.

In here first we are going to activate first bit of the GPIOD port and this will activate the first pin and first LED. And then I will shift the bits of the ODR of the related GPIO port one by one with shift operator. When I reach the end of register data then I will come back at this time. I am going to use for loop.
Below code block will do that.

		GPIOD->ODR = 0x01; // 0b0000 0001
		for(uint16_t i = 0;i < 8; i++)
			GPIOD->ODR = GPIOD->ODR << 1;
		for(uint16_t i=8; i>0; i--)
			GPIOD->ODR = GPIOD->ODR >> 1;
As you can see above we have two for loops. One of them will go the forward and the other one will go back. I also use the delay function to see the changings. Do not forget it :)
Below image you can see the picture f the example. STM32F429 Floating LED Example Application Picture

You can also reach the working video on youtube:

You can also reach the source code of this example on Github :

That is all in this article.

Have a great shifting data.

Burak Hamdi TUFAN


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!


  • There is no comment. Be the owner of first comment...