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. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
    Appologies.... You can use the enum the way you have it!! Also you can type define it

    Just add this line

    my_task_ t task;

    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)
    {
    my_task_t task;
       switch  (task)
       {
          case task1:
             DoSomething0();
             break;
     
          case task2:
             DoSomething1();
             break;
     
          case task3:
             DoSomething2();
             break;
    }
     
     
     
  2. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    I am not asking for enum statement. please look at following program. here I am trying to use enum , switch and if condition. I want make working program later I will test. how to use all 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;
    my_task_ t task;

    int main(void)
    {
       switch  (task)
       {
          case task1:
              if <task> == task1
             DoSomething0();
             break;
     
          case task2:
            if <task> == task2
           DoSomething1();
             break;
     
          case task3:
              if <task> == task3
             DoSomething2();
             break;
    }

     
     
  3. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    please you don't need to Apologizes. you are doing great work. you always help me.
     
  4. dave

    Dave New Member

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


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK

    I'm going to write up a program and then I'll be able to show you... I hate trying to help when there is doubt..

    If I can't get round to it tonight, I'll do it first thing tomorrow..
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
    I don't think you are using emun the way it should be used..

    I wrote this and it works as intended
    Code (c):

    #include <stdio.h>
    #include <8052.h>
    void do_task_one(void), do_task_two(void), do_task_three(void);
    enum{
     task1,
     task2,
     task3,
    };
    void main()
     {
     char my_task = 0;
     
     while(1)
      {
      switch(my_task)
       {
       case task1 : do_task_one(); break;
       case task2 : do_task_two(); break;
       case task3 : do_task_three(); break;
       }
      if(++my_task > 2) my_task = 0;
      }
         }
    void do_task_one()
    {}
    void do_task_two()
    {}
    void do_task_three()
    {}
         
     
    Remember to rename your headers as I use SDCC compiler.
     
  7. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    I wrote something. Its not complete code I just trying to complete code. I have some issues like if condition and code for three LEDs and I am working on that issue what do you think about code?
    Code (c):

    #include<reg51.h>    //header file

    #define  LED1_ON   1
    #define  LED1_OFF  0
    #define  LED2_ON   1
    #define  LED2_OFF  0
    #define  LED3_ON   1
    #define  LED4_OFF  0

    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
    typedef enum
    {
        task1,
        task2,
        task3
    };
    // prototype function
    void LED1_Blink(void);
    void LED2_Blink(void);
    void LED3_Blink(void);
    int main(void)
    {
       unsigned int task=0;
     
       while(1)
      {
       switch  (task)
       {
          case task1:
             LED1_Blink();
             break;
       
          case task2:
             LED2_Blink();
             break;
       
          case task3:
             LED3_Blink();
             break;    
       }
    }
    }
    void LED1_Blink ()
    {
        unsigned int i;
        for (i = 0; i < 1000; i++);
    }
    void LED2_Blink()
    {
        unsigned int j;
        for (j = 0; j < 1500; j++);
    }
    void LED3_Blink()
    {
       unsigned int k;
        for (k = 0; k < 1200; k++);
    }

     
     
    Last edited by a moderator: Jun 11, 2017
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,163
    Likes:
    909
    Location:
    Rochdale UK
    But you are not incrementing task... If you want to cycle through the three tasks then change the variable..
     
  9. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    Does it make any sense ? I think I am too long to test code on proteus.
    Code (c):

    #include<reg51.h>    //header file

    #define  LED1_ON   1
    #define  LED1_OFF  0
    #define  LED2_ON   1
    #define  LED2_OFF  0
    #define  LED3_ON   1
    #define  LED4_OFF  0

    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

    typedef enum
    {
        task1,
        task2,
        task3
    };
    // prototype function
    void LED1_Blink(void);
    void LED2_Blink(void);
    void LED3_Blink(void);
    int main(void)
    {
       unsigned int task=0;

        while(1)
      {
       switch  (task)
       {
          case task1:
                     if (task == task1)
                     {
                        LED1 = LED1_ON;
                        LED1_Blink();
                        LED1 = LED1_OFF;
                     }
             break;

          case task2:
                     if (task == task2)
                     {
                          LED2 = LED2_ON;
                          LED2_Blink();
                          LED2 = LED2_OFF;
                     }
             break;

          case task3:
                     if (task == task3)
                     {
                        LED3 = LED3_ON;
                        LED3_Blink();
                        LED3 = LED4_OFF;
                     }
             break;
       }
    if(++task > 2) task = 0;
    }
    }

    void LED1_Blink ()
    {
        unsigned int i;
        for (i = 0; i < 1000; i++);
    }

    void LED2_Blink()
    {
        unsigned int j;
        for (j = 0; j < 1500; j++);
    }

    void LED3_Blink()
    {
       unsigned int k;
        for (k = 0; k < 1200; k++);
    }
     
     
    Last edited by a moderator: Jun 11, 2017

Share This Page