📄 qei_sim.asm
字号:
; This file is a basic code template for assembly code generation
; on the PICmicro PIC12F675.
list p=12f675 ; list directive to define processor
#include <p12f675.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.
#define LED GPIO,4
#define RESET_SW GPIO,3
#define OUTA GPIO,1
#define OUTB GPIO,2
#define INDEX GPIO,5
#define RPM_3600 .53
;***** VARIABLE DEFINITIONS
w_temp EQU 0x20 ; variable used for context saving
status_temp EQU 0x21 ; variable used for context saving
CBLOCK 0x30
DelayVar1 : 1
DelayVar2 : 1
Flags : 1
Step_Var : 1
Buf_TMR1L : 1
Buf_TMR1H : 1
TempVar : 1
Base_TMR1L : 1
Base_TMR1H : 1
Index_Location : 1
ENDC
#define DIRECTION 0
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
movf Buf_TMR1L,W ; Update the initialize value of TMR1
movwf TMR1L
movf Buf_TMR1H,W
movwf TMR1H
movf Step_Var,W
btfsc Flags,DIRECTION ; DIRECTION = 0 > FWD
goto REW_OUT ; Reverse Direction
FWD_OUT
addwf PCL,F
bcf OUTA ; Phase 0
bcf OUTB
movf Index_Location,F
btfsc STATUS,Z
bsf INDEX
incf Index_Location,F
movlw 0x08
goto Exit_ISRF
decf Index_Location,W ; ( 0x08 )Phase 1 , clear INDEX pin if Index_Location == 1
btfsc STATUS,Z
bcf INDEX
incf Index_Location,F
bsf OUTA
bcf OUTB
movlw 0x10
goto Exit_ISRF
bsf OUTA ; ( 0x10 )Phase 3
bsf OUTB
incf Index_Location,F
movlw 0x15
goto Exit_ISRF
bcf OUTA ; ( 0x15 )Phase 4
bsf OUTB
incf Index_Location,F
movlw 0x00
Exit_ISRF
movwf Step_Var
bcf PIR1,TMR1IF
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
REW_OUT
addwf PCL,F
bcf OUTA ; Phase 0
bcf OUTB
movf Index_Location,F
btfsc STATUS,Z
bsf INDEX
decf Index_Location,F
movlw 0x08
goto Exit_ISRR
incf Index_Location,W ; (0x08 ) Phase 1 for reverse !!
btfsc STATUS,Z
bcf INDEX
decf Index_Location,F
bcf OUTA
bsf OUTB
movlw 0x010
goto Exit_ISRR
bsf OUTA ; (0x10 ) Phase 3
bsf OUTB
decf Index_Location,F
movlw 0x15
goto Exit_ISRR
bsf OUTA ; ( 0x15 ) Phase 4
bcf OUTB
decf Index_Location
movlw 0x00
; isr code can go here or be located as a call subroutine elsewhere
Exit_ISRR
movwf Step_Var
bcf PIR1,TMR1IF
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
; these first 4 instructions are not required if the internal oscillator is not used
main
call 0x3FF ; retrieve factory calibration value
bsf STATUS,RP0 ; set file register bank to 1
movwf OSCCAL ; update register with factory cal value
bcf STATUS,RP0 ; set file register bank to 0
BANKSEL CMCON
movlw B'00000111' ; Disable Comparator module
movwf CMCON
bcf LED
BANKSEL ANSEL
movlw B'00010001' ; TAD = FOSC/8 , Only AN0 is analog input
movwf ANSEL
bcf TRISIO,4 ; GPIO4 = LED output
bsf TRISIO,3 ; GPIO3 = MCLR
bcf TRISIO,1 ; OUTA
bcf TRISIO,2 ; OUTB
bcf TRISIO,5 ; INDEX
bsf PIE1,TMR1IE
BANKSEL ADCON0
bsf ADCON0,ADON
bcf ADCON0,ADFM
bcf ADCON0,VCFG
bcf INDEX
bcf OUTA
bcf OUTB
clrf Index_Location
movlw ((.65536-.57) % .256)
movwf TMR1L
movwf Buf_TMR1L
movlw ((.65536-.57) / .256)
movwf TMR1H
movwf Buf_TMR1H
movlw B'00000001'
movwf T1CON ; Turn Timer ON & Prescaler = 1:1
; remaining code goes here
BANKSEL GPIO
bsf INTCON,GIE
bsf INTCON,PEIE
clrf Step_Var
Null_Loop
bsf ADCON0,GO
Wait_ADC
nop
btfsc ADCON0,GO
goto Wait_ADC
movf ADRESH,W
btfsc ADRESH,7
goto ADC_GT_0x80
ADC_LT_0x80
btfsc Flags,DIRECTION
goto LT_DIR_DONE
bcf INTCON,GIE
clrf Step_Var
clrf Index_Location
bsf INTCON,GIE
LT_DIR_DONE
bsf Flags,DIRECTION
Get_Base_TMR1LH
movwf Base_TMR1L ; ( Base_TMR1H : Base_TMR1L ) = ADRESH * 2 + RPM_3600
clrf Base_TMR1H
bcf STATUS,C
rlf Base_TMR1L,W
addlw RPM_3600
btfsc STATUS,C
incf Base_TMR1H,F
movwf Base_TMR1L
movlw 0xff ; 0xffff - ( Base_TMR1H : Base_TMR1L )
movwf TempVar
movf Base_TMR1L,W
subwf TempVar,W
movwf Buf_TMR1L
movlw 0xff
movwf TempVar
movf Base_TMR1H,W
subwf TempVar,W
movwf Buf_TMR1H
goto Null_Loop
ADC_GT_0x80 ; W = 0x80 to 0xff
xorlw 0xff ; Complement W register ............................
btfss Flags,DIRECTION
goto GT_DIR_DONE
bcf INTCON,GIE
clrf Step_Var
clrf Index_Location
bsf INTCON,GIE
GT_DIR_DONE
bcf Flags,DIRECTION
goto Get_Base_TMR1LH
Delay
movf ADRESH,W
movwf DelayVar1
btfsc STATUS,Z
incf DelayVar1,F
D_Loop1
clrf DelayVar2
D_Loop2
nop
decfsz DelayVar2,F
goto D_Loop2
decfsz DelayVar1,F
goto D_Loop1
return
; initialize eeprom locations
ORG 0x2100
DE 0x00, 0x01, 0x02, 0x03
END ; directive 'end of program'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -