SOLVED: Thread safe sprintf

I'm making a timing system where i'm using timer1 with 1 millisecond intervals to update a counter.

The problem I'm having is that during timing if I try to update the time to LCD in a formatted way, the sprintf() is probably using cli(); ? to disable interrupts, disabling the counting effectively :0

I've done a similar project before with avr-gcc and somehow almost identical code worked without a hitch (that code didn't use a long to store the time). What I'm asking is how do I get around this? Some way to output the string without sprintf comes to mind but I haven't found an easy way to do that.

Sure, I could just print the time raw and that works but it looks ugly :P

Format code below:

// Format time for display  
void format(unsigned long o) {
  secs = o / 1000;
  tics = o - (secs * 1000);
  sprintf(disp, "%03u.%03u", secs, tics);  
}

And it's used like this when the timer is running:

        if ((mem - time) == 0){ //update display every 0.100 seconds
          mem += 100;
          temp = 1;
        }
        if (temp == 1){
        temp = 0;
        format(time);
    lcd.setCursor(0,3);
    lcd.print(disp);
    }

If I comment out either the sprintf(disp, "%03u.%03u", secs, tics); or the format(time); the timing is accurate.

Oh, and i've commented out cli();s in wiring.c just to be sure they're not interfering and checked rest of the code in case of any cli();s.

You might want to try out another of the functions in stdio and read the notes at
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio

I’m not familiar enough with the avr libraries to give any more definitive help.

The problem I'm having is that during timing if I try to update the time to LCD in a formatted way, the sprintf() is probably using cli(); ? to disable interrupts, disabling the counting effectively

\ No. The sprintf function does not disable or enable interrupts. It has no reason to.

You have not posted enough of your code to see for sure what your problem is. Is format() an ISR? Why are secs and tics global?

PaulS: No. The sprintf function does not disable or enable interrupts. It has no reason to.

You have not posted enough of your code to see for sure what your problem is. Is format() an ISR? Why are secs and tics global?

Changed the variables secs and tics to format(), there was no need for them to be global (they were from previous iteration of the code).

Solved the problem, it was as easy as increasing the array disp[] size, seemed to be a overflow issue..

Thank you for the help!

Maybe we'd've got the answer if you'd've posted the code.

AWOL: Maybe we'd've got the answer if you'd've posted the code.

True, my code is however at the moment 500+ lines, partly commented out and not so informatively commented so I was shy to post it :blush: