Rolando Andrade
Back to projects

Built in 2019

PIC 16F84A chronometer

PIC 16F84A Chronometer

PIC 16F84A chronometer preview

Chronometer programed with a PIC 16F84A and implemented as a Computer Architecture course project.

The assignment fit in one line: build a chronometer with a PIC microcontroller. It was for my Computer Architecture course. No high level languages. No libraries. Only assembly and a handful of hardware parts.

A chronometer is a stopwatch. You use one to debug a circuit or to measure a reaction time. It sounds like a small project. On this chip, every instruction had to earn its place.

The PIC 16F84A is a classic 8 bit microcontroller from Microchip. It has 1KB of program memory, 68 bytes of RAM, and it runs at 4 MHz. With so little room, you cannot waste cycles or memory. You see the registers, the interrupts and the timing loops that high level programming hides from you.

Four digits, one button

The chronometer shows time on four 7 segment displays. Two digits show seconds (00 to 59). Two digits show centiseconds (00 to 99). One button starts and stops the timer. Another button resets it.

The display uses multiplexing. Only one digit lights up at a time. The chip switches between them so fast that your eye sees all four at once. This saves I/O pins (the chip’s input and output connections). Four displays would need 28 pins. With multiplexing, I use 12.

Time comes from the chip’s internal timer. An interrupt fires every 10 milliseconds to update the count. An interrupt is a signal that pauses the main program and runs a short handler. The main loop only refreshes the display.

Two ports, many jobs

The circuit connects to the PIC through two ports.

Port B drives the 7 segment displays. Seven pins choose which segments light up. The eighth pin reads the start and stop button.

Port A chooses which digit is active. Four pins cycle through the four displays. The fifth pin reads the reset button.

Each display has a common cathode. When its control pin goes high, current flows through the lit segments. The pattern on Port B decides which segments glow.

The encoding follows the standard convention. Each digit from 0 to 9 has its own bit pattern. A lookup table in program memory holds these patterns.

Counting with TMR0

The PIC 16F84A has an 8 bit timer called TMR0. It counts clock cycles and fires an interrupt when it overflows from 255 to 0.

With a 4 MHz crystal and a prescaler of 256, each timer tick is 256 microseconds. A prescaler divides the clock so the timer counts slower. Loading the timer with 217 gives about 10 milliseconds between interrupts. That is a 100 Hz timebase, one tick per centisecond.

The interrupt handler does the time arithmetic. It adds one to the centiseconds counter. When that counter reaches 10, it goes back to 0 and the tens of centiseconds go up by one. Seconds and tens of seconds follow the same pattern. The tens of seconds counter wraps at 6, so the display stays under 60 seconds.

Drawing digits in a loop

The main loop cycles through all four digits, fast. For each digit it does seven steps:

  1. Load the current time value for that position
  2. Look up the 7 segment pattern in the conversion table
  3. Output the pattern to Port B
  4. Enable that digit’s control line on Port A
  5. Wait a short delay
  6. Disable the control line
  7. Move to the next digit

The delay routine is a simple countdown loop. Each pass takes a known number of cycles. That gives steady timing without the hardware timer, which is already busy with the interrupt.

A table made of returns

The lookup table converts a number into its 7 segment pattern with a computed jump. The value is an offset into a table of return instructions. Each return carries the segment pattern as immediate data.

The table covers digits 0 to 9. The digit 8 lights all seven segments. The digit 1 lights only two. The patterns use active low logic, so a 0 bit turns a segment on.

Start, stop and reset

The start and stop button sits on Port B pin 7. When you press it, the pin reads low. The program enables the timer interrupt and starts counting. When you release it, the pin reads high. The program disables the interrupt and freezes the display.

The reset button on Port A pin 4 works differently. Pressing it clears the interrupt control register. The count stops and the program goes back to the wait state.

Buttons bounce. A switch flickers between on and off for a moment when you press it. Here, debouncing came for free from the display loop timing. The loop runs slow enough that the bounce settles before the next check.

Fitting in 95 words

The PIC 16F84A uses a Harvard architecture. Program memory and data memory are separate. Program memory starts at address 0. The interrupt vector sits at address 4.

The main code starts at address 80 (hexadecimal). That leaves room for the interrupt handler near the beginning. General purpose registers hold the time values and temporary variables.

The whole program fits in about 95 words of program memory. That is less than 10% of the space. Embedded systems often have tighter limits, but this project stayed well inside them.

What went wrong first

Timing accuracy needed careful math. The crystal frequency, the prescaler and the timer load value all change the interval. Getting exactly 10 milliseconds took trial and error in the simulator.

Display brightness changed with the refresh rate. Too slow, and the digits flickered. Too fast, and they looked dim because each digit got less time on. Finding the balance took experiments.

Context switching in the interrupt handler needed care. The handler saves the working register when it starts and restores it when it ends. Forget that, and the main program’s math breaks.

MPASM and Proteus

MPASM assembled the source code into machine code. It produces a HEX file that you program into the chip.

Proteus simulated the circuit before I built any hardware. I could run the code, watch the registers and check the timing without burning chips.

The DSN file in the repository is the Proteus schematic. It includes the PIC, the displays, the buttons and the power supply.

The machine has no secrets

Assembly made me understand the machine. Every instruction matters. There are no abstractions to hide behind.

Interrupts changed how I structure code. The main loop and the interrupt handler run at the same time, so shared variables need care.

Hardware and software touch each other directly. A mistake on either side breaks the system. Debugging means thinking about both at once.

Now I know how hard a compiler’s job is. Turning high level logic into efficient machine code is difficult. The patterns from this chronometer helped me write better code in every language since.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build