Blink an LED: your first PIC program

Write, assemble and run a PIC16F84A program that blinks an LED on RA0 every half second, and learn banks, TRIS registers and delay loops on the way.

  • PIC16F84A
  • Beginner
  • Watch the RA0 pin in the GPIO Simulation widget

Open the example in the simulator

What the program does

The LED on RA0 turns on for half a second, then off for half a second, forever. That needs three things: make RA0 an output, switch it on and off, and wait in between.

The header

list p=16f84a names the chip, include <p16f84a.inc> brings in register names such as PORTA and STATUS, and __CONFIG sets the configuration word: HS oscillator, watchdog off, power-up timer on, code protection off.

cblock 0x20 gives names to RAM locations starting at 0x20, the first general-purpose register on the PIC16F84A. The delay loop uses three of them as counters.

Reset and interrupt vectors

After reset the PIC starts at address 0x0000, and an interrupt jumps to 0x0004. org 0x00 followed by goto start jumps over the interrupt vector, which here is just a RETFIE because interrupts are never enabled.

Making RA0 an output

Each port has a direction register: a 1 in TRISA makes the pin an input, a 0 an output. After reset every pin is an input.

TRISA lives in bank 1, so the program sets RP0 in STATUS with BSF, clears bit 0 of TRISA with BCF, then clears RP0 again to get back to bank 0, where PORTA is.

The main loop

bsf PORTA, 0 drives RA0 high and bcf PORTA, 0 drives it low, each followed by call delay_500ms. goto main_loop repeats it forever; a PIC program never just ends.

How long is the delay?

At 4 MHz the PIC runs one instruction cycle per microsecond (four clock periods per instruction). The innermost loop is DECFSZ plus GOTO: 3 cycles a pass. Three nested counters of 4, 249 and 166 give 4 × 249 × 166 passes of about 3 µs, roughly 500,000 cycles or 0.5 s.

Notice that each loop reloads the counter below it on every pass. Forgetting that is a classic bug: the inner counter is left at 0, so every later pass counts 256 times instead.

The delay calculator works these numbers out for other times and clock speeds.

Try it

  1. Open the example, press Assemble (Ctrl+B), then Run (F5). RA0 in the GPIO widget blinks.
  2. Press Reset (F8) and use Step (F7) to walk through the setup one instruction at a time. Watch STATUS change as the bank switches.
  3. Use Step Over (F10) on a call delay_500ms line to run the whole delay in one go.
  4. Change movlw 4 in the delay to movlw 2 and assemble again: the LED blinks twice as fast.

The complete program

This is examples/blink.asm, the same file the simulator loads.

; Blink LED Example
; Simple LED blinking program for PIC16F84A
; LED connected to RA0: on for 0.5 s, off for 0.5 s (4 MHz crystal)

    list p=16f84a
    include <p16f84a.inc>
    
    ; Configuration bits
    __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _CP_OFF
    
    ; Variables
    cblock 0x20
        delay1
        delay2
        delay3
    endc
    
    org 0x00
    goto start
    
    org 0x04
    ; Interrupt vector (not used in this example)
    retfie
    
start:
    ; Set RA0 as output
    bsf STATUS, RP0      ; Select Bank 1
    bcf TRISA, 0         ; Set RA0 as output
    bcf STATUS, RP0      ; Select Bank 0
    
    ; Main loop
main_loop:
    bsf PORTA, 0         ; Turn LED on
    call delay_500ms     ; Wait 500ms
    bcf PORTA, 0         ; Turn LED off
    call delay_500ms     ; Wait 500ms
    goto main_loop       ; Repeat
    
delay_500ms:
    ; At 4 MHz one instruction cycle is 1 us. The inner loop takes 3 cycles a
    ; pass, so 4 x 249 x 166 passes is about 500,000 cycles = 0.5 s.
    movlw 4
    movwf delay3
delay_outer:
    movlw 249
    movwf delay2
delay_middle:
    movlw 166
    movwf delay1
delay_loop:
    decfsz delay1, f
    goto delay_loop
    decfsz delay2, f
    goto delay_middle
    decfsz delay3, f
    goto delay_outer
    return
    
    end

Open it in the simulator

Next