1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

two different delay functions in program

Discussion in 'Microcontrollers' started by Parth86, Jun 9, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    Hello
    I have confused on following two programs. I think both program's are same but there are little difference. In first program, variable j declare inside bracket. while in another program, there is only empty space

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

    void delay ()
    {
        unsigned int j;
        for (j = 0; j < 40000; j++);
    }
     
    Is there any difference difference between these two program's? why we write in two style?
     
  2. elecLear78

    elecLear78 Member

    Joined:
    Jul 16, 2013
    Messages:
    226
    Likes:
    1
    Location:
    india
    In your case both are same. But first one can modified to give delay as per the argument
    Code (text):

    void delay(unsigned int j)
    {
    unsigned int k;
    for(k=0;k<j;k++)
    {
    ;
    }
     
    From the main you can invoke like
    Code (text):

    int main(void)
    {
    delay(12000);
    delay(13000);
    retutn 0;
    }
     
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    No! they are the same, The first example will have a spare variable on the stack ( not a good idea.)

    Normally we use the parameter to pass a variable.

    Code (c):

    void delay(unsigned int j)
          {
           while(j--);
          }
     
    This gives a variable delay system..
     
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5

    Thanks Ian. I have another doubts following is program for LED blinking
    Code (c):

    #include <REG51.h>
    #define LED              P2
    #define LED_ON      0xff
    #define LED_OFF     0x00
    void delay (unsigned int j)
    {
       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 exact use of while (1) statement in above program
    Code (c):
    while (1)
    My understanding it should be while (j=1). why write only while (1)
    Code (c):

    while (condition) // while (j=1)
    {
       statements;
    }
     
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    Code (c):

    void delay (unsigned int j)
    {
       unsigned int j;
       for (j = 0; j < 40000; j++);
       {
       
       }
    }
     
    The first int J will not be used here so delay(500) is a waste of typing... Only the local j will be used.

    As for the while()... The compiler evaluates a Boolean answer.. Yes / No so while(1) will always be true..

    while(1) {;;;;} is a foreverloop as 1 will always be 1.. Every thing in the curly brackets will be executed forever!
     
  7. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    I saw this type of while statement in program
    Code (c):
     while (1)
       {
           LED = LED_ON;
           delay(500);
           LED = LED_OFF;
           delay(5000);
           LED = LED_OFF;
     
       }
    }
    we can also use j==1
    Code (c):
     while (j==1)
       {
           LED = LED_ON;
           delay(500);
           LED = LED_OFF;
           delay(5000);
           LED = LED_OFF;
          }
    }
    why do we use only while (1) statement and why don't we use while (j==1) instead of while(1) ?
     
    Last edited by a moderator: Jun 9, 2017
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    Because j is a variable.... Its easier to just write while(1) because j can change and 1 cannot!
     
  9. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    [​IMG]
    have a lookz
     
  10. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    And some code too see how it works
    Code (text):

    #include <stdio.h>

    int main () {

       /* local variable definition */
       int a = 10;

       /* while loop execution */
       while( a < 20 ) {
         printf("value of a: %d\n", a);
         a++;
       }

       return 0;
    }
     
    while is set to 1 when you want it to run forever if you use a test it runs as long as test is true
    the j==1 would run till j didn't equal 1 seeing j is the holder for your delay time i guess you don't need a delay cause 1 will happen one time
     
  11. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    okay , suppose following conditions
    while (1)
    if while is set to value (1) then run forever loop other wise don't run loop. does compiler set while to 1 and suppose if while (6) than what happen. does compiler store value of 6 at memory register automatically?
     
  12. elecLear78

    elecLear78 Member

    Joined:
    Jul 16, 2013
    Messages:
    226
    Likes:
    1
    Location:
    india
    The compiler only checks for the condition inside the while(condition). if condition is TRUE (which is non zero) value then it executes the statements inside the while loop. If the condition in the while(condition) is FALSE (value 0) then it comes out of while loop. since 1,6 are non zero values it keeps executing the statements inside the loop. The condition can be an expression or constant, the compiler will evaluate the condition to either TRUE or FALSE and based on that takes decision to either execute or not execute the while statements.

    while(condition)
    {
    /* statements*/
    }
     
  13. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    That s not totally true the while loop works. One way if it's true like while 1 then it's forever looping
    If it's a test for a value it loops to the value is met
    While j < 20 so any value can end it.
     
    Last edited: Jun 10, 2017
  14. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,798
    Likes:
    134
    Location:
    morristown,tn
    If you compled the code I posted you could see what happens the while loop runs till it hits 20
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    The main reason 1 is used is because 1 = true and 0 = false.. But! As you said it could be 1~255 and it will still work..

    If you define true and false ( which the compiler already does ) You would write.
    Code (c):

    #define true 1
    #define false 0

    void main(void)
       {
       while(true)
          {
          }
       {
     
    Which works exactly the same...
     
    • Like Like x 1
  16. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    if you don't mind.. can I continue another topic in single thread
    typedef is a reserved keyword in the C language. The struct keyword is used to define structure. the structure declaration look like following
    Code (c):

    typedef struct
    {
        int i;
        int j;
        int k;
        char a;
    }my_str;
     
    how to use struct statement in real program.
    Code (c):

    #include <REG51.h>
    typedef struct
    {
        int i;
        int j;
        int k;
        char a;
    }my_str;

    void main()
    {
        while (1)
        {
          //write code here
       
        }
    }
     
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    Structures are types.. It is still classed as a single variable..
    Code (c):

     struct
       {
       int i;
       int j;
       int k;
       char a;
       }a_structure;

    // to initialise a structure you use
    struct a_structure my_str;

    // using typedef we can define the structure as one type
    typedef struct
       {
       int i;
       int j;
       int k;
       char a;
       }a_structure;

    // to initialise this structure you use
    a_structure my_str;

    //to use the elements you simple use the '.' operator..

    my_str.i = 20;
    //or
    my_str.a = 'A';

     
     
    • Like Like x 1
  18. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    Thanks. Please at look at following program
    Code (c):

    typedef enum
    {
        task1 ,
        task2,
        task3
    } my_task;

    // to initialize a
    my_task task;
     
    How to use enum statement in c program.
     
    Last edited by a moderator: Jun 10, 2017
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    enum isn't a structure and hasn't a name

    Code (c):

    enum{
       White,
       Blue,
       Red,
       Black,
    };
     
    Now White = 0, Blue = 1, Red = 2 and Black = 3..

    enum enumerates identifiers for easy reading.
     
  20. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    513
    Likes:
    5
    Now I understand what is enum. How to insert enum and case statement in program. I want to understand use of enum and switch statement in one program
    Code (c):

    #include<reg51.h>    //header file

    sbit   LED1     = P2^0;    //LED 1 conncted to port P2 pin 0
    sbit   LED2     = P2^1;    //LED 2 conncted to port P2 pin 1
    sbit   LED3     = P2^2;    //LED 3 conncted to port P2 pin 2

    // prototype function
    void DoSomething0(void);
    void DoSomething1(void);
    void DoSomething2(void);

    typedef enum
    {
        task1 ,
        task2,
        task3
    } my_task_t;

    int main(void)
    {
       switch  (task)
       {
          case task1:
             DoSomething0();
             break;
     
          case task2:
             DoSomething1();
             break;
     
          case task3:
             DoSomething2();
             break;
    }
     
     
    Last edited by a moderator: Jun 10, 2017
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    ONLINE
    Enum isn't a structure!!
    Code (c):

    typedef enum //<<--- No no!!
    {
        task1 ,
        task2,
        task3
    } my_task_t;  //<<-- No no...
     
    Code (c):

    enum my_task_t //<<--- Yes yes!
    {
        task1 ,
        task2,
        task3
    };  //<<-- Yes yes..
     
    task1 = 0, task2 = 1 and task3 = 2

    Yo can always change the enum like this..
    Code (c):

    enum my_task_t
    {
        task1 = 3 ,
        task2 = 7,
        task = 9
    } ;
     
    Other than that... Yes enum is used correctly!!
     

Share This Page