Select Page

3 LED binary counter with start button code in assembly for PIC18F452

;******************************************************************************
; Introduction to Assembly Programming
; Title: Port control with counting
; Author: Tim Norton
; Date: 6/3/2020
; 


;******************************************************************************
; NOTES
;******************************************************************************
; Will count to 7 in binary on the LED's connected to RC0..2
; If the pushbutton is not pressed, all LED's will turn off until
; the button is pressed

;******************************************************************************
; INCLUDES AND ASSEMBLER DIRECTIVES
;******************************************************************************
#include <p18f452.inc>
config OSC = HS
config BOR = OFF, WDT = OFF, LVP = OFF
org 0x0000 ; reset vector
goto start

;******************************************************************************
; VARIABLES
;******************************************************************************
cblock ; where varialbls are defined in memory
    count1 ;
    count2
    count3
  endc
;******************************************************************************
; INCLUDES
;******************************************************************************

;******************************************************************************
; START PROGRAM
;******************************************************************************
start:
    call init
    bra loop

;******************************************************************************
; INITIALISATION
;******************************************************************************
; initialise setting    
init: 
    movlw   b'11111000'	 
    movwf   TRISC  ; put port C [2:0] outputs [7:3] inputs
    return
;******************************************************************************
; MAIN PROGRAM LOOP
;******************************************************************************
loop:

    movlw   b'00000001' ; turn on LED 1 only
    movwf   PORTC
    call    delay500	;500mS delay
    incf    PORTC  ; Increment PORT C by 1 so = 2 
    call    delay500	;500mS delay
    incf    PORTC  ; Increment PORT C by 1 so = 3
    call    delay500
    incf    PORTC  ; Increment PORT C by 1 so = 4
    call    delay500
    incf    PORTC  ; Increment PORT C by 1 so = 5
    call    delay500
    incf    PORTC  ; Increment PORT C by 1 so = 6
    call    delay500
    incf    PORTC  ; Increment PORT C by 1 so = 7
    call    delay500
buttonTest:  ; stop if button not pressed  
    btfss   PORTC, 3 ; if button pressed will =0
    bra	    loop 
    movlw   b'00000000' ; turn off all LEDs
    movwf   PORTC
    bra	    buttonTest
;******************************************************************************
; FUNCTIONS
;******************************************************************************
; Delay functions
delay500:    ;500mS delay
    movlw   d'10'
    movwf   count3
    bra	    delay
delay100:    ;100mS delay
    movlw   d'1'
    movwf   count3
    bra	    delay
delay: 
    movlw   d'199'
    movwf   count1
delay_loop_outer:
    clrf    count2
delay_loop_inner:
    incf    count2
    bnz	    delay_loop_inner
    incf    count1
    bnz	    delay_loop_outer
    decf    count3
    bnz	    delay_loop_outer
    return
   

;******************************************************************************
; END
;******************************************************************************
end