Lister - HW3

3
;=============================================================================== ;Class: AME 487 ;Date: 05/06/2015 ;Exercise: Homework 3 ;Description: Timers and Counter with multiple push buttons ;MCU: PIC16F84A ;References: See Homework_3.pdf for further information ;=============================================================================== ;=========================== ; switches ;=========================== ; Switches used in __config directive: ; _CP_ON Code protection ON/OFF ; * _CP_OFF ; * _PWRTE_ON Power-up timer ON/OFF ; _PWRTE_OFF ; _WDT_ON Watchdog timer ON/OFF ; * _WDT_OFF ; _LP_OSC Low power crystal oscillator ; * _XT_OSC External parallel resonator/crystal oscillator ; _HS_OSC High speed crystal resonator (8 to 10 MHz) ; Resonator: Murate Erie CSA8.00MG = 8 MHz ; _RC_OSC Resistor/capacitor oscillator (simplest, 20% error) ; | ; |_____ * indicates setup values processor 16f84a include <p16f84a.inc> __CONFIG _HS_OSC &_CP_OFF & _WDT_OFF & _PWRTE_ON ;===================================================== ; variables in PIC RAM ;===================================================== ; Declare variables at 3 memory locations used for delay loop Counter1 equ 0x0C Counter2 equ 0x0D Button_Count equ 0x0E ;======================================================== ; m a i n p r o g r a m ;======================================================== org 0 ; start at address 0 goto main ;============================= ; space for interrupt handler ;============================= org 0x04 ;============================= ; main program ;============================= main: bsf PORTB,0 ; Make the LED on PORTB.0 "off" bsf STATUS,RP0 ; Goto Bank 1 to set Port Direction movlw B'00011110' ; w = 00000111 binary tris PORTA ; Set up PortA 1/2/3/4 for input bcf TRISB,0 ; Set RB0 to Output bcf STATUS,RP0 ; Go back to Bank 0 buttonCheckLoop: btfsc PORTA,1 ; Check if button1 pressed & if so, don't skip next call Button_Press1 btfsc PORTA,2 ; Check if button2 pressed & if so, don't skip next call Button_Press2 btfsc PORTA,3 ; Check if button3 pressed & if so, don't skip next call Button_Press3 btfsc PORTA,4 ; Check if button4 pressed & if so, don't skip next call Button_Press4

description

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

Transcript of Lister - HW3

Page 1: Lister - HW3

;===============================================================================;Class: AME 487;Date: 05/06/2015;Exercise: Homework 3;Description: Timers and Counter with multiple push buttons;MCU: PIC16F84A;References: See Homework_3.pdf for further information;===============================================================================

;===========================; switches;===========================; Switches used in __config directive:; _CP_ON Code protection ON/OFF; * _CP_OFF; * _PWRTE_ON Power-up timer ON/OFF; _PWRTE_OFF; _WDT_ON Watchdog timer ON/OFF; * _WDT_OFF; _LP_OSC Low power crystal oscillator; * _XT_OSC External parallel resonator/crystal oscillator; _HS_OSC High speed crystal resonator (8 to 10 MHz); Resonator: Murate Erie CSA8.00MG = 8 MHz; _RC_OSC Resistor/capacitor oscillator (simplest, 20% error); |; |_____ * indicates setup values

processor 16f84a include <p16f84a.inc> __CONFIG _HS_OSC &_CP_OFF & _WDT_OFF & _PWRTE_ON

;=====================================================; variables in PIC RAM;=====================================================; Declare variables at 3 memory locations used for delay loopCounter1 equ 0x0CCounter2 equ 0x0DButton_Count equ 0x0E

;========================================================; m a i n p r o g r a m;========================================================

org 0 ; start at address 0goto main

;=============================; space for interrupt handler;=============================

org 0x04;=============================; main program;=============================main: bsf PORTB,0 ; Make the LED on PORTB.0 "off" bsf STATUS,RP0 ; Goto Bank 1 to set Port Direction

movlw B'00011110' ; w = 00000111 binarytris PORTA ; Set up PortA 1/2/3/4 for input

bcf TRISB,0 ; Set RB0 to Output bcf STATUS,RP0 ; Go back to Bank 0

buttonCheckLoop: btfsc PORTA,1 ; Check if button1 pressed & if so, don't skip next call Button_Press1 btfsc PORTA,2 ; Check if button2 pressed & if so, don't skip next call Button_Press2 btfsc PORTA,3 ; Check if button3 pressed & if so, don't skip next call Button_Press3 btfsc PORTA,4 ; Check if button4 pressed & if so, don't skip next call Button_Press4

Page 2: Lister - HW3

goto buttonCheckLoop

Loop: bcf PORTB,0 ; LED ON movlw d'50' ; 1 second wait (50x20ms) movwf Counter2 ; Move decimal 50 to Counter2

L2: call Wait decfsz Counter2,f ; Decrement Counter2, if it's zero skip next line goto L2 bsf PORTB,0 ; LED OFF movlw d'50' ; 1 second wait (50x20ms) movwf Counter2 ; Move decimal 50 to Counter2

L3: call Wait decfsz Counter2,f ; Decrement Counter2, if it's zero skip next line goto L3 goto Loop

Wait: movlw 0x10 ; Move hex 0x10 to variable Counter1 movwf Counter1

L0: clrw ; Clear the work register bcf STATUS,C ; Clear carry bit in the STATUS register

L1: addlw 0x01 ; Adds hex 0x10 to value in work register nop ; Nothing happens in this line btfss STATUS,C ; Test if STATUS carry bit is set. If so, skip next goto L1 decfsz Counter1,f ; Decrement Counter1, if it's zero skip next line goto L0 return

Button_Press1: movlw d'5' ; Move decimal d 5 to work register movwf Button_Count ; Move work register to Button_Count variableLoop_B1: btfss PORTA,1 ; Detect falling edge skip next line if PORTA,1 set goto $-1 ; Jump to previous line btfsc PORTA,1 ; Skip next line if PORTA,1 clear goto $-1 ; Jump to previous line decfsz Button_Count ; Decrement Button_Count, skip next line if is zero goto Loop_B1 return

Button_Press2: movlw d'7' ; Move decimal d 7 to work register movwf Button_Count ; Move work register to Button_Count variableLoop_B2: btfss PORTA,2 ; Detect falling edge skip next line if PORTA,2 set goto $-1 ; Jump to previous line btfsc PORTA,2 ; Skip next line if PORTA,2 clear goto $-1 ; Jump to previous line decfsz Button_Count ; Decrement Button_Count, skip next line if is zero goto Loop_B2 return

Button_Press3: movlw d'9' ; Move decimal d 9 to work register movwf Button_Count ; Move work register to Button_Count variableLoop_B3: btfss PORTA,3 ; Detect falling edge skip next line if PORTA,3 set

Page 3: Lister - HW3

goto $-1 ; Jump to previous line btfsc PORTA,3 ; Skip next line if PORTA,3 clear goto $-1 ; Jump to previous line decfsz Button_Count ; Decrement Button_Count, skip next line if is zero goto Loop_B3 return

Button_Press4: movlw d'11' ; Move decimal d 11 to work register movwf Button_Count ; Move work register to Button_Count variableLoop_B4: btfss PORTA,4 ; Detect falling edge skip next line if PORTA,3 set goto $-1 ; Jump to previous line btfsc PORTA,4 ; Skip next line if PORTA,3 clear goto $-1 ; Jump to previous line decfsz Button_Count ; Decrement Button_Count, skip next line if is zero goto Loop_B4 return

end