Summary of Making a Digital Clock using PIC16F628A
Summary: The article describes a PIC16F628A-based digital clock using Timer1 for timekeeping. Running on a 20MHz crystal (instruction clock 5MHz), the author adjusts Timer1 preload and prescaler to achieve 0.1 s interrupts (TMR1 preset 0x0BDC with 1:8 prescaler) and counts ten overflows per second. They discuss initial overflow-count approach, timing errors from register-write latency, and crystal frequency drift (~20 ppm).Parts used in the Digital Clock Updated Version:
- PIC16F628A microcontroller
- 20 MHz crystal
- Timer1 module (internal to PIC16F628A)
- Prescaler (Timer1 1:8 setting)
- MikroC compiler (for code and register writes)
- Power supply (implied for PIC operation)
--- Original Version ---
As I am a WIS so I built a clock as my first microcontroller project.
The clock is controlled by PIC16F628A from the PIC book . The idea was making a digital clock with hour, minute and second display. I just wanted to learn about microcontroller so the accuracy of the clock was not an issue (yet). However, I was trying to make it the most accurate as possible by using Timer1 Module of the PIC.
The Timer1 Module is a 16 bit counter. It counts from 0x0000 to 0xFFFF and rolls over to 0x0000. The TMR1 interrupt is generated on overflow of the TMR1 register. More information about TMR1 is available in the data sheet of the PIC. In my clock, the PIC is running with 20MHz crystal so the internal clock is 5MHz. So, the duration between each TMR1 increment is 1/5000000 = 0.2 µs . That means TMR1 will overflow every 0.2 µs x 65536 = 0.0131072 s. If I count number of overflows to 77 times, I will get 1.0061744 s which is close to 1 second but the clock will gain about 6ms every second. The result will be a fast running clock (gaining about 9 min/day) .
To get a better accuracy, I make TMR1 counts to 62500 and set prescaler of the TMR1 to 1:8 (How these 62500 and 1:8 come? I did guess and test) . From these settings, the TMR1 will overflow every 1/5000000 x 8 x 62500 = 0.1 s. Counting number of overflows to 10 will yield 1 second. Just perfect huh!
Making Timer1 counts to 62500
It's easy. Just make it starts counting at 65536-62500 = 3036 = 0x0BDC by setting: (MikroC compiler)
Setting TMR0 requires 2 clock cycles but I have no idea about how many cycles required for setting TMR1L. If 2 cycles were required for setting TMR1L, I should set TMR1L to 0xDC+2 = 0xDE so my clock will run more accurate. The frequency drift in the crystal is also a source of inaccuracy of the clock. The time drift from crystal can be calculated by using data from data sheet of the crystal. Normally, it says 20ppm (Part-Per-Million). That means the crystal will produce error about 20 s. in 1,000,000 s. or about 1.7 s./day.
For more detail: Making a Digital Clock using PIC16F628A
Quick Solutions to Questions related to the Digital Clock Updated Version:
- What microcontroller is used in the clock?
The PIC16F628A microcontroller is used. - What crystal frequency does the project use?
The project uses a 20 MHz crystal. - How is Timer1 configured to generate 0.1 second intervals?
Timer1 is preset to 62500 counts with a 1:8 prescaler so each overflow represents 0.1 s. - What register values set TMR1 to start at 62500 counts?
Set TMR1L = 0xDC and TMR1H = 0x0B to preload the timer to 0x0BDC (65536-62500). - How many Timer1 overflows are counted to make one second?
Ten overflows of the 0.1 s Timer1 interval are counted to make one second. - Why was an initial overflow-count method of 77 used and why was it inaccurate?
Counting 77 overflows of the raw 16-bit Timer1 (with no prescaler) gave ~1.0061744 s per set, causing a fast clock gaining ~9 minutes per day. - Does writing TMR1L introduce timing error and how is it handled?
Yes, register-write cycles may introduce error; the author notes uncertainty about write cycle count and considers adjusting the preload if writes take two cycles. - How significant is crystal frequency drift for accuracy?
The crystal drift is about 20 ppm, causing roughly 1.7 seconds per day error as noted from the crystal datasheet.
Related projects
DHT11 Low Cost Humidity Sensor using PIC16F628A with Proteus Simulation
IntroductionThis microcontroller project demonstrates how to interface a low-cost DHT11 temperature and humidity sensor with a PIC16F628A using a Proteus simulation environment.The...
LCD & Display Projects · PIC16F628AFrequency Counter by PIC16F628
In this project we are building a basic and low cost frequency counter circuit . It can measure from 16Hz to 100Hz...
Games, Toys & Calculators · PIC16F628ATIC TAC TOE game using PIC16F628
This project is not to introduce Tic Tac Toe. Everyone knows this game. It's to introduce two features. Bi-coloured LEDs, microcontrollers and...
Clocks & TimersAdaptive Alarm Clock: Build a Smart, User-Centric wake-Up system
Project Overview The goal of this ambitious undergraduate project was to develop a novel alarm clock capable of analyzing a person's unique...
Clocks & TimersDesigning Your Ideal Alarm Clock: An Introduction to PIC18
Within this laboratory session, you'll employ the C language to create an alarm clock, delving into the implementation of "C" programs on...
Clocks & TimersBuilding your Dream Alarm Clock Using the PIC18
Objective: In this laboratory session, your objective is to create an alarm clock using the C programming language. You will gain proficiency...

