switching flag...why doesn't this work

This is a discussion on switching flag...why doesn't this work within the C Programming forums, part of the General Programming Boards category; BOOL G_FlagJ=0; BOOL G_Flag =0; if(G_FlagJ==1) { if(G_Flag==1) { LCD_Position(0,5); LCD_PrCString("on"); G_Flag=0; } else { LCD_Position(1,5); LCD_PrCString("off"); G_Flag=1; } G_FlagJ=0; ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    switching flag...why doesn't this work

    BOOL G_FlagJ=0;
    BOOL G_Flag =0;

    if(G_FlagJ==1)
    {

    if(G_Flag==1)
    {
    LCD_Position(0,5);
    LCD_PrCString("on");
    G_Flag=0;
    }
    else
    {
    LCD_Position(1,5);
    LCD_PrCString("off");
    G_Flag=1;
    }
    G_FlagJ=0;
    }







    interrupt_ISR(void)
    G_Flag=1;
    G_FlagJ=1;

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If this is complete. Then you start by initializing G_FlagJ to zero, then immediately checks if it's one. It isn't so nothing is executed.

    Code:
    BOOL G_FlagJ=0;
    BOOL G_Flag =0;
    
    if(G_FlagJ==1)
    {
    	
    	if(G_Flag==1)
    	{
    		LCD_Position(0,5);
    		LCD_PrCString("on");
    		G_Flag=0;
    	}
    	else
    	{
    		LCD_Position(1,5);
    		LCD_PrCString("off");
    		G_Flag=1;
    	}
    	G_FlagJ=0;
    }
    
    
    interrupt_ISR(void)
    G_Flag=1;
    G_FlagJ=1;

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    Right, so it goes to the interrupt makes the flag a one and executes the for loop, however the nested for loop is causing me problems, I want it to say on whrn the flag is a one and off when the flag is a zero I thought this logic was right but its not working

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So, you are convinced that a for loop is what is causing your problems, but despite this you post the above, unrelated code snippet. Did I get that right?

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    I'm not sure what the problem is, I was hopping someone could help, and bot sure what u mean the code is c for my micrcontroller I use, the interrupt void just generates and interrupt every second as I have it set and changes the variable flag I just wanna cycle through the on off stuff

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Subsonics has it almost fixed... try it like this....

    Code:
    BOOL G_Flag =0;
    
    interrupt_ISR(void)
      {  if(G_Flag==1)
            { LCD_Position(0,5);
               LCD_PrCString("on");
               G_Flag=0; }
           else
              { LCD_Position(1,5);
                 LCD_PrCString("off");
                 G_Flag=1; } }
    You need to switch the flags etc, inside the interrupt service routine...

    It is very important that you block and deblock your code with braces. It's how the compiler knows what to do in what order and how often...
    Last edited by CommonTater; 02-22-2011 at 08:40 PM.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    the code that i posted was a bad copy and past part on my half...sorry about that i do have it blocked and whatnot, however when i have the if statments in my while loop it is stuck in the "on" command and doesn't go to the "off" part and the lcd we are using takes to much time to do in the ISR so we just have to set flags and do it in main

    so i am still puzzled as why this doesn't work

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... post the whole code as it is right now...

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    // spin forever
    while(1)
    {
    if(G_Flag1==1)
    {

    if(G_Flag2==1)
    {
    LCD_Position(0,5);
    LCD_PrCString("off");
    G_Flag2=0;
    G_Flag2=!G_Flag2;

    }
    else
    {
    LCD_Position(0,5);
    LCD_PrCString("on");
    G_Flag2=1;
    G_Flag2=!G_Flag2;

    }

    G_Flag1=0;
    }



    } // end of while 1;



    }// end of main


    void Counter16_ISR(void)
    {
    G_Flag1=1;
    G_Flag2=1;

    test_Data_ADDR ^= test_MASK;
    }
    the test data...is to make sure the interrup is working correctly

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    For the love of all things good and holy, use code tags!

    Then, look at the following sets of instructions:
    Code:
    if (G_Flag2 == 1)
    {
        ....
        G_Flag2=0;  // invert G_Flag2
        G_Flag2=!G_Flag2;  // invert it again, so it's back to 1!
    It was 1, you made it zero, then negated that, making it 1 again. It never really changes. You also have the same sort of logic in your else block. Lastly, I don't see any need for two flags, just one is fine. The snippet that Tater gave you in post #6 is perfect, use that and change main to:
    Code:
    while (1)  // loop forever, the interrupt will handle everything
        ;

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    don't really know what a code tag is, but like i said before you can not update an lcd in the ISR it will never work, it the lcd takes way to long to change i need to do this in main

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,425
    Quote Originally Posted by begginer View Post
    don't really know what a code tag is
    That red text is a link. Click on it and read it before you post any more code. You also could have found that information if you actually read << !! Posting Code? Read this First !! >> before posting.

    but like i said before you can not update an lcd in the ISR it will never work, it the lcd takes way to long to change i need to do this in main
    If your interrupt is faster than your LCD can update you'll never actually get accurate on/off results whether you do this in main or in the ISR. If I switch a light on/off every second, and you try to tell me the state every 2.4 seconds, you wont be giving a very accurate description of what the light is doing, will you? You need to be updating at least every second, preferably at least twice that fast. So if your LCD can't update at least once per second, you're not going to get good results no matter where the code is. I can write faster than your LCD, so there must be something else going on. Maybe it's the mysterious nested for loop you wont show us. In the future, we'll need to see all relevant parts of the actual code to help you. No more guessing games. If you must do the LCD code from main, here is an example:
    Code:
        while (1) {
            if (g_flag) {
                 // print "on"
            }
            else {
                 // print "off"
            }
        }
    } // end of main
    
    void Counter16_ISR(void)
    {
        g_flag = !g_flag;  // toggle g_flag from on to off each interrupt
    }
    Notice how I only change the flag in the interrupt and only display the status in main.

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    i really appreciate all the help, but i don't have any mystery for loop, i would show u all that code if i had it. The micro that i use the PSoC (cypress ccy8c29466-24pxi) has a slow lcd and you are right you will never get an accurate result. You would never update the lcd in the isr and just never would

    however all i am trying to do is set the flag so that it updates once a second what i have the counter 16ISR doing and doing the stuff in the lcd based off what the flag condition is

    here is the code in proper format that i have been working on taking your ideas and what i want it to do in to account

    Code:
    	
           BOOL G_Flag1=0;
           BOOL G_Flag2-0;
    
    
    
        
            if(G_Flag21{
                    I2Caddr=0x20;
    	        charcount=2;
    	        command[0]=0x00;
    	       command[1]=0x81;
    	
    	
    	       I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    	       while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    	       I2C_ClrWrStatus( );
              }
    	
    	
    	if(G_Flag2){
    	
    	      I2Caddr=0x20;
    	      charcount=2;
    	      command[0]=0x00;
    	      command[1]=0x24;
    	
    	     I2C_bWriteBytes(I2Caddr, command, charcount, I2C_CompleteXfer);
    	     while(!(I2C_bReadI2CStatus( ) & I2CHW_WR_COMPLETE));
    	    I2C_ClrWrStatus( );
            }
    
    
    
    
    void Counter16_ISR(void)
    {
    	G_Flag1=1;
    	G_Flag2=1;
    	
    	
    	test_Data_ADDR ^= test_MASK;
    }
    This happens to be an i2c project and depending on if the clock is high or low i need ot do something different which is why i was trying to just get it working with the lcd...not secret for loop or anything....just trying to update the flag conditons to rising edge or falling edge

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    @begginer:

    Learn how to cut and paste; your code is full of errors; DO NOT RETYPE YOUR CODE!

    Do something so we can know when 1 snippet ends and another begins.

    Tim S.
    Last edited by stahta01; 02-23-2011 at 05:44 AM.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    Ok, I M new to this forum and I am more of a hardware guy, not that big into software...I did get it to work with my I2c commands thanks everyone for the help I do have a question that is on the same Subject....

    I know in c++ ( I know c is way better) there is a way to generate random number....is there a way to generate ransom numbers in c, and more specific hex number?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  3. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21