Leading Zeros for clock display

Uncle Doug

New Member
I made a clock with an RTC module and serial OLED which displays time in 24-hour format, i.e. 14:05:32. My line for the output is SEROUT 0, N2400, (#hrs,":",#mns,":",#secs). I need leading zeros for the time instead of 6:17:7, I want 06:17:07. Basic Stamp uses DEC2 so it's easy.
 
I sympathize. The best I've been able to do in the past is something like the code below. It's a little cryptic but the divide sets the leading digit and the modulus sets the second digit. For convenience the characters are just stored in RAM and then output in one go:

Code:
    bptr = 100
    @bptrinc = hrs / 10 + "0"
    @bptrinc = hrs // 10 + "0"
    @bptrinc = mins / 10 + "0"
    @bptrinc = mins // 10 + "0"
    @bptrinc = secs / 10 + "0"
    @bptrinc = secs // 10 + "0"
    bptr = 100
    pause 2000
    sertxd(cr,lf,@bptrinc,@bptrinc,":",@bptrinc,@bptrinc,":",@bptrinc,@bptrinc)
 
Another way of doing it.

Code:
    hi2cin $0,(seconds,mins,hour,day,date,month,year)
    
    bcdtoascii seconds,b30,b31
    bcdtoascii mins,b32,b33
    bcdtoascii hour,b34,b35
    bcdtoascii date,b36,b37
    bcdtoascii month,b38,b39
    bcdtoascii year,b40,b41
    
    serout oled,dbaud,(254,148,"RTC Time(GMT) & Date")
    
    if b34 = "?" then
     serout oled,dbaud,(254,212,"RTC Module not Found")
     return
    endif 

    serout oled,dbaud,(254,212,b34,b35,":",b32,b33,":",b30,b31)         ;RTC Time
    serout oled,dbaud,(254,220,"  ",b36,b37,"/",b38,b39,"/20",b40,b41) ;RTC Date
    return
 
Back
Top