Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : AM/PM code for RTC...
Author | Message | ||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
Hi folks. While playing around with some I2C stuff tonight(let us never speak of that other thread again! ), I wrote a little snippet of code for manipulating time$ into either AM or PM display if you prefer a 12Hr clock, which I do. It might be of use to someone. Let me know if you see where the code could be improved, but it seems to do all that is asked of it. CLOCK: Print Date$,Time$ 'Show current system date and time Print:Print 'Blank lines X=Val(Mid$(Time$,1,2)) 'Calculate VALue of 'HOURS' from current time If X>12 Then 'Time is PM... H=X-12:AP$=" PM" 'Subtract 12 from current hour and set AM/PM indicator and space If H<10 Then 'Hour needs a leading zero... HOUR$="0" + Str$(H) '...so add one Else HOUR$=Str$(H) 'Hour is double-digit, so just turn that into a string EndIf Else H=X:AP$=" AM":HOUR$=Mid$(Time$,1,2) 'Time is AM... If HOUR$="00" Then HOUR$="12" 'If midnight, set hour to 12 EndIf AMPM$=HOUR$+Mid$(Time$,3,7)+AP$ 'Build new time$ variable Print AMPM$ 'Show new AM/PM clock. Smoke makes things work. When the smoke gets out, it stops! |
||||
Chris Roper Senior Member Joined: 19/05/2015 Location: South AfricaPosts: 280 |
Thanks Groggy, quick question, Does this code require Pull Up Resistors... /Me Ducks and runs very fast... Looks good :) Cheers Chris http://caroper.blogspot.com/ |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
.....Fuuuuunnnnnnnyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy..................... Smoke makes things work. When the smoke gets out, it stops! |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
Chris it was good you did that edit Bob |
||||
Chris Roper Senior Member Joined: 19/05/2015 Location: South AfricaPosts: 280 |
Yes, that was a bad typo indeed :) http://caroper.blogspot.com/ |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
@ Chris - I was being sarcastic in that post above - I was not being snooty towards you or anything. I can take my medicine. If you can't laugh at your mistakes and take others jokes about your mistakes, then life just isn't worth it anymore! It's all good value and fun, and I am not offended by anyone taking the micky about that other thread - I deserve it. I doubt that I will forget to triple check the pull-up resistors again with I2C for as long as I live. ......but I digress....... Smoke makes things work. When the smoke gets out, it stops! |
||||
Chris Roper Senior Member Joined: 19/05/2015 Location: South AfricaPosts: 280 |
The Typo Bob and I were referring too was that my original post had an I in the word Duck :) I knew you well enough to know you wouldn't take offence or I wouldn't have pulled your leg in the first place. http://caroper.blogspot.com/ |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
Okey dokey, thanks. Just checking. Sometimes easy to be misinterpreted. Smoke makes things work. When the smoke gets out, it stops! |
||||
busa Regular Member Joined: 12/02/2015 Location: AustraliaPosts: 81 |
Hi everyone, Have used this code which is similar to that of @Grogster. clock:
hour = VAL(left$(TIME$, 2)) min = val(mid$(time$,4,2)) sec = val(right$(time$,2)) ho = hour if hour = 0 then ho = 12 if hour >12 then ho = ho - 12 'format for 12 hour time if hour <12 then ampm$ = "AM" else ampm$ = "PM" 'format for 12 hour time AM or PM hou$ = str$(ho,2,0,"0") 'format hours with leading zeros mi$ = str$(min,2,0,"0") 'format minutes with leading zeros se$ = str$(sec,2,0,"0") 'format seconds with leading zeros ti$ = hou$ + ":" + mi$ + ":" + se$ + space$(1) + ampm$ 'ti$ is new time variable lcd 1,C20, ti$ 'send time to lcd display ireturn Seems to work quite well on my desktop clock, hopefully will be of use to someone. |
||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
I love this stuff. Makes my library better and better. Thanks Grogster and Busa!!!! |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 5908 |
Gogster, Did you try the clock at lunchtime? You need to make allowances for that first hour after midday when it is 12:xx What to do with midday and midnight is always a problem. ' TassyJim ' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' CLOCK: PRINT DATE$,TIME$ 'Show current system date and time PRINT:PRINT 'Blank lines X=VAL(MID$(TIME$,1,2)) 'Calculate VALue of 'HOURS' from current time IF X>12 THEN 'Time is PM... H=X-12:AP$=" PM" 'Subtract 12 from current hour and set AM/PM indicator and space IF H<10 THEN 'Hour needs a leading zero... HOUR$="0" + STR$(H) '...so add one ELSE HOUR$=STR$(H) 'Hour is double-digit, so just turn that into a string ENDIF ELSE H=X:AP$=" AM":HOUR$=MID$(TIME$,1,2) 'Time is AM... IF HOUR$="00" THEN HOUR$="12" 'If midnight, set hour to 12 ENDIF AMPM$=HOUR$+MID$(TIME$,3,7)+AP$ 'Build new time$ variable PRINT AMPM$ 'Show new AM/PM clock. PRINT mytime$(TIME$) PRINT mytime$("00:00:00") PRINT mytime$("00:00:01") PRINT mytime$("11:59") PRINT mytime$("12:00:00") PRINT mytime$("13:00:00") PRINT mytime$("23:59:59") END FUNCTION mytime$(Ttime$) ' IF Ttime$ >= "13:00:00" THEN 'PM mytime$= RIGHT$("00"+STR$(VAL(Ttime$)-12),2)+MID$(Ttime$,3)+" PM" ELSEIF Ttime$ > "12:00:00" THEN 'PM first hour mytime$= Ttime$+" PM" ELSEIF TTime$="12:00:00" OR TTime$="12:00"THEN mytime$="Midday" ELSEIF TTime$="00:00:00" OR TTime$="00:00"THEN mytime$="Midnight" ELSEIF Ttime$ < "01:00:00" THEN ' early AM mytime$= "12"+MID$(Ttime$,3)+" AM" ELSE ' AM mytime$= Ttime$+" AM" ENDIF END FUNCTION Output: DOS MMBasic Version 4.5
Copyright 2011-2014 Geoff Graham 10-10-2015 12:26:18 12:26:18 AM 12:26:18 PM Midnight 12:00:01 AM 11:59 AM Midday 01:00:00 PM 11:59:59 PM > I think all the various possibilities are covered. I prefer 24hr time... Jim VK7JH MMedit MMBasic Help |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9066 |
Jim, yes I did spot that lunchtime error, but ignored it - I wanted to see how long it took for someone else to pick up on that. "Not long." is the answer. Yeah, if I issue 'time$="12:34:50" or similar at the command prompt, then call the CLOCK routine, it shows as "12:34:50 AM" I think, and that is totally out by 12 hours. I will try your code. Smoke makes things work. When the smoke gets out, it stops! |
||||
Print this page |