6 Digits LED 7-Segment Multiplexing using PIC16F627A

In the post β€˜LED 7-Segment Multiplexingβ€˜, I have explained about the concept and benefits of multiplexing. Multiplexing implementation is very similar to driving Led Dot Matrix. I use Timer0 interrupt for switching through each digit. Timer0 or TMR0 is an 8-bit timer which overflows every 256 (0xFF) counts. It’s known that the refresh rate above 50Hz would be enough for human’s eyes to see the display without recognizing the flickering. If I set TMR0 with 1:8 Prescaler, the multiplexing frequency will be

4MHz(internal OSC.)/4(working OSC)/8(prescaler)/256(max counts of TMR0)/6(number of digits) = 81.3 Hz which is good for a display.

Just an example, I have implemented (in Proteus) a 999999-second counter by using 6 Digits LED 7-Segment Multiplexing technique. There are 2 main components in the project, PIC16F627A or PIC16F628 and 6 x LED7-segment display. The schematic shows below. The crystal is 32.768KHz as usual. There is a 10KOhm pull up resistor at RA4 pin as this pin is an open-drain pin as I described in β€œOpen-Drain RA4 pin on PIC Microcontrollerβ€œ.

The source code in MikroC is listed below: (.hex is also available, please feel free to contact me)

6 Digits LED 7-Segment Multiplexing schematic

//PIC16F627A
//4MHzΒ InternalΒ OSC
//MUXΒ byΒ theΒ MUCΒ itselfΒ withΒ Interrupt
//TMR0Β ..Β checkΒ theΒ prescelar+delayΒ inΒ scanΒ routineΒ asΒ theyΒ areΒ related
//[email protected]
unsignedΒ shortΒ numberΒ [10]Β =Β {
0x5F,Β 0x06,Β 0x9b,Β 0x8f,Β 0xC6,Β 0xCd,Β 0xDD,Β 0x07,
0xDf,Β 0xCf
};
unsignedΒ shortΒ digitΒ [6];
unsignedΒ shortΒ counter;
unsignedΒ shortΒ shift_register;
unsignedΒ shortΒ x1;
unsignedΒ shortΒ x2;
unsignedΒ shortΒ x3;
unsignedΒ shortΒ x4;
unsignedΒ shortΒ x5;
unsignedΒ shortΒ x6;
unsignedΒ shortΒ tick;
voidΒ interruptΒ ()
{
Β Β Β Β ifΒ (INTCON.T0IF)
Β Β Β Β {
Β Β Β Β Β Β Β Β //ScanΒ digitsΒ withΒ TMR0
Β Β Β Β Β Β Β Β INTCON.T0IFΒ =Β 0;
Β Β Β Β Β Β Β Β ifΒ (counterΒ ==Β 5)
Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β PORTAΒ =Β numberΒ [digitΒ [counter]];
Β Β Β Β Β Β Β Β Β Β Β Β Delay_usΒ (500);
Β Β Β Β Β Β Β Β Β Β Β Β shift_registerΒ =Β 0x01;
Β Β Β Β Β Β Β Β Β Β Β Β PORTBΒ =Β ~shift_register;
Β Β Β Β Β Β Β Β Β Β Β Β PORTAΒ =Β 0x00;
Β Β Β Β Β Β Β Β Β Β Β Β counterΒ =Β 0;
Β Β Β Β Β Β Β Β }Β else
Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β PORTAΒ =Β numberΒ [digitΒ [counter]];
Β Β Β Β Β Β Β Β Β Β Β Β Delay_usΒ (500);
Β Β Β Β Β Β Β Β Β Β Β Β shift_registerΒ =Β shift_registerΒ <<Β 1;
Β Β Β Β Β Β Β Β Β Β Β Β PORTBΒ =Β ~shift_register;
Β Β Β Β Β Β Β Β Β Β Β Β PORTAΒ =Β 0x00;
Β Β Β Β Β Β Β Β Β Β Β Β counterΒ ++;
Β Β Β Β Β Β Β Β }
Β Β Β Β }
Β Β Β Β ifΒ (PIR1.TMR1IF)
Β Β Β Β {
Β Β Β Β Β Β Β Β TMR1HΒ =Β 0x80;
Β Β Β Β Β Β Β Β PIR1.TMR1IFΒ =Β 0;
Β Β Β Β Β Β Β Β tickΒ =Β 1;
Β Β Β Β Β Β Β Β //updateΒ currentΒ time
Β Β Β Β Β Β Β Β x6Β ++;
Β Β Β Β Β Β Β Β ifΒ (x6Β >Β 9)
Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β x6Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β x5Β ++;
Β Β Β Β Β Β Β Β Β Β Β Β ifΒ (x5Β >Β 9)
Β Β Β Β Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x5Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x4Β ++;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ifΒ (x4Β >Β 9)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x4Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x3Β ++;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ifΒ (x3Β >Β 9)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x3Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x2Β ++;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ifΒ (x2Β >Β 9)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x2Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x1Β ++;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ifΒ (x1Β >Β 9)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β x1Β =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β Β Β Β Β }
Β Β Β Β Β Β Β Β }
Β Β Β Β }
}
voidΒ mainΒ ()
{
Β Β Β Β //DigitalΒ I/OΒ forΒ PORTA
Β Β Β Β CMCONΒ =Β 0x07;
Β Β Β Β TRISAΒ =Β 0x00;
Β Β Β Β PORTAΒ =Β 0x00;
Β Β Β Β TRISBΒ =Β 0x00;
Β Β Β Β PORTBΒ =Β 0x00;
Β Β Β Β //InternalΒ ClockΒ 4MHz
Β Β Β Β PCON.OSCFΒ =Β 1;
Β Β Β Β counterΒ =Β 0;
Β Β Β Β //Β EnableΒ TMR0
Β Β Β Β OPTION_REG.T0CSΒ =Β 0;
Β Β Β Β //Β EnableΒ Prescaler
Β Β Β Β OPTION_REG.PSAΒ =Β 0;
Β Β Β Β //Β PS0,1,2Β =Β 010Β =Β 3
Β Β Β Β //Β 3Β meansΒ 1:8Β prescaler
Β Β Β Β //Β 1:2,Β 1:4,Β 1:8,Β 1:16,Β 1:32,Β 1:64,Β 1:128,Β 1:256
Β Β Β Β OPTION_REG.PS2Β =Β 0;
Β Β Β Β OPTION_REG.PS1Β =Β 1;
Β Β Β Β OPTION_REG.PS0Β =Β 0;
Β Β Β Β INTCON.T0IFΒ =Β 0;
Β Β Β Β INTCON.T0IEΒ =Β 1;
Β Β Β Β INTCON.GIEΒ =Β 1;
Β Β Β Β INTCON.PEIEΒ =Β 1;
Β Β Β Β T1CONΒ =Β 0x0F;
Β Β Β Β TMR1HΒ =Β 0x80;
Β Β Β Β TMR1LΒ =Β 0x00;
Β Β Β Β //Β EnableΒ TMR1Β interrupt
Β Β Β Β PIE1.TMR1IEΒ =Β 1;
Β Β Β Β shift_registerΒ =Β 0x01;
Β Β Β Β x1Β =Β 0;
Β Β Β Β x2Β =Β 0;
Β Β Β Β x3Β =Β 0;
Β Β Β Β x4Β =Β 0;
Β Β Β Β x5Β =Β 0;
Β Β Β Β x6Β =Β 0;
Β Β Β Β whileΒ (1)
Β Β Β Β {
Β Β Β Β Β Β Β Β ifΒ (tick)
Β Β Β Β Β Β Β Β {
Β Β Β Β Β Β Β Β Β Β Β Β tickΒ =Β 0;
Β Β Β Β Β Β Β Β Β Β Β Β //updateΒ digits
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [0]Β =Β x1;
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [1]Β =Β x2;
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [2]Β =Β x3;
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [3]Β =Β x4;
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [4]Β =Β x5;
Β Β Β Β Β Β Β Β Β Β Β Β digitΒ [5]Β =Β x6;
Β Β Β Β Β Β Β Β }
Β Β Β Β }
}

Source : 6 Digits LED 7-Segment Multiplexing using PIC16F627A


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

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.