Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include<reg51.h> //header file #define task1 (1) #define task2 (2) #define task3 (3) //prototype function DoSomething0(); DoSomething1(); DoSomething2(); int main() { unsigned int task; switch (task) { case task1: DoSomething0(); break; case task2: DoSomething1(); break; case task3: DoSomething2(); break; } } DoSomething0() { } DoSomething1() { } DoSomething2() { }
Code C - [expand] 1 2 3 4 5 6 7 switch (task) { case task1: if (task == task1) DoSomething1(); break; }
Code C - [expand] 1 2 3 4 5 6 7 switch (task) { case task1: if (someVar == someValue) DoSomething1(); break; }
void Task1 (void)
{
//do something
printf("Task1\n");
}
void Task2 (void)
{
//do something
printf("Task2\n");
}
void Task3 (void)
{
//do something
printf("Task3\n");
}
int main()
{
void (* Tasks[])(void) = {Task1, Task2, Task3};
char i = 0;
for (i = 0; i < 3; i++)
Tasks[i]();
return 0;
}
enum E_STATE = {task0, task1, task2, task3};
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 #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; } } } 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++); }
switch (task)
{
case task1:
LED1 = LED1_ON;
LED1_Blink();
LED1 = LED1_OFF;
break;
case task2:
LED2 = LED2_ON;
LED2_Blink();
LED2 = LED2_OFF;
break;
case task3:
LED3 = LED3_ON;
LED3_Blink();
LED3 = LED4_OFF;
break;
}
Code C - [expand] 1 2 3 4 5 6 typedef enum { task1, task2, task3 }myTasks;
Code C - [expand] 1 2 3 4 int main(void) { myTasks task; .....
myTasks task;
int task;
None in behaviour. Only I thought it gives a more readable code.Which different behavior do you expect by declaring...
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 #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 enum mytask { task1 = 1, task2, task3 }; // prototype function void LED1_Blink(void); void LED2_Blink(void); void LED3_Blink(void); int main(void) { int i=task1; for (i=0; i<4; i++) switch(i) { case task1: { LED1_ON; LED1_Blink (); LED1_OFF; } break; case task2: { LED2_ON; LED2_Blink(); LED1_OFF; } break; case task3: { LED3_ON; LED3_Blink(); LED3_FF; } 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++); }
how many mistake do you find in program what could be possible reason
Code C - [expand] 1 LED1 = LED1_ON;
that was my mistake. while doing so many test I posted old code. now code are working fine. I just made that program to learn how to do multiple tasks using c program.Moreover, there is no LEDs toggling at all. #defines define (sorry for tautology) only constants, but they are not macros, so after the preprocessing the compilier will see only 1's and 0's followed by semicolon. Your previous version was correct:
Code C - [expand] 1 LED1 = LED1_ON;
Task2 has LED1_OFF, but not LED2_OFF. Although thay are both equal to 0 it's not good to place another's constant.
The style of the code looks very wierd. A lot of copy-paste programming, unnecessary redundancy. But it will work (if you correct led switching).
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?