; Andy Meng errorlevel -302 ; turn off annoying bank bits notification __CONFIG _BODEN_OFF & _CP_OFF & _HS_OSC & _PWRTE_OFF & _WDT_OFF & _LVP_OFF include "p16f872.inc" org 0 goto Init ; pin equates Servo1Pin equ 2 Servo2Pin equ 3 ; memory locations Servo1Pos equ 0x20 Servo2Pos equ 0x21 tmrflags equ 0x22 resulthi equ 0x23 resultlo equ 0x24 org 04 ISR: bcf INTCON,GIE ; disable global interrupts bcf PIR1,TMR1IF clrf T1CON ; turn off timer clrf TMR1H ; clear timer high byte clrf TMR1L ; clear timer low byte btfss tmrflags,0 goto delay17ms btfss tmrflags,1 goto Servo1 goto Servo2 delay17ms: bcf PORTC,Servo2Pin movlw 0x9B ; put 0xBD in w (9B97 = 17ms delay) movwf TMR1H ; put it in the high timer byte movlw 0x97 ; put 0x97 in w movwf TMR1L ; put it in the low timer byte movlw 0x29 ; turn on the timer again movwf T1CON bsf tmrflags,0 ; let it know to do Servo1 next time incf Servo2Pos,f decf Servo1Pos,f retfie ; return from interrupt Servo1: ; multiply and set TMR1 up for Servo1Delay bsf PORTC,Servo1Pin movlw 0x06 ; multiplicand movwf resultlo ; put it in resultlo to multiply movf Servo1Pos,w ; put Serv1Pos in w to multiply call Multiply ; multiply them movlw 0xBF ; add 191 decimal to total addwf resultlo,f ; add 191 to resultlo btfsc STATUS,C ; check status bit incf resulthi,f ; if resulthi < FF, inc resulthi movlw 0xFF movwf TMR1H movf resulthi,w subwf TMR1H,f movlw 0xFF movwf TMR1L movf resultlo,w subwf TMR1L,f movlw 0x39 ; set up and turn on timer again 8:1PS movwf T1CON movlw 0x03 ; set tmrflags to go to servo2 movwf tmrflags retfie ; return from interrupt Servo2: ; same as above except for Servo 2 bcf PORTC,Servo1Pin ; end Servo1 pulse bsf PORTC,Servo2Pin ; start Servo2 pulse movlw 0x06 movwf resultlo movf Servo2Pos,w call Multiply movlw 0xBF ; add 191 decimal to total addwf resultlo,f ; add 191 to resultlo btfsc STATUS,C ; check status bit incf resulthi,f ; if resulthi > FF, inc resulthi movlw 0xFF movwf TMR1H movf resulthi,w subwf TMR1H,f ; sub resulthi from FF (since it is count up instead of dn) movlw 0xFF movwf TMR1L movf resultlo,w subwf TMR1L,f movlw 0x39 movwf T1CON clrf tmrflags ; let it know to do 17ms delay next retfie ; end of ISR org 4A Init: movlw 0xC0 ; ints on, peripheral ints on movwf INTCON movlw 0x29 ; turn on timer, prescaler, internal clock movwf T1CON bsf STATUS,RP0 clrf PIE1 bsf PIE1,TMR1IE movlw B'00001111' movwf TRISB clrf TRISC bcf STATUS,RP0 clrf PORTB clrf PORTC clrf tmrflags clrf resulthi movlw 0xFF ; 0x00 is actually 0xFF (decfsz substracts one) movwf Servo1Pos movlw 0x01 movwf Servo2Pos wait: ; the rest of the program code would go here goto wait Multiply: ; from piclist.com, multiplies 8bit by 8bit mult MACRO btfsc STATUS,C addwf resulthi,F rrf resulthi,F rrf resultlo,F ENDM clrf resulthi ; * 1 cycle rrf resultlo,F ; * 1 cycle mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles mult ;* 4 cycles return end