PIC16F887 with 7-Segment Display and Rotary Encoder | mikroC

This is another example that shows how to interface PIC16F887 microcontroller with common anode 7-segment display.
This example shows how to print rotary encoder values (positive or negative) on 4-digit 7-segment display where the first digit (most left) is used for the minus sign ( -). Here the rotary encoder is an input device and the 7-segment display is an output device.
The compiler used in this example is mikroC PRO for PIC.

To see how to interface PIC16F887 with 7-segment display (digital counter example), visit the following post:
Interfacing PIC microcontroller with 7-segment display | mikroC Projects

And to see how the rotary encoder works and how to interface it with PIC16F887 in order to control a DC motor speed, take a look at this project:
DC Motor control with rotary encoder and PIC MCU | mikroC Projects

Components Required:

  • PIC16F887 microcontroller   —->  datasheet
  • 4-digit common anode 7-segment display
  • 4 x PNP transistor (2SA10152S90152N3906 …)
  • Rotary encoder
  • 7 x 100 ohm resistor
  • 4 x 4.7k ohm resistor
  • 5V source
  • Breadboard
  • Jumper wires
  • PIC MCU Programmer (PICkit 3, PICkit 4…)

PIC16F887 with 7-segment display and rotary encoder circuit:
The image below shows our example circuit diagram.

PIC16F887 rotary encoder 7 segment display circuit

All the grounded terminals are connected together.

The rotary encoder board has 5 pins: GND, + , SW, DT (pin B or data pin) and CLK (pin A or clock pin) where:
GND is connected to circuit ground (0V)
+ is connected to +5V
SW is push button pin, not used in this example
DT is connected to PIC16F887 pin RB5 (#38)
CLK is connected to PIC16F887 pin RB4 (#37)

The 4 transistors are of the same type (PNP).

The PIC16F887 microcontroller uses its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

PIC16F887 with 7-segment display and rotary encoder C code:
The following C code is for mikroC PRO for PIC compiler, it was tested with version 7.2.0.

Since the 4 digits are multiplexed we need to refresh the display very quickly (display one digit at a time, others are off), for that I used Timer0 module (8-bit timer) interrupt with 1:16 prescaler, this means Timer0 overflows every 2048 microseconds { 256/[8/(4 x 16)] = 256 x 8 = 2048 microseconds }.

PORTB interrupt-on-change is enabled for pins RB4 and RB5 which are respectively connected to CLK and DT pins of the rotary encoder. This interrupt detects falling and rising of the two lines:

Full mikroC code:
Configuration words:
CONFIG1 = 0x2CD4
CONFIG2 = 0x0700

My hardware circuit gave me a result similar to what’s shown in the following video where Arduino UNO is used instead of the PIC16F887 microcontroller:

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top