Strange Compilation behaviour

Status
Not open for further replies.

gredpath

Well-known member
I am writing an Interrupt service routine, and it exhibits 2 strange behaviours.
The routine is called every 100 mSecs, and takes 9 usecs to execute.
Whilst this routine is called, a value is checked if true a state is checked and changed.
If the condition is not true it is supposed to set the state to LOW.
What seems to happen is that when the value is true it enters that portion of the code, but then enters the section of code if the condition was not true.
But even more bizarre I have placed a Serial print in the false portion, if I program it to print a period "." it works, but if I program it print a numerical value the ISR completely hangs, I have also tried other functions and these cause it to hang as well.
It seems to be a compiler issue.

void set_timer(uint8_t iptr) // Immediate set of outputs
{
if (timers[iptr].period > 0) // timer is active turn on or flash
{
timers[iptr].pinstate=HIGH;
if(timers[iptr].flash == true) // flash enabled
{
if(timers[iptr].on_timer != 0)
{
timers[iptr].pinstate = HIGH;
}
else // state was low
{
timers[iptr].pinstate = LOW;
}
}
set_pin(iptr,timers[iptr].pin_no,timers[iptr].pinstate);
}
else // Force output off it has timed out
{
if (timers[iptr].invert == false)
{ digitalWrite(timers[iptr].pin_no,LOW); }
else {digitalWrite(timers[iptr].pin_no,HIGH); }
timers[iptr].pinstate=LOW;
// Serial.print(iptr);
}
}
:confused:
 
Have you declared the timers array to be volatile?

Printing in an ISR is usually a bad idea because printing uses interrupts too.

Pete
 
Yes they are volatile.
I agree on your print comment, it is only there to debug, it won't be in the final version of the code.
thanks
struct Timers{
uint8_t pin_no;
uint32_t period;
uint16_t on_period;
uint16_t on_timer;
uint16_t off_timer;
uint16_t off_period;
bool flash;
bool flash_state; // used to indicate if to decrement on or off timer
bool invert;
bool pinstate;
bool enablepin;
};
Timers volatile timers[maxouts];
 
What seems to happen is that when the value is true it enters that portion of the code, but then enters the section of code if the condition was not true.
But even more bizarre I have placed a Serial print in the false portion, if I program it to print a period "." it works, but if I program it print a numerical value the ISR completely hangs, I have also tried other functions and these cause it to hang as well.
You probably have memory corruption / array out of bounds access or you are using uninitialized variables.

It seems to be a compiler issue.
Chances are 99.99999% that your code has a bug and the compiler is fine.

Please post complete code and use code tags:

[CODE]
Your code
[/CODE]

\\

Serial is not really interrupt safe, but it normally works well enough for debug output.
 
Thanks tin, I will check, but I can get the routine to run, if I add any code with the else statement, even dummy equates, it crashes.
 
As I am using the tick (1Ms) interrupt, which always starts, I will enabling running of my code by way of a Boolean flag, (after the standard part of the Interrupt) which I can set after all initialisation is completed.
I will update if this works.
 
Status
Not open for further replies.
Back
Top