Lister - HW4

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

description

A great assembly code for PIC MCUs. Should be sufficient enough for beginners to understand.

Transcript of Lister - HW4

Page 1: Lister - HW4

;===============================================================================;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 & _PWR TE_ON

CBLOCK 0x0CCounter1Counter2Press_Count ENDC;========================================================; m a i n p r o g r a m;======================================================== org 0x0000 goto Start

;=============================; Register Definitions;============================= CBLOCKLoop_Count:2 ; Counter for the number of samplesDeb_Volt ; Simulated voltage level 0-255 ENDC;=====================================================; variables in PIC RAM;=====================================================#define Button PORTA,0Count_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_PressMain: bcf PORTB,0 ; LED ON call Wait ; 1 sec wait bsf PORTB,0 ; LED OFF call Wait ; 1 sec wait goto MainWait: ; Wait time = 262ms * Counter1 movlw d'4' movwf Counter1L0 clrf Counter2L1 clrwL2 addlw 0x01 btfss STATUS,C goto L2 incfsz Counter2,f goto L1 decfsz Counter1,f

Page 2: Lister - HW4

goto L0 return

;**********************************************************************Button_Press: movlw d'5' movwf Press_CountL4 call Debounce_High call Debounce_Low decfsz Press_Count,f goto L4 return

;**********************************************************************Debounce_High:

btfss Button ; Test Button for High goto Debounce_High movlw 0xFF ; 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_1: call Adjust_DB_Volt btfss Deb_Volt,7 ; Check if less than half voltage goto Debounce_High ; Start from the beginning

movlw 0x01 ; Implement 16 bit counter subwf Loop_Count,f ; The program segment before the btfss STATUS,C ; counter is executed Loop_Count + 1 times subwf Loop_Count+1,f btfsc STATUS,C goto Loop_1 return

;**********************************************************************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 Adjust_DB_Volt btfsc Deb_Volt,7 ; Check if more than half voltage goto Debounce_Low ; Start from the beginning movlw 0x01 ; Implement 16 bit counter subwf Loop_Count,f ; The program segment before the btfss STATUS,C ; counter is executed Loop_Count + 1 times subwf Loop_Count+1,f btfsc STATUS,C goto Loop_2 return

;**********************************************************************

Adjust_DB_Volt movlw RC_val ; This subroutine adds and subtracts with limits btfss Button ; on 0x00 and 0xFF. The execution time is the goto DB_Sub ; same for both Low and High cases

Page 3: Lister - HW4

addwf Deb_Volt,f movlw 0xFF btfsc STATUS,C movwf Deb_Volt returnDB_Sub subwf Deb_Volt,f btfss STATUS,C clrf Deb_Volt return

end