You are on page 1of 3

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

;Name:
Julio Sanchez with changes by Peter Seid
;Class:
AME 487
;Date:
03/30/2015
;Exercise:
Homework 6
;Description:
Implement code mathematics operations with PIC, interrupts
;MCU:
PIC16F84A
;References:
See Homework_6.pdf for further information
;===============================================================================
; 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 occilator
; * _XT_OSC
External parallel resonator/crystal ocillator
;
_HS_OSC
High speed crystal resonator (8 to 10 MHz)
;
Resonator: Murate Erie CSA8.00MG = 8 MHz
;
_RC_OSC
Resistor/capacitor ocillator (simplest, 20% error)
; |
; |_____ * indicates setup values
processor 16f84A
include
<p16f84A.inc>
__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
;=====================================================
;
variables in PIC RAM
;=====================================================
counter
equ
0x0c
counterDirection
equ
0x0d
sleepOn
equ
0x0e
j
equ
0x0f
k
equ
0x10
;========================================================
;
m a i n
p r o g r a m
;========================================================
org 0x0000
; start at address 0
goto
main
;=============================
; space for interrupt handler
;=============================
org
0x0004
goto
int_routine
;=============================
;
main program
;=============================
org 0x0020
main:
;===================
; Interrupt Setup
;===================
;input
bsf INTCON, GIE
;enable interrupts
bsf INTCON, RBIE
;enable RB4-7 interrupts
bcf INTCON, RBIF
;clearing the interrupt flag on RB4-7
;===================
; Initialize lines 4/5/6/7 in port B for input
; --> RB4 is 'increase counter by 20' button (interrupt) and 'wake from sleep'
; --> RB5 is 'reset counter to 0' button (interrupt)
; --> RB6 is 'sleep' button
; --> RB7 is 'flip counter direction' button
movlw
B'11110000'
; w = 00110001 binary
tris
PORTB
; Set up port B for input
goto main

;===============================
; Other Labels Used in Program
;===============================
slp:
SLEEP
btfsc
sleepOn,0
return
goto
slp

;command to tell 84A to sleep


;if sleepOn,0 is set,

count:
btfsc
incf
btfss
decfsz
return

counterDirection,0
counter
counterDirection,0
counter

halveTheCounter:
movlw
.17
movwf
counter
return
buttonRB4Pressed:
bcf
sleepOn,0
movf
.34,w
subwf
counter,w
btfsc
STATUS,Z
call
halveTheCounter
btfsc
STATUS,Z
return
call
return

count

buttonRB5Pressed:
movlw
B'00000000'
movwf
counter
return
buttonRB6Pressed:
bsf
sleepOn,0
call
slp

;this halves the counter from .34 to .17

;pressing button on RB4 turns off sleep


;next few lines test whether the counter is <34
;significance of 34 --> ((2040/3))/20 = 34
;if so, halve the counter
;if the zero register is 0, don't count
;otherwise do count

;reset counter to B'00000000'

;this sets sleepOn to 1 and calls slp

buttonRB7Pressed:
btfss
counterDirection,0
bsf
counterDirection,0
btfsc
bcf
return

;if counterDirection,0 is 0...


;... increment counter
;if counterDirection,0 is 1...
;... decrement counter

counterDirection,0
counterDirection,0

;if counterDirection,0 is 0...


;...set it to 1
;if counterDirection,0 is 1...
;...set it to 1

;================================
;
delay sub-routine
;================================
delay:
movlw
.200
; w = 200 decimal
movwf
j
; j = w
jloop:
movwf
k
; k = w
kloop:
decfsz k,f
; k = k-1, skip next if zero
goto
kloop
decfsz j,f
; j = j-1, skip next if zero
goto
jloop
return
;================================
;
interrupt sub-routine

;================================
int_routine:
btfsc
PORTB,4
;if PORTB,4 is set...
call
buttonRB4Pressed
;call buttonRB4Pressed
btfsc
call

PORTB,5
buttonRB5Pressed

;if PORTB,5 is set...


;call buttonRB4Pressed

btfsc
call

PORTB,6
buttonRB6Pressed

;if PORTB,6 is set...


;call buttonRB4Pressed

btfsc
call

sleepOn,0
slp

;turn off SLEEP if sleepOn,0 is clear

btfsc
call

PORTB,7
buttonRB7Pressed

;if PORTB,7 is set...


;call buttonRB4Pressed

call

delay

retfie
end

You might also like