This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Interrupt and global variable

Hello!

I've an Timer-Interrupt Routine (timer 3 with reload) with will
served every 10 ms!
In this Interrupt-Routine, I send 2 Messages via the CAN Interface.
Now, I send the second message straight after the first, and the problem
is, that the second message won't send in a 10 ms rhytm but often
longer (up to 400 ms!).
Now I use a global variable, where I decide which message to send;
when the value is 0, send the first message, und change the value to 1,
then send message 2 and set the value to 0 and so on!

I declared an int (I also tried unsigned int, char ...). I have set the
value of the variable to 0 in the "main" function, but in the Interrupt
Service-Routine, the value ist "FF", and when I try to change to value
inside the Interrupt-Service Routine, it has no effect!

Is there any other way to transfer data between th "main" and the Interrupt
Routine?

THX in advance!

Have a nice day!
SVEN

  • 1. use an unsigned char, it can be updated atomically (in one instruction) by your main() function. If it only needs to be true or false, delcare it as 'bit'.

    2. declare the function volatile so that the compiler is forced to reload the variable's contents from RAM each time it is checked in main().

    Ex: volatile unsigned char data g_flag;
    Note that the 'data' word forces the var into the DATA area, the fastest RAM after registers on the 8051. The g_ prefix is a quick and ugly reminder that this variable is global and can be destroyed by any function in any file that externs it.

    3. think of a way to do this with a function call from main() into the driver file to set the flag on behalf of main(). Globals are evil, use them only if you cannot meet your real-time deadlines because of function call overhead.

    - Mark

  • Um, I didn't notice the 166 toolset, so some of the 8051 biased stuff doesn't apply. Sorry.