how does variable store in memory address

Thread Starter

vead

Joined Nov 24, 2011
629
Hello
here is c program for LED blinking. I have tested this code
Code:
   #include<regx51.h>
    #define LED               P2
    #define LED_ON       0xff
    #define LED_OFF     0x00

    void delay (unsigned int i)
    {
        unsigned int j;
        for (j = 0; j < 40000; j++);
        {
        
        }
    }

    void main()
    {
    
      while (1)
        {
            LED = LED_ON;
            delay(500);
            LED = LED_OFF;
            delay(5000);
            LED = LED_OFF;
        
        }
    }
I don't understand this part of code
Code:
 void delay (unsigned int i)
    {
        unsigned int j;
        for (j = 0; j < 40000; j++);
        {
       
        }
I understand j is variable. how does this part of code work on microcontroller ?
 

AlbertHall

Joined Jun 4, 2014
11,969
It starts with j at zero and adds one to it repeatedly until j gets to 39999 and then stops. It just wastes some time resulting in a delay.
 

WBahn

Joined Mar 31, 2012
26,398
It's the most basic kind of delay loop (hence the name of the function).

Note that the pair of braces after it are meaningless and are not even part of the loop. The person that wrote the code might or might not know that; they might have put them in there as a visual clue that the loop is empty. It's not very good style because it lends itself to two problems. First, if they decide to do something in that loop, such as print out the value of j to verify that things are working, anything in the braces is not part of the loop no matter how it might look. Second, someone reading the code (including many compilers) will conclude that the semicolon after the for statement is a logic error and be tempted to remove it (or throw a warning, in the case of a compiler).

Also, note that the function takes an argument, i, but then doesn't use it. Yet look at where the function is called -- it is called with two different values for i; presumably the intent is that the delay caused by the second loop is ten times as long as the delay caused by the first, yet both calls will produce the exact same delay. A better way to write this function would be

Code:
void delay (unsigned int i)
{
   unsigned int j;
   for (j = 0; j < i; j++)
   {
       // EMPTY LOOP
   }
}
I actually have a #define for this case

Code:
#define EMPTY_LOOP

void delay (unsigned int i)
{
   unsigned int j;
   for (j = 0; j < i; j++)
      EMPTY LOOP;
}
 

WBahn

Joined Mar 31, 2012
26,398
Thanks for your quick response. actually I am asking , which part of microcontroller work for this code counter or memory registers. how does it work during the execution of program?
That depends on how the code generator for the particular compiler works and what hardware resources the particular microcontroller has available.

One of the risks of this code is that many newer compilers are likely to recognize that the code doesn't perform any computation and therefore may optimize it away completely. Compilers intended for embedded systems are less likely to do this in part because they traditionally haven't been as good at code optimization but also the compiler writer is more likely to be aware that programmers use these kinds of tricks for purposes other than computation.
 

Thread Starter

vead

Joined Nov 24, 2011
629
That depends on how the code generator for the particular compiler works and what hardware resources the particular microcontroller has available.

One of the risks of this code is that many newer compilers are likely to recognize that the code doesn't perform any computation and therefore may optimize it away completely. Compilers intended for embedded systems are less likely to do this in part because they traditionally haven't been as good at code optimization but also the compiler writer is more likely to be aware that programmers use these kinds of tricks for purposes other than computation.
I am trying to understand delay function regarding to hardware. I see in every program there is always delay function I am having problem to understand part of code for delay . actually I don't understand how does this part of code work on microcontroller. I use 8051 (p89v51rd2) and keil compiler
 

MrChips

Joined Oct 2, 2009
25,945
I am trying to understand delay function regarding to hardware. I see in every program there is always delay function I am having problem to understand part of code for delay . actually I don't understand how does this part of code work on microcontroller. I use 8051 (p89v51rd2) and keil compiler
There are various ways to implement delay functions on a computer, some in software, some in hardware.

What you have shown are software solutions that simply waste computer processing time.

The code sits in a loop executing dummy instructions for a set number of times chewing up computer cycles. No other computer tasks are processed during this time (except if there are interrupting events also being processed).
 

WBahn

Joined Mar 31, 2012
26,398
I am trying to understand delay function regarding to hardware. I see in every program there is always delay function I am having problem to understand part of code for delay . actually I don't understand how does this part of code work on microcontroller. I use 8051 (p89v51rd2) and keil compiler
Do you understand how the following loop would be implemented?

Code:
unsigned int j;
j = 0;
while (j < 40000)
{
   j++;
}
If so, then you understand how the delay loop works because this IS the delay loop.
 

DickCappels

Joined Aug 21, 2008
8,685
In an 8051 most likely the compiler would associate the variable "j" with a particular RAM or register location (or some number of RAM or register locations depending upon how many bytes "j" occupies). It would first set the value in those registers/memory locations to zero initially and then increment and test the contents of those registers/memory locations at each pass through the loop.

As WBahn has noted a smart (or optimizing) compiler would recognize that this loop doesn't do anything and not even implement it.
 

MrChips

Joined Oct 2, 2009
25,945
Are you asking how the 8051 chip executes this code in hardware?
Code:
         MOV A, #255
goback:  DEC A
         JNZ goback
Do you understand what the code above does?
 

WBahn

Joined Mar 31, 2012
26,398
suppose j has memory address 0000 1001 and initially it has value 0000 0000 and then it increment 0000 0001- 0000 0010- 0000 0100---------- 00004000 when it reach 0000 0000 again increment from 0000 0000 to 0000 4000.
am I getting something ?[/QUOTE]

What's special about 0000 4000?

When it no longer passes the test (being less than 40,000 in decimal) it fails the test and exits the loop. It does not reach 0000 0000 again.

am I getting something ?
Not as near as I can tell.
 

spinnaker

Joined Oct 29, 2009
7,835
suppose j has memory address 0000 1001 and initially it has value 0000 0000 and then it increment 0000 0001- 0000 0010- 0000 0100---------- 00004000 when it reach 0000 0000 again increment from 0000 0000 to 0000 4000. am I getting something ?

You are over thinking this. All you need to know is that this is a basic loop. It will run machine instructions i times. So if you call delay(50) the loop will run 50 times. If you call delay(100) it will run 100 times or take approximately twice the time as delay(50) .
 

WBahn

Joined Mar 31, 2012
26,398
You are over thinking this. All you need to know is that this is a basic loop. It will run machine instructions i times. So if you call delay(50) the loop will run 50 times. If you call delay(100) it will run 100 times or take approximately twice the time as delay(50) .
Not the code the TS posted. Calling delay(100) will take exactly the same amount of time as delay(50) -- in either case it will run the loop 40,000 times.
 

spinnaker

Joined Oct 29, 2009
7,835
Not the code the TS posted. Calling delay(100) will take exactly the same amount of time as delay(50) -- in either case it will run the loop 40,000 times.

Oh rats I had the code you posted on my brain. Either way the TS is over thinking this. It is not that complicated of a concept.
 
Top