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;
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;
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;
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
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?
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
Subsonics has it almost fixed... try it like this....
You need to switch the flags etc, inside the interrupt service routine...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; } }
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 09:40 PM.
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
Ok... post the whole code as it is right now...
// 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
For the love of all things good and holy, use code tags!
Then, look at the following sets of instructions:
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:if (G_Flag2 == 1) { .... G_Flag2=0; // invert G_Flag2 G_Flag2=!G_Flag2; // invert it again, so it's back to 1!
Code:while (1) // loop forever, the interrupt will handle everything ;
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
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.
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: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
Notice how I only change the flag in the interrupt and only display the status in main.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 }
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
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 edgeCode: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; }
@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 06:44 AM.
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?