How to share a variable change throughout the main.c an the ISR

valentinopapan wrote on Monday, June 24, 2019:

I am working with a periodic Interrup in which i have inizialized a counter, every time i get an interrupt the counter is increased by one unit through a notification function. This notification function is executed in the ISR ( Interupt Service Rountine). Is there anyway that i can show that variation of this counter outside the ISR, say in my main.c file so that i can use it for a conditional trigger ( ex: " if ( counter == 50)")
thanks to all that might be able to help

richard_damon wrote on Monday, June 24, 2019:

A variable defined outside any function, is at file scope and is directly accessable by any function in that file, and by functions in other files in they have an ‘extern’ decleration for the variable, unless the definition was declared static.

You do need to make sure that the reading/changes are atomic (done without possible interruption) or you might get mixed results. You may need to also add a volatile to let the compiler know that this variable might change behind its back in the ISR, so it doesn’t ‘optimize’ the test to use a value it read previously, which might now be wrong.

valentinopapan wrote on Wednesday, June 26, 2019:

Can you explain better what you mean by atomic ? my changes on this
variable happen in the ISR routine by it is a global variable and not
defined as static. But my main.c doesn’t seem to recognise the changes at
all. I know that my variable is increasing in the ISR because the reaching
of a certain value on that variable triggers an hardware action that I can
see. but I cannot use the variable change outside my function that runs in
the ISR.

richarddamon wrote on Wednesday, June 26, 2019:

Perhaps if you show the code you use. The only way that main.c won’t see the change in the variable is if main.c is seeing a dfferent variable (perhaps with the same name).

The atomic comment is that if the varable in question isn’t accessed in a single unbreakable instruction, then it is possible that the read gets part of an old value and part of a new value, giving a value that doesn’t match either of them.

pfav63 wrote on Wednesday, June 26, 2019:

Hi
Did you declare the variable as volatile?

valentinopapan wrote on Friday, June 28, 2019:

Thank you all for your help, indeed declaring the variable as volatile was
necessary and also the fact that I initialised my counter variable as equal
to 0 from the beginning so that was all that was readable outside the
function. I used a support variable in order to check how my main counter
was changing and everything works fine. Thank you all again.

On Wed, Jun 26, 2019 at 2:36 PM pfav63 pfav63@users.sourceforge.net wrote:

Hi
Did you declare the variable as volatile?

On Mon, Jun 24, 2019, 17:21 Valentino Papandrea
valentinopapan@users.sourceforge.net
valentinopapan@users.sourceforge.net wrote:

I am working with a periodic Interrup in which i have inizialized a
counter, every time i get an interrupt the counter is increased by one unit
through a notification function. This notification function is executed in
the ISR ( Interupt Service Rountine). Is there anyway that i can show that
variation of this counter outside the ISR, say in my main.c file so that i
can use it for a conditional trigger ( ex: " if ( counter == 50)")
thanks to all that might be able to help

How to share a variable change throughout the main.c an the ISR

SourceForge.net: Log In to SourceForge.net

Sent from sourceforge.net because you indicated interest in
SourceForge.net: Log In to SourceForge.net

To unsubscribe from further messages, please visit
SourceForge.net: Log In to SourceForge.net


How to share a variable change throughout the main.c an the ISR
https://sourceforge.net/p/freertos/discussion/382005/thread/1e23c91fee/?limit=25#fe4d/2acd

Sent from sourceforge.net because you indicated interest in
SourceForge.net: Log In to SourceForge.net

To unsubscribe from further messages, please visit
SourceForge.net: Log In to SourceForge.net