Electronics

Projects
  Kodak DC20 Controller

PIC Microcontrollers
  Getting Started
  16F84

Reference
  Resistor Color Codes
  Resistance Calculator
  Ohm's Law

 

 

Driving a 5-digit, 7-segment LED Display With a PIC 16F84

Purpose

The purpose of this demonstration is to control a 5 digit, 7-segment LED display with a PIC 16F84 microcontroller.

Parts Used5 Digit, 7-Segment LED Display

  • 1x PIC 16F84
  • 1x 5 Digit, 7 Segment LED Display
  • 1x 4 MHz Crystal Resonator
  • 1x 10K Ω pull-up resistors
  • 8x 680 Ω current limiting resistors
  • Theory of Operation

    The 5 digit, 7-segment display is capable of displaying 5 numeric digits at once — sort of.

    The device only has 14 pins. This is no where near enough pins to account for each individual LED in the device (5 digits times 7 segments plus decimal point per digit = 40 individual LEDs). Even if all the cathodes or all the anodes were connected together, the device would need a minimum of 41 pins.

    In order to reduce the number of pins in the device, a clever arrangement of common cathodes and common anodes is employed. Internally, the eight cathodes in each digit are connected together. These common cathode connections are the "digit select" lines and make up 5 of the pins on the device (one common cathode connection per digit).

    The anodes are connected across the digits such that the anode for a segment is connected to the corresponding anode in all 5 digits. Since there are 8 segments (7 plus the decimal point) per digit, there are 8 common anode connections. These common anode connections, or "segment-select" lines, make up 8 more pins.

    Since there is no such thing as a 13 pin DIP, the 14th pin is simply not connected to anything.

    Operating the device is fairly straight forward. By selectively connecting a digit-select line (one of the 5 common cathodes) to ground and applying a positive voltage to any of the segment-select lines (common anode), it is possible to illuminate any segment of any digit.

    The problem with this arrangement is that it is not possible to display different segment patterns on each digit at exactly the same time. For instance, to display a numeral on the first digit, the first digit-select line would be connected to ground and the appropriate segment-select lines would be connected to the positive voltage supply. If the second digit-select line were also connected to ground, the second digit would display the same numeral as the first digit.

    The solution is to select only one digit at a time. By changing the selected segments as each digit is selected, it is possible to display different numerals on each digit, one at a time. If this switching happens fast enough, it will appear as though each digit is displaying a different numeral simultaneously. The blinking will not be perceptable, unless the display device is moving with respect to the viewer at a fairly good rate. This phenomenon can be observed by waving an LED wrist-watch or alarm clock back and forth rapidly.

    The PIC 16F84 is an ideal microcontroller to drive one of these display units. I/O Port A on the 'F84 is 5 bits wide: one bit per digit. I/O Port B on the 'F84 is 8 bits wide: one bit for each segment. Software on the microcontroller can switch the I/O port bits on and off in the correct pattern rapidly enough1 to give the illusion that each digit is displaying a different numeral.

    5-Digit, 7-Segment LED Display

    5-Digit, 7-Segment LED Display Pin-out
    ds1  -  Digit 1 Select
    e  -  Segment E Select
    f  -  Segment F Select
    ds3  -  Digit 3 Select
    dp  -  Dec. Pnt. Select
    g  -  Segment G Select
    ds5  -  Digit 5 Select
    d  -  Segment D Select
    ds4  -  Digit 4 Select
    b  -  Segment B Select
    nc  -  No Connection
    c  -  Segment C Select
    ds2  -  Digit 2 Select
    a  -  Segment A Select

    The Circuit

    No circuit diagram prepared.

    The circuit is very simple. Port A, bits 0-4 are connected to the digit select lines directly. Port B, bits 0-7 are connected to the segment select lines through 680Ω resistors. A 10KΩ pull-up resistor connects the MCLR line of the PIC to +5V. A 4MHz crystal resonator is connected to the OSC1/CLKIN and OSC2/CLKOUT pins of the PIC, with the center lead connected to Gnd. Vss and Vdd are connected to Gnd and +5V, respectively.

    PIC 16F84 Code

    ;5d7s.ASM
    ;Drive a 5 digit, 7 segment LED display
    
    	list    p=16F84
    
    porta	equ	0x05
    portb	equ	0x06
    delay	equ	0x0c		;first general purpose register
    digit	equ	0x0d
    digmask	equ	0x0e
    value	equ	0x0f
    
    	org	0x000
    	goto    start
    	org	0x004
    	goto	isrv
    
    start
    ;configure all port A bits for output
    	clrw
    	tris	porta
    
    ;configure all port B bits for output
    	clrw
    	tris	portb
    
    loop
    	movlw	b'00100100' ;one
    	movwf	value
    	movlw	0x01
    	call	displaydigit
    	movlw	b'00101110' ;four
    	movwf	value
    	movlw	0x02
    	call	displaydigit
    	movlw	b'01101101' ;three
    	movwf	value
    	movlw	0x03
    	call	displaydigit
    	movlw	b'00000000' ;blank
    	movwf	value
    	movlw	0x04
    	call	displaydigit
    	movlw	b'00000000' ;blank
    	movwf	value
    	movlw	0x05
    	call	displaydigit
    
    ;loop forever
    	goto	loop
    
    
    
    d250
    	movlw	0x52
    	movwf	delay
    l250	decfsz	delay,f
    	goto	l250
    	return
    
    displaydigit
    	movwf	digit
    	movlw	b'11111111'
    	movwf	digmask
    	addlw	0x00 ; clear carry bit
    displaydigitrotateloop
    	rlf	digmask,f
    	decfsz	digit,f
    	goto	displaydigitrotateloop
    	movfw	digmask
    	andlw	b'00011111'
    
    ;select a digit
    	movwf	porta
    
    ;display a value on the selected digit
    	movfw	value
    	movwf	portb
    
    ;wait for a while
    	call	d250
    
    	return
    
    isrv
    	retfie
    
    	end
    

    Improvements

    The code as written can only display a static value. A useful enhancement would be to read the digit values from memory addresses, and have these addresses set by some other process, perhaps a counter based on TMR0.

    The digit select lines could be connected to a binary de-multiplexer so that only three lines would be needed to select one of 5 digits. This would free up two I/O pins, which could be used for serial communications between the PIC and another device.


    1 The fact is, operating at 4MHz, the PIC is too fast. The I/O pins under a load cannot switch on and off as fast as the microcontroller can instruct them to. Because of this physical limitation, a delay must be built in to the software.

     

    Tell me what you think