;Thisprogramdisplaya24htimerona2x16LCD.;Itimplementsthezero-error1secalgorithmfromRomanBlackusingTimer0;See:http://www.romanblack.com/one_sec.htm;;ThisGCBasicversionisderivedfrom:;http://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.SoftRTC;;JNMay2014;;ChipSettings;#chip16F1508,8'Does not work with 16F1508. OSCCON redefined in init sub #chip 16F690,8 'UsedwithRealPICsimulator;Defines(Constants)#define LCD_IO 4#define LCD_RS PORTB.6#define LCD_NO_RW#define LCD_Enable PORTB.5#define LCD_DB4 PORTC.0#define LCD_DB5 PORTC.1#define LCD_DB6 PORTC.2#define LCD_DB7 PORTC.3OnInterruptTimer0OverflowCallIncCounterDimOneSecond,Cyc_CounterAsLong') These are theDim T0Load as byte alias Cyc_Counter ')3keyDimCountdownaswordAliasCyc_Counter_U,Cyc_Counter_H') lines !Dim hrs,mins,secs as byteDim update As BitOneSecond = 2000000-2 '1sec=8MHZ/4-TMR0startupadjustment;;ThefollowingroutineiscalledaftereachTimer0overflow;subIncCounterCountdown--IfCountdown=0ThenCyc_Counter=OneSecond' reload timer TMR0 = TMR0 + T0Load 'includeadditionalTMR0countsIfSTATUS.0=0ThenCountdown++'if no carry increment "countdown" counter once End If set update on 'flagtoupdateclockoutputEndIfEndSub;;Initialization;SubInitialize()CLSsecs=0mins=0hrs=0Cyc_Counter=OneSecondsetupdateoffOPTION_REG=0b00001000'PSA on so no prescaler on Timer0INTCON = 0b10100000 'GIEon,T0IEon(turninterruptson);Followingstatementonlyneededfor16F1508;OSCCON=0b01110000'Insure running at 8Mhz with internal osc on 1508TMR0 = 0 'Timer0setto0EndSub;;Displaya24Htimerona2x16LCDdisplay.FormatisHHhMMmSSs;SubClock24()secs++Ifsecs=60Then'Checkeachtallyforrolloversecs=0mins++Ifmins=60Thenmins=0hrs++Ifhrs=24Thenhrs=0EndIfEndIfEndIflocate1,0Printpad0(hrs)+"h"Locate1,3Printpad0(mins)+"m"Locate1,6Printpad0(secs)+"s"setupdateoffEndSub;;Doeswhatitisnamedafter:-);functionpad0(invar)asstringifvar<10thenpad0="0"+str(var)elsepad0=str(var)endifendFunction;;MainProgram;InitializeDoifupdatethenClock24endifLoop
Last edit: Jacques Nilo 2014-05-19
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2014-05-09
Thanks! I'm looking forward to playing with this, timekeeping being one of my hobbies.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am also discovering the fascinating world of timers :-)
I changed the code of my initial post to replace the 16F1508 by a more traditional 16F690 in order to avoid the OSCCON compile problem with this chip. I then compiled the stuff, imported the Hex file into real pic simulator, initialized a virtual LCD screen with the code ports setup (RS=PORTB,PIN6 / E=PORTB,PIN5 / D4=PORTC,PIN0 ...) and run. Et voila ! Here is the result and I was then able to check my clock against the one of my PC...
Last edit: Jacques Nilo 2014-05-10
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Timers can be really interesting to work with.
Recognize though that when you run it in the simulator it's using the PC clock as the time base so they should match up. When you run it on an actual PIC it will be less accurate over time due to the crystal accuracy.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Last edit: Jacques Nilo 2014-05-19
Thanks! I'm looking forward to playing with this, timekeeping being one of my hobbies.
I am also discovering the fascinating world of timers :-)
I changed the code of my initial post to replace the 16F1508 by a more traditional 16F690 in order to avoid the OSCCON compile problem with this chip. I then compiled the stuff, imported the Hex file into real pic simulator, initialized a virtual LCD screen with the code ports setup (RS=PORTB,PIN6 / E=PORTB,PIN5 / D4=PORTC,PIN0 ...) and run. Et voila ! Here is the result and I was then able to check my clock against the one of my PC...
Last edit: Jacques Nilo 2014-05-10
Timers can be really interesting to work with.
Recognize though that when you run it in the simulator it's using the PC clock as the time base so they should match up. When you run it on an actual PIC it will be less accurate over time due to the crystal accuracy.