Lister - HW6

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

description

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

Transcript of Lister - HW6

Page 1: Lister - HW6

;===============================================================================;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 16f84Ainclude <p16f84A.inc>__config _XT_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF

;=====================================================; variables in PIC RAM;=====================================================counter equ 0x0ccounterDirection equ 0x0dsleepOn equ 0x0ej equ 0x0fk 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 0x0020main:;===================; 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 binarytris PORTB ; Set up port B for input

goto main

Page 2: Lister - HW6

;===============================; Other Labels Used in Program;===============================slp: SLEEP ;command to tell 84A to sleep btfsc sleepOn,0 ;if sleepOn,0 is set, return goto slp

count: btfsc counterDirection,0 ;if counterDirection,0 is 0... incf counter ;... increment counter btfss counterDirection,0 ;if counterDirection,0 is 1... decfsz counter ;... decrement counter return

halveTheCounter: movlw .17 ;this halves the counter from .34 to .17 movwf counter return

buttonRB4Pressed: bcf sleepOn,0 ;pressing button on RB4 turns off sleep movf .34,w ;next few lines test whether the counter is <34 subwf counter,w btfsc STATUS,Z ;significance of 34 --> ((2040/3))/20 = 34 call halveTheCounter ;if so, halve the counter btfsc STATUS,Z ;if the zero register is 0, don't count return

call count ;otherwise do count return

buttonRB5Pressed: movlw B'00000000' ;reset counter to B'00000000' movwf counter return

buttonRB6Pressed: ;this sets sleepOn to 1 and calls slp bsf sleepOn,0 call slp

buttonRB7Pressed: btfss counterDirection,0 ;if counterDirection,0 is 0... bsf counterDirection,0 ;...set it to 1

btfsc counterDirection,0 ;if counterDirection,0 is 1... bcf counterDirection,0 ;...set it to 1 return

;================================; delay sub-routine;================================delay:

movlw .200 ; w = 200 decimalmovwf j ; j = w

jloop:movwf k ; k = w

kloop:decfsz k,f ; k = k-1, skip next if zerogoto kloopdecfsz j,f ; j = j-1, skip next if zerogoto jloopreturn

;================================; interrupt sub-routine

Page 3: Lister - HW6

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

btfsc PORTB,5 ;if PORTB,5 is set... call buttonRB5Pressed ;call buttonRB4Pressed

btfsc PORTB,6 ;if PORTB,6 is set... call buttonRB6Pressed ;call buttonRB4Pressed

btfsc sleepOn,0 ;turn off SLEEP if sleepOn,0 is clear call slp

btfsc PORTB,7 ;if PORTB,7 is set... call buttonRB7Pressed ;call buttonRB4Pressed

call delay

retfie

end