NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Problem with LCD

April 27, 2012
by HexManiac
HexManiac's Avatar

I've been playing with realtimeclock1.c and I'm having a few problems displaying numbers on the screen. What it boils down to is this...

I added this to the original code to give me time in hours, minutes and seconds:

        uint32_t hours, minutes, seconds;
        int32_t totalseconds = (int32_t) (the_time / 100);

        seconds = totalseconds % 60UL;
        totalseconds /= 60UL;
        minutes = totalseconds % 60UL;
        totalseconds /= 60UL;
        hours = totalseconds % 24UL;

Then I tried displaying the result on the LCD like this:

        lcd_home();
        fprintf_P(&lcd_stream, PSTR("%02u:%02u:%02u"), hours, minutes, seconds);

This caused all sorts of problems, the numbers were really screwed up. But when I did this:

        lcd_home();
        fprintf_P(&lcd_stream, PSTR("%02u:"), hours);
        fprintf_P(&lcd_stream, PSTR("%02u:"), minutes);
        fprintf_P(&lcd_stream, PSTR("%02u"), seconds);

... it all works perfectly. Can anyone explain please?

April 27, 2012
by pcbolt
pcbolt's Avatar

Hex -

That's pretty strange. I'd have to test the code to see what's going on...like trying %02d maybe, or using a "uint32_t" for totalseconds.

April 28, 2012
by HexManiac
HexManiac's Avatar

Actually, I started out with %02d (signed decimal) but changed to %02u (unsigned decimal) when I first encountered problems, but it made no difference.

I tried dumping (int32_t) the_time to the display (right-justified so I don't need to clear the old value)...

fprintf_P(&lcd_stream, PSTR("%20d"), the_time);

... the number displayed seems always to be between 32767 and -32768 (I'm guessing cos the numbers change too quick) although I know the actual value goes between 0 and 8639999.

Displaying 8 and 16 bit numbers is not a problem, only 32 bit numbers, so it would seem to be an issue with your lcd library.

Heh, I've only been a nerdkitter for a few weeks and already I'm helping! Wicked!

April 29, 2012
by pcbolt
pcbolt's Avatar

Hex -

The NK LCD library doesn't define the "fprintf" type functions, that's done in the AVR library inside "stdio.h".

Your 2nd to last paragraph gave me a clue what was wrong. To print out "long" variables (i.e. 32-bit), you need to prefix either %d or %u with "l" (lower case "L").

So try this out...

fprintf_P(&lcd_stream, PSTR("%02lu:%02lu:%02lu"), hours, minutes, seconds);

Why it worked when you separated the statement out is still a mystery.

April 29, 2012
by HexManiac
HexManiac's Avatar

Magic! All problems gone away now, thanks!

Post a Reply

Please log in to post a reply.

Did you know that our USB NerdKit comes with everything you need to get started with microcontrollers? Learn more...