PIC Projects

Flash a LED

This is traditionally one of the first projects in EE, an analog of the first "Hello World" program in study of any programming language. It is a good test of your development environment including PIC programmer, circuit board, and power supply.

The device flashes a LED in approximately 1 sec intervals. The schematic is very simple. Besides of default elements which are common to every design involving PIC16F84A it has just two additional elements: the LED and a current limiting resistor of 220 Ohm. The circuit is assembler on a solderless board, the layout is straightforward. I used one of the segments of a 10-bar LED to flash.

Schematic Layout

As it follows from the schematic, the LED is connected to the RB1 port of 16F84A. If this port outputs a logical 1 (approx. +5v) the LED is on. Otherwise, it is off. Hence, to flash the LED one has to periodically output a 1 on this port. This is achieved by delay() function in the program. This function is just a series of 3 nested loops. Each iteration of the most inner loop takes 3 cycles. Running the PIC at 4Mhz provides completion of each instruction that does not modify PC (program counter) in 1 μs. The instructions that modify the PC (all conditional jumps, if the jump is taken, subroutine calls and returns) take 2 cycles, i.e. 3 μs.

 TITLE  "led1.asm" 			; Turning on a LED
 LIST P=16F84a, R=DEC
 INCLUDE "p16F84a.inc"

; data segment 
 CBLOCK 0x00C                   
 i,j,k                          	; these variables are used in delay()
 ENDC

 __CONFIG _CP_OFF & _PWRTE_ON  & _WDT_OFF & _XT_OSC

 ORG 0
  	bsf    	STATUS, RP0		; change to BANK 1
  	bcf    	TRISB ^ 0x080, 1       	; enable RB1 for output
  	bcf    	STATUS, RP0		; back to BANK 0

loop
	bsf	PORTB, 1		; RB1 = 1, thus LED on 
	call 	delay

	bcf	PORTB, 1		; RB1 = 0, thus LED off
	call 	delay
	goto	loop			; repeat forever

delay					; this is a dalay for approx. 2s
	movlw	5			; for 16F84A @ 4Mhz
	movwf	k
	
L1	movlw	200			; outer loop
	movwf	i

L2 	movlw	200			; inner loop
	movwf 	j

L3 	decfsz	j, f			; inner inner loop
	goto 	L3

	decfsz	i, f		
	goto 	L2

    	decfsz 	k, f	
	goto 	L1

	return
 END

Download led1.asm


Last modified:Mon, Jan 23, 2023.

09436