Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How does flag create in C language

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
Hello

I want to know How does flag create in C language. I want to create three flags. what I have to do


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define flag_1  (0)
#define flag_2  (0)
#define flag_3  (0)
 
#define flag_1  (1)
#define flag_2  (1)
#define flag_3  (1)
 
int main()
 
uint8 flag;
 
flag = 1;       /* set flag' */
 
flag = 0;     /* clear flag  */

 

what do you mean by "flag".
Are you talking about "boolean" variable.
 

A flag is simply a variable which is used to represent the current state of a system, typically stored as a numeric value.

In regards to the predefined macros you have implemented in your current code:

Code:
#define flag_1  (0)
#define flag_2  (0)
#define flag_3  (0)
 
#define flag_1  (1)
#define flag_2  (1)
#define flag_3  (1)

The second group of macros actually redefined the previous group of macros for the symbols, flag_1, flag_2 and flag_3.

When the preprocessor encounters multiple definitions of the same symbol, only the macro immediately preceding the defined symbol is utilized to replace the symbol with macro replacement text. Such redefining of a macro, typically triggers a warning during preprocessor phase of compilation, unless a #undef is utilized to first undefine a symbol, before the redefining #define is encountered. Therefore, in the case of your current predefined macros arrangement, only the second set will have any effect during the preprocessor phase of compilation.

Flags are often utilized to implement a finite state machine (FSM), as in the following example:


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
#define state0  (0)
#define state1  (1)
#define state2  (2)
 
 
int main(void)
{
    unsigned char stateflag = state0;
    
    while(1)
    {
        
        switch (stateflag)
        {
            case state0:
                 // Perform Tasks Assigned to State0
                stateflag = state1;
                break;
 
            case state1:
                // Perform Tasks Assigned to State1
                stateflag = state2;
                break;
 
            case state2:
                // Perform Tasks Assigned to State2
                stateflag = state0;
                break;
 
            default:
                // Unknown State Encountered, Reset System to State0
                stateflag = state0;
 
        }
    }
 
    return (0);
}



Similarly, an enumerated type (enum) can be utilized in place of the predefined macros, #define preprocessor directives, to implement the required system states:


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
enum sysstate {state0, state1, state2};
 
enum sysstate stateflag;
 
 
int main(void)
{
 
    
    stateflag = state0;
    
    while(1)
    {
 //       __delay_ms(100);
        
        switch (stateflag)
        {
            case state0 :
                // Perform Tasks Assigned to State0
                stateflag = state1;
                break;
 
            case state1 :
                // Perform Tasks Assigned to State1
                stateflag = state2;
                break;
 
            case state2 :
                // Perform Tasks Assigned to State2
                stateflag = state0;
                break;
 
            default:
                // Unknown State Encountered, Reset System to State0
                stateflag = state0;
 
        }
    }
 
    return (0);
}




BigDog
 
A flag is simply a variable which is used to represent the current state of a system, typically stored as a numeric value.



The second group of macros actually redefined the previous group of macros for the symbols, flag_1, flag_2 and flag_3.

When the preprocessor encounters multiple definitions of the same symbol, only the macro immediately preceding the defined symbol is utilized to replace the symbol with macro replacement text. Such redefining of a macro, typically triggers a warning during preprocessor phase of compilation, unless a #undef is utilized to first undefine a symbol, before the redefining #define is encountered. Therefore, in the case of your current predefined macros arrangement, only the second set will have any effect during the preprocessor phase of compilation.

Flags are often utilized to implement a finite state


BigDog

If pin is high turn on LED and If pin is low turn off LED

There are three task that I want to perform using flag

task0
If pin is high turn on LED for 15 second

task1
If pin is high turn on LED for 25 second

task2
If pin is high turn on LED for 35 second


Code C (Mac) - [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
#define task0  (0)
#define task1  (1)
#define task2  (2)
 
 
int main(void)
{
    unsigned char taskflag = state0;
    
    while(1)
    {
        
        switch (taskflag)
        {
            case task0:
                 // Perform Tasks Assigned to task1
               taskflag = task1;
                break;
 
            case task1:
                // Perform Tasks Assigned to task2
               taskflag = task2;
                break;
 
            case state2:
                // Perform Tasks Assigned to task0
               taskflag = task0;
                break;
 
            default:
                // Unknown State Encountered, Reset System to State0
               taskflag = task0;
 
        }
    }
 
    return (0);
}

 
Last edited:
  • Like
Reactions: Duresh

    Duresh

    Points: 2
    Helpful Answer Positive Rating
To assist you further, you'll need to specify the specific microcontroller and compiler you are utilizing.

BigDog
 

I wonder if they mean using individual bits with a byte as flags.

If so, give the bits their own name within the byte:
#define task0 0
#define task1 1
#define task2 2 ....and so on

The refer to the bit by name:
taskflag |= (1 << task0); to set the task0 bit
taskflag &= ~(1 << task0); to reset the task0 bit.

The compiler should convert the part on the right of the '=' to a constant so although it looks more complicated, it doesn't increase code size or slow it down. Kiel may have a special way of identifying a bit within a byte but the method I showed is generic 'C' and should work in all compilers.

Brian.
 

See if this works in your Keil Compiler. I have not tested it.


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
struct BitFields {
 
    unsigned char F0:1;
    unsigned char F1:1;
    unsigned char F2:1;
    unsigned char F3:1;
    unsigned char F4:1;
    unsigned char F5:1;
    unsigned char F6:1;
    unsigned char F7:1;
} myFlags;
 
sbit statusFlag = myFlags.F0;
sbit processFlag = myFlags.F1;
sbit outputFlag = myFlags.F2;
 
void main() {
 
    while(1) {
 
        if(statusFlag) {            
 
        }
        else if(processFlag) {
 
        }
        else if(outputFlag {
 
        }       
    }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top