PIC16F877A Microcontroller Tutorial using Proteus | applied electronics engineering


PIC16F877A Microcontroller Tutorial using Proteus

By Applied Electronics - Monday, October 7, 2013 No Comments
In this post, a 7 segment display controlled by PIC16F877A microcontroller simulation in Proteus 8 will be shown.

The schematic for using PIC16F877A to produce characters on a 7 segment display is shown below-

PIC16F877A Microcontroller Tutorial using Proteus

 The parts that are used are-
  • PIC16F877A microcontroller
  • 7SEG-COM-CAT-Blue
  • 9C04021A2200FLHF3(220 ohm resistor)
  • 9C08052A1002JLHFT(10k ohm resistor)
  • DIPSW_4
 The assembly source code is below-

; setup and configuration
    processor 16f877A
    include      <p16f877A.inc>
    __config  _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF & _LVP_OFF

#define Pb_sw  4    ; Port A line 4 to push button switch

porta    equ    0x05
portb   equ     0x06
;      local variables
    cblock    0x20        ; Start of block
    J                ; counter J
    K                ; counter K
;                           program
    org    0      ; start at address 0
    goto    main
; Space for interrupt handlers
    org        0x08

    bsf STATUS,RP0
; Port A. Five low-order lines set for for input
    movlw    B'00011111'    ; w = 00011111 binary
    movwf    TRISA        ; port A (lines 0 to 4) to input
; Port B. All eight lines for output
    movlw    B'00000000'    ; w := 00000000 binary
    movwf    TRISB        ; port B to output
    movlw    0x6
    movwf    ADCON1
    bcf        STATUS,RP0
; Pushbutton switch processing
; Push button switch on demo board is wired to port A bit 4
; Switch logic is active low
    btfss    PORTA,Pb_sw    ; Test and skip if switch bit set
    goto    buzzit        ; Buz if switch ON,
; At this point port A bit 4 is set (switch is off)
    call    buzoff        ; Buzzer off
    goto    readdip        ; Read DIP switches
    call    buzon        ; Turn on buzzer
    goto    pbutton
;   dip switch monitoring
; Read port A switches
    movf    PORTA,w        ; Port A bits to w
; Since board is wired active low then all switch bits
; must be negated.  This is done by XORing with 1-bits
    xorlw    b'11111111'    ; Invert all bits in w
; Mask off 4 high-order bits
    andlw    b'00001111'        ; And with mask
; At this point the w register contains a 4-bit value
; in the range 0 to 0xf. Use this value (in w) to
; obtain seven-segment display code
    call    segment
    movwf    PORTB        ; Display switch bits
    goto    pbutton
;  routine to returns 7-segment
;             codes
        addwf    PCL,f    ; PCL is program counter latch
        retlw    0x3f    ; 0 code
        retlw    0x06    ; 1
        retlw    0x5b    ; 2
        retlw    0x4f    ; 3
        retlw    0x66    ; 4
        retlw    0x6d    ; 5
        retlw    0x7d    ; 6
        retlw    0x07    ; 7
        retlw    0x7f    ; 8
        retlw    0x6f    ; 9
        retlw    0x77    ; A
        retlw    0x7c    ; B
        retlw    0x39    ; C
        retlw    0x5b    ; D
        retlw    0x79    ; E
        retlw    0x71    ; F
        retlw    0x7f    ; Just in case all on

;   piezo buzzer ON
; Routine to turn on piezo buzzer on port B bit 7
        bsf        PORTB,7        ; Tune on bit 7, port B
;   piezo buzzer OFF
; Routine to turn off piezo buzzer on port B bit 7
        bcf        PORTB,7        ; Bit 7 port b clear
;   long delay sub-routine
;    (for code testing)
        movlw    D'200'    ; w = 200 decimal
        movwf    J        ; J = w
jloop:    movwf    K        ; K = w
kloop:    decfsz    K,f        ; K = K-1, skip next if zero
        goto     kloop
        decfsz    J,f        ; J = J-1, skip next if zero
        goto    jloop



It's easy to use this assembly code as the firmware code for the 16F877A micro-controller. Double click on the microcontroller and you can see "Edit Firmware" option. Click on that and copy/paste the above code into the firmware source editor.

PIC16F877A Microcontroller Tutorial using Proteus

Then click on the Build Project icon to compile the assembly code into Hex code(firmware). You should see Complied Successfully output message with minor warning.

PIC16F877A Microcontroller Tutorial using Proteus

Now go back to schematic and click on RUN to run the simulation. You can change the character to be displayed by turning ON/OFF the 4 Dip switch. And you can hear sound when you click on the push button.

PIC16F877A Microcontroller Tutorial using Proteus

If you require proteus, see the proteus software download post. See more microcontroller tutorials.

No Comment to " PIC16F877A Microcontroller Tutorial using Proteus "