Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

guidance for digital clock

Not open for further replies.


Advanced Member level 3
Jun 24, 2012
Reaction score
Trophy points
Activity points
I like to design digital clock using seven segment display. I'm using pic 18f2520. and thought of using 12Mhz crystal. I have some questions.
1. Which timer can i use to get correct timing without any error? It has 4 timers. which one is best?
2. Is it must to use external ic (real time clock)? Without RTC, can i get correct time in seven segment display even it runs for a month?

Thanks in advance :)

Not true - the timing depends on the accuracy of the 12MHz crystal and how you divide it down in the timers and software. There is no reason why an external RTC has to be used.

None of the timers inside that PIC will give you the 'wall clock time' directly but by using them in conjunction with software counters you can make an extremely accurate clock using software routines. My approach would be to use a counter with the best ability to divide large numbers. This would imply using Timers 0, 1 or 3 as these are all 16 bits wide. I normally use timer 1 but the others are just as good. Use the timer to generate a periodic interrupt. In that interrupt routine you count the number of times it has ben entered (if necessary) to determine a one second interval. From there you just count modulo 60,60,24,7 and so on to generate minutes, hours, days. day of week and so on. Keeping the time and date is actualy very easy in software.

you can use any timer, better if you use which has support for external clock source and which can be incremented during sleep mode(display-off ).

It is nice to emulate RTC in software, however keep in mind;
Timer should be incremented no matter what happens or you lose time setting.
For this, you should use separate Battery-cell which will only supply power to PIC and clock source during sleep or unwanted disconnection/failure of main power source.

hope it helps.
Thanks for your replies.
I like to use DS1307 and display the value in seven segment.
Please help. Please explain step by step what i have to do?

Thank you. I will look into that.
Is it possible to get 1 second delay using timer1?
What are the values to load in TMR1H and TMR1L registers?
Please explain ..

I don't know where you get your PICs from lijoppans but all the ones I've ever seen are extremely accurate and exactly predicatable. If you use timers and interrupts, at least 99% of the processing time will be available for displaying or other tasks, the RTC part of the code will be insignificant and run in the background. If you are using software timing loops to get delays you WILL have problems but thats the reason Microchip built the timers in silicon to take the problem away.

I am using a PIC to display the time and date inset in a TV picture, exerything is done in software, including all the RTC functions. It shows the time as HH:MM:SS DD-MM-YY as well as other text in the picture, handles a 38.4Kbaud serial link and a vision multiplexer at the same time. It's accurate to about 1 second per month but the time can be set via the serial link from a PC. It even works out leap years and daylight savings start/finish dates. All this is in a PIC16F628A clocked at 19.6608MHz.

Granted, you can use external segnment decoders like the 4511 but why bother when it only takes a couple of instructions in software to do almost the same thing.

Hemnath, the calculation for the timer values is simple but there are several calculators you can use if you want them. The best is probably here: **broken link removed**
Remember that timers count UP and generate an interrupt when they go beyond their maximum value and roll over to zero again so the value you use is the remaining counts before roll over, not the number of counts itself.

With a 12MHz crystal, even at the largest timer value in TMR1 you can't make a delay as long as one second so I suggest using a shorter inteval, say 10mS and counting 100 occurences to make up 1 second (100 x 10mS = 1 second). Bear in mind that you can do that in one instruction!



As shown in the 2520 datasheet Timer1 section , you just need to attach a 32khz watch crystal and that provides the 1 second timebase for a rtc thats run though your ISR.

A DS1307 uses the same type of crystal so is no more accurate, but does have the advantage of battery back up etc.

Not open for further replies.

Part and Inventory Search

Welcome to