Class 7 - Simple RPM Counter

May 26, 2025

Hi buddies! In this post, let’s explore the Timer and Counter feature in the Buddy51 board.

Why use a Timer instead of NOP?

In our previous examples, we used NOP-based software delays. However, these are not accurate. To achieve precise timing, we use hardware-based timers and counters.

What is a Timer or Counter?

A timer or counter is a register that increments with each clock pulse. If the pulse is from an external pin, it’s a counter. If it’s from the system clock, it’s a timer. Once it overflows, a flag is set, which we use to take action.

Think of it like an alarm on your phone. When time is up, the alarm rings. Similarly, in the MCU, the flag is set when time is up.

Example: RPM Meter

An RPM meter counts the number of pulses from a proximity sensor in one second. Timer0 acts as the counter, and Timer1 is used for the 1-second delay.

Number of Timers

The Buddy51 has 3 timers:

  • Timer0
  • Timer1
  • Timer2

This can vary depending on the IC manufacturer.

Timer Modes in Buddy51

Buddy51 supports four timer modes: Mode 0, Mode 1, Mode 2, and Mode 3.

  • Mode 0: 13-bit timer, max count 8191. Rarely used.
  • Mode 1: 16-bit timer, max count 65536. Most commonly used.
  • Mode 2: 8-bit auto-reload mode. TL is counter, TH is reload value.
  • Mode 3: Split Timer Mode. Timer0 is split into two 8-bit timers. Timer1 is disabled in this mode.

Timer Initialization Steps

  1. Select Timer and Mode
  2. Load Timer Value
  3. Start Timer
  4. Check Overflow

Step 1: Timer and Mode Selection using TMOD

The TMOD (Timer Mode) register is an 8-bit register used to configure Timer0 and Timer1.

Bit76543210
LabelGATE1C/T1M1M0GATE0C/T0M1M0
  • GATE: Allows external signal to control timer
  • C/T: Selects between Counter (1) and Timer (0)
  • M1/M0: Select timer mode

Example: TMOD = 0x01; configures Timer0 in Mode 1.

Step 2: Load Timer Value

In 16-bit Mode, to generate 50ms delay with 11.0592MHz clock:

  • Machine cycle ≈ 1.085 microseconds
  • Counts needed = 50,000 / 1.085 ≈ 46080
  • Initial value = 65536 – 46080 = 19456 = 0x4C00

So, set:

Step 3 & 4: Start Timer and Check Overflow

Use TCON (Timer Control Register):

Bit76543210
LabelTF1TR1TF0TR0IE1IT1IE0IT0
  • TR0: Start Timer0
  • TF0: Timer0 Overflow Flag

When TF0 is set, delay is complete.

Example: RPM Meter using Counter

Sensor input connected to P3.4 (T0 pin). Timer0 is Counter, Timer1 is 1s delay generator.

That’s it! Your basic RPM meter using Buddy51 is ready!

What’s Next?

In the next post, we’ll use timer + interrupt to build a real-time application.

Leave a reply
Ultrasonic Sensor Distance Calculation – BuddyKitClass 8 – Servo Motor

Leave Your Reply