Continue to Site

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.

  • 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.

two different delay functions in program

Status
Not open for further replies.
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;

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;
}
 
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?
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;
}
 
I don't think you are using emun the way it should be used..

I wrote this and it works as intended
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.
 
I don't think you are using emun the way it should be used..
I wrote this and it works as intended
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?
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:
But you are not incrementing task... If you want to cycle through the three tasks then change the variable..
Does it make any sense ? I think I am too long to test code on proteus.
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:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top