Microcontroller Embedded C Programming Lecture 128| LED toggle using software delay

  • Post author:
  • Post category:Blog

 

LED toggle using software delay

 

 

Exercise

  • Write a LED toggle program by creating a software delay between the LED on and LED off. 
  • LED should continuously toggle with a certain time delay forever.

 

We have to convert this program into an LED toggle program. After that, we have to introduce a small human observable delay and we are going to create the delay by using a loop statement. You can either use a while loop or for loop. It doesn’t matter actually. So, I’m using a for loop to create a delay.

/introduce small human observable delay
//This loop executes for 10K times
for(uint32_t i=0 ; i < 10000 ; i++ ); ;//empty body
}

 

Basically, we have to trap the processor for a certain amount of delay inside a loop. So, let’s use the ‘for’ loop. 

How to create the delay? 

Let’s create one variable and let’s keep incrementing that variable until it increases to a certain number. For that, let’s create a local variable. So, a local variable you can create outside the loop or you can also create here in the very first block of the for loop. 

Let’s create a variable uint32_t i = 0;

And after that let’s give the condition. That is, a conditional expression ‘i’ is less than 10K.

So, i < 10000 and i++. This loop executes 10K times. When ‘i’ becomes 10K this for loop breaks. That’s how this loop is going to introduce some small amount of delay because the processor will be looped inside this ‘for’ loop for 10K times.

Instead of creating an empty body inside the for loop, you can keep the semicolon after the for loop.  So, both are identical codes. 

for(uint32_t i=0; i<10000; i++);  Let’s use this syntax that would be better because the code looks compact. 

But we are not sure whether 10K times of looping will result in human observable delay or not.  So, we have to fix that by using the trial and error method. 

 

Now,  after the delay, we have to turn off the LED. For that, we have to reset the 12th bit of the GPIO port D output data register.

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

    //introduce small human observable delay
    //This loop executes for 10K times
    for(uint32_t i=0 ; i < 10000 ; i++ );

    //Tun OFF the LED
    *pPortDOutReg &= ~( 1 << 12);

    for(uint32_t i=0 ; i < 10000 ; i++ );
}
LED toggle using software delay
Figure 1. Code

Let’s use PortDOutReg.

How do you reset? 

PortDOutReg &= ~(1 << 12);

And after this again we have to introduce delay. Let’s give the for loop statement here, as shown in Figure 1.  This will toggle the LED, but the LED should toggle forever. That’s why we have to keep all these instructions inside an infinite loop. 

So, let’s use a while loop. For that, while(1) will create an infinite loop or you can also use for loop.  If you want to use a for loop, you have to write like for(;;). This creates an infinite loop using for loop, but I would stick with the while loop. We have to keep all those 4 statements.  So, the LED will toggle forever. So, you created LED toggling application.

 

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.