Categories
Microchip PIC16 PIC-CCS

Seven-Segment Multiplexing with RTOS on PIC16F887

Multiplexing 7 segment display is one of major problem. We try to solve this problem using RTOS in PIC CCS Compiler. In this blog post we demonstrated 9-digit seven segment multiplexing using PIC16F887 microcontroller.

Required Components

Here are the list of required components for driving 7 segment display with PIC16F887 microcontroller.

  • PIC16F887 Microcontroller
  • Seven-Segment Display (Common Anode, 9 Digits)
  • A733 NPN Transistors (9 units)
  • 1KΩ Resistors (9 units for base resistors of A733 transistors)
  • 10KΩ Resistors (9 units for base to emitter resistors of A733 transistors)
  • 7805 Voltage Regulator
  • Crystal Oscillator (e.g., 8MHz)
  • 22pF Capacitors (2 units for crystal oscillator)
  • Current Limiting Resistors (for each segment of each digit)
  • Power Supply (providing appropriate voltage and current for the microcontroller and display)
  • Decoupling Capacitors (e.g., 100nF)
  • Breadboard or PCB
  • Jumper Wires
  • PIC Programmer (e.g., PICkit)
  • Pull-up or Pull-down Resistors (if required for input pins)
  • External Components (such as buttons, sensors, etc., if needed for input)
PIC16F887

Wiring Instructions

  • Connect PIC16F887 pins to Seven-Segment Display:
    • Connect PIC pins to display segments via current-limiting resistors.
    • Connect common anode pins of each digit to A733 transistors’ collectors.
  • A733 Transistor Connections:
    • Connect A733 transistors’ emitters to ground.
    • Connect A733 transistors’ bases to PIC pins via 1KΩ resistors.
    • Connect A733 transistors’ collectors to common anode pins of display digits.
  • Power Supply and Voltage Regulator:
    • Connect power supply positive to 7805 voltage regulator input.
    • Connect voltage regulator output to PIC Vcc and display common anode pins.
    • Connect voltage regulator ground to circuit ground.
  • Crystal Oscillator and PIC Oscillator Pins:
    • Connect crystal oscillator pins to PIC OSC1 and OSC2 pins.
    • Add 22pF capacitors between OSC1 and ground, and OSC2 and ground.
  • Decoupling and Stability:
    • Add 100nF decoupling capacitors close to PIC Vcc and ground pins.
  • Input Components (if used):
    • Connect input components (buttons, sensors) to PIC pins.
    • Use pull-up/down resistors if required for input stability.
  • PIC Programmer:
    • Connect PIC programmer to PIC programming pins for programming and debugging.
  • Grounding:
    • Connect all ground (GND) points in the circuit together for a common ground reference.

PIC CCS 7 segment multiplexing Code

Here is the complete working code for the RTOS based 7 segments multiplexing 9 digits.

#include <16F887.h>

#fuses NOMCLR, NOBROWNOUT, NOLVP, INTRC_IO
#use delay(clock = 4MHz)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use rtos(timer = 0, minor_cycle = 50ms)

// Constants and pin definitions
#define NUM_DIGITS 9
unsigned char seg_code[] = {0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09, 0xFF, 0x63, 0x91};
int disp_pins[] = {PIN_C0, PIN_C1, PIN_C2, PIN_C3, PIN_D0, PIN_D1, PIN_D2, PIN_D3, PIN_C4};
unsigned char allDigits[] = {11, 2, 3, 11, 5, 6, 12, 8, 9};

// Global variables

char digitsArray[6];    // Array to store digits between 0 and 9

// Function prototypes
void multiplex();
void mdelay();

#task(rate = 50ms, max = 50ms)
void mdelayTask() {
    while (1) {
        mdelay();  // Call the mdelay function for multiplexing
    }
}

void main() {
    setup_oscillator(OSC_4MHZ);                    //internal oscillator setup
    output_b(0);                                   // All PORTB register pins are zeros
    set_tris_b(0);                                 // Configure PORTB pins as outputs
    set_tris_c(0xFF);                              // Configure PORTC pins as inputs
    set_tris_d(0xFF);                              // Configure PORTD pins as inputs
    rtos_run();                                    // Start all the RTOS tasks
}

void multiplex() {
    for (int i = 0; i < NUM_DIGITS; i++) {
        output_b(seg_code[allDigits[i]]);
        output_low(disp_pins[i]);
        delay_ms(1);
        output_high(disp_pins[i]);
    }
}

void mdelay() {
    int i = 0;
    for (i = 0; i < 13; i++) {
        multiplex();
        delay_ms(5);
    }
}
Code language: PHP (php)

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

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.