Summary of PIC16F877 stop watch code and Proteus simulation
This article explains implementing a countdown stop watch on a PIC16F877 using Timer0-generated 1 ms interrupts, an LCD display, three RE0–RE2 pushbuttons for time setting, and an LED that lights when time reaches zero. The code is in C (MPLAB with HI-TECH C) and can be simulated in Proteus; downloads are provided.Parts used in the PIC16F877 Stop Watch:
- PIC16F877 microcontroller
- 16x2 LCD module
- Timer0 (internal peripheral)
- Three push buttons (connected to RE0, RE1, RE2)
- LED D1
- Resistors for LCD, LED and buttons (pull-ups/pull-downs as required)
- Crystal oscillator or clock source for PIC
- Proteus simulation software (for verification)
- MPLAB IDE with HI-TECH C compiler
Using PIC16 simulator (Proteus) you can verify this stop watch code and change it according to your needs. Using three push buttons (As shown in figure below) you can adjust time as you desire. Then after setting the time, value displayed on the LCD starts to decrease with each second and when this value reaches zero, then LED D1 is turned on. This code is written in C language using MPLAB with HI-TECH C compiler. You can download this code from the 'Downloads' section at the bottom of this page.
In this article, it is assumed that you know,
- How to make a simple digital clock using PIC16F877. If you don't then please read this page.
- How to interface LCD with PIC16F877 microcontroller. If you don't then please read this page.
- How to configure timer0 of PIC16F877 microcontroller. If you don't then please read this page.
The following diagram (made in Proteus) shows the PIC microcontroller circuit diagram.
The above figure was taken when time was 00:00:07, i-e only 7 seconds were remaining until LED D1 is turned on. Timer0 is used as the base for digital clock generation. Timer0 is used here to generate 1msec interrupts. After every 1msec a global variable named msCounter increments. When msCounter reaches a value of 1000 then another global variable named secCounter decrements and this process repeats itself. Similarly, when secCounter reaches 0, then minCounter decrements. And when minCounter reaches 0 then hrCounter decrements. This process continues until hrCounter reaches 0. LCD is updated with the new values of hrCounter, minCounter and secCounter after every second.
You can set time using three push buttons attached on RE0, RE1 and RE2 pins (As shown in the above figure). By pressing 'Set Time' button one time, code enters in configuration state. Hours value starts to blink and you can modify it using Up and Down buttons. Pressing Up button increments the value and pressing Down button decrements the value. When you are done setting Hours value, press 'Set Time' button again, then Minutes value will start to blink and you can adjust this value using Up and Down buttons. Similarly, after setting Minutes value, you can press 'Set Time' button again, then Seconds value will start to blink and you can adjust this value using Up and Down buttons. When you are done adjusting the time, then press 'Set Time' button for the last time and this stop watch will start to work normally.
Code
The main function code is shown below.
Downloads
Stop watch clock display code using PIC16F877 was compiled in MPLAB v8.85 with HI-TECH C v9.83 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.
For more detail: PIC16F877 stop watch code and Proteus simulation
Quick Solutions to Questions related to PIC16F877 Stop Watch:
- How is timekeeping implemented in the stop watch?
Timer0 generates 1 ms interrupts; msCounter increments every 1 ms and when it reaches 1000 secCounter decrements, cascading to minutes and hours. - Can I set hours, minutes, and seconds on this stop watch?
Yes; pressing the Set Time button cycles through hours, minutes, and seconds which blink and are adjusted with Up and Down buttons. - What happens when the countdown reaches zero?
When hrCounter reaches 0 (time reaches zero), LED D1 is turned on. - Which pins are used for the time-setting buttons?
The three push buttons are attached to RE0, RE1 and RE2 pins. - How is the LCD updated?
The LCD is updated with hrCounter, minCounter, and secCounter values after every second. - What development tools and simulation software were used?
Code was compiled in MPLAB v8.85 with HI-TECH C v9.83 and simulated in Proteus v7.10. - Does the article provide downloadable code and simulation files?
Yes; download links for the code and Proteus simulation are provided in the Downloads section. - How do Up and Down buttons function during configuration?
Pressing Up increments the current blinking value (hours/minutes/seconds) and Down decrements it.
Related projects
PIC16F877 Driving LCD in 4-Bit Mode using PIC16F877 with Proteus Simulation
Introduction This microcontroller project shows how a PIC16F877 can drive an LCD display using 4-bit mode inside a Proteus simulation.The project demonstrates...
Programmers & ICSP · PIC16F877AEPE PIC/LCD Demo using PIC16F877 with Proteus Simulation
IntroductionThe EPE PIC/LCD Demo is a classic microcontroller project that demonstrates how a PIC16F877 can fully control a PG12864F graphical LCD.Designed as...
Motors & Robotics · PIC16F877AEfficient Sun-Following Robot: A Solar Tracker with Microcontroller Control
Abstract The impending global challenge of dwindling non-renewable fuel resources underscores the urgency of transitioning towards renewable energy alternatives like solar power....
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...

