You are on page 1of 3

;===============================================================================

;Name:
Kalin Lazarov
;Class:
AME 487
;Date:
06/07/2003
;Exercise:
Homework 4
;Description:
This subroutine debounces a noisy digital input (for example
;
button). The input is sampled every 20 instruction cycles
;MCU:
PIC16F84A
;References:
See Homework_3.pdf for further information
;===============================================================================
;===========================
;
switches
;===========================
processor 16f84a
include <p16f84a.inc>
__CONFIG _HS_OSC &_CP_OFF & _WDT_OFF & _PWRTE_ON
CBLOCK 0x0C
Counter1
Counter2
Press_Count
ENDC
;========================================================
;
m a i n
p r o g r a m
;========================================================
org 0x0000
goto Start
;=============================
;
Register Definitions
;=============================
CBLOCK
Loop_Count:2 ; Counter for the number of samples
Deb_Volt ; Simulated voltage level 0-255
ENDC
;=====================================================
;
variables in PIC RAM
;=====================================================
#define Button PORTA,0
Count_Val equ d'1000'
RC_val
equ d'1'
;=============================
;
main program
;=============================
Start:
bsf PORTB,0
; Make the LED on PORTB.0 "off"
bsf STATUS,RP0 ; Goto Bank 1 to set Port Direction
bcf TRISB,0
; Set RB0 to Output
bcf STATUS,RP0 ; Go back to Bank 0
call Button_Press
Main:
bcf PORTB,0
; LED ON
call Wait
; 1 sec wait
bsf PORTB,0
; LED OFF
call Wait
; 1 sec wait
goto Main
Wait:
; Wait time = 262ms * Counter1
movlw
d'4'
movwf
Counter1
L0 clrf
Counter2
L1 clrw
L2 addlw
0x01
btfss
STATUS,C
goto
L2
incfsz
Counter2,f
goto
L1
decfsz
Counter1,f

goto
return

L0

;**********************************************************************
Button_Press:
movlw
d'5'
movwf
Press_Count
L4 call
Debounce_High
call
Debounce_Low
decfsz
Press_Count,f
goto
L4
return
;**********************************************************************
Debounce_High:
Button
Debounce_High
0xFF
Deb_Volt
Low Count_Val
Loop_Count
High Count_Val
Loop_Count+1

btfss
goto
movlw
movwf
movlw
movwf
movlw
movwf
Loop_1:
call
btfss
goto
movlw
subwf
btfss
subwf
btfsc
goto
return

Adjust_DB_Volt
Deb_Volt,7
Debounce_High

; Test Button for High


; Initialize the Voltage Level
; Initialize the Loop counter
; for the number of samples to be
; monitored

; Check if less than half voltage


; Start from the beginning

0x01
Loop_Count,f
STATUS,C
Loop_Count+1,f
STATUS,C
Loop_1

; Implement 16 bit counter


; The program segment before the
; counter is executed Loop_Count + 1 times

;**********************************************************************
Debounce_Low
btfsc
Button
; Test Button for Low
goto
Debounce_Low
movlw
0x00
; Initialize the Voltage Level
movwf
Deb_Volt
movlw
Low Count_Val
; Initialize the Loop counter
movwf
Loop_Count
; for the number of samples to be
movlw
High Count_Val
; monitored
movwf
Loop_Count+1
Loop_2
call
btfsc
goto
movlw
subwf
btfss
subwf
btfsc
goto
return

Adjust_DB_Volt
Deb_Volt,7
Debounce_Low
0x01
Loop_Count,f
STATUS,C
Loop_Count+1,f
STATUS,C
Loop_2

;
;
;
;
;

Check if more than half voltage


Start from the beginning
Implement 16 bit counter
The program segment before the
counter is executed Loop_Count + 1 times

;**********************************************************************
Adjust_DB_Volt
movlw
limits
btfss
goto

RC_val

; This subroutine adds and subtracts with

Button
DB_Sub

; on 0x00 and 0xFF. The execution time is the


; same for both Low and High cases

DB_Sub

end

addwf
movlw
btfsc
movwf
return
subwf
btfss
clrf
return

Deb_Volt,f
0xFF
STATUS,C
Deb_Volt
Deb_Volt,f
STATUS,C
Deb_Volt

You might also like