In this post we are going to do 9 digit 7 segment multiplexing using timer0 interrupt in pic16f887 microcontroller using PIC CCS Compiler.

Here is the complete code



#include <16F887.h>
#device *= 16 ADC=10

#fuses NOWDT, HS, PROTECT, CPD, NOWRT, BROWNOUT, NODEBUG, NOLVP, PUT
#use delay(clock=16000000)
#define OUT_SEG_CODE(CODE) output_b(CODE)

#define NUM_DIGITS 9
unsigned char seg_codes[] = {0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09, 0xFF, 0xE5, 0x91};
int seg_commons[] = {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};



//====================================================================
// ISRs
//====================================================================
#INT_TIMER0
void timer0_isr(void)
{
 static int8  ms_counter=0,cur_segment=0;   
 
   
      if(cur_segment==0)output_high(seg_commons[NUM_DIGITS-1]); 
      else output_high(seg_commons[cur_segment-1]);   
      OUT_SEG_CODE(seg_codes[allDigits[cur_segment]]);   
      output_low(seg_commons[cur_segment]);
      cur_segment++;
      if(cur_segment>NUM_DIGITS)cur_segment=0;
   
   clear_interrupt(INT_TIMER0);
   set_timer0(999); 
}
//====================================================================



void main(){
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8); // Prescaler of 8
   set_timer0(999); // Set Timer0 reload value to 999 for 500 µs interval

   clear_interrupt(INT_TIMER0);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(GLOBAL);
   while(True);
}Code language: C++ (cpp)

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.