📄 pic.asm
字号:
;------------------------------------------------------------------------------
; SVA-IC
;------------------------------------------------------------------------------
; CPU: PIC12F629
; MPLAB
; BOARD: FOR SD3002
;------------------------------------------------------------------------------
list p=12F629 ; list directive to define processor
INCLUDE <P12F629.INC>
; code protect off (_CP_OFF)
; data protect off (_CPD_OFF)
; 欠压检测disable (_BODEN_OFF)
; GP3/MCLRE 为I/O脚 (_MCLRE_OFF)
; 上电延迟定时器禁止 (_PWRTE_ON)
; 看门狗定时器禁止 (_WDT_OFF)
; 内部OSC震荡器,GP4,GP5 均为I/O功能 (_INTRC_OSC_NOCLKOUT)
__CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT
;*********************** VARIABLE DEFINITIONS **************************
; using Shared Uninitialized Data Section
INT_VAR UDATA_SHR 0x20
w_temp RES 1 ; variable used for context saving
status_temp RES 1 ; variable used for context saving
or_temp RES 1 ; option_reg temp
d_num RES 1 ; delay num 0
d_num1 RES 1 ; delay num 1
d_num2 RES 1 ; delay num 2
bit_flag RES 1 ; bit flag
cir_num RES 1 ; circle num
ir_num RES 1 ; ir circle num
ir_data RES 1 ; IR data
tmp_data1 RES 1 ; IR data
ir_circle RES 1 ; IR circle
;----- bit_flag Bits --------------------------------------------------------
TIMF EQU H'00'
PWON EQU H'01'
GP2PWON EQU H'02' ; INFO MAIN ROUTINE POWERON
;***************** MACRO DEFINITIONS*********************
;立即数送寄存器的宏
movlf MACRO FX,VAL
movlw VAL
movwf FX
ENDM
;寄存器送寄存器的宏
movff MACRO FX2,FX1 ;FX1-->FX2 注意方向
movf FX1,W
movwf FX2
ENDM
comp MACRO FX10,FX11,LARGER,EQUAL,SMALLER ; FX10是F,FX11是立即数
movlw FX11 ;先将FX11→W
subwf FX10,W ;FX10-W→W
btfsc STATUS,Z ;Z=0,不相等,跳
goto EQUAL ;FX10=FX11,跳到分支EQUAL
btfsc STATUS,C ;C=0,跳
goto LARGER ;FX10>FX11,跳到分支LARGER
goto SMALLER ;FX10<F11
ENDM
;-----------------------------------------------------------------------------
; entery
;*****************************************************************************
RESET_VECTOR CODE 0x000 ;ORG 0x000 ; processor reset vector
goto START ; go to beginning of program
;-----------------------------------------------------------------------------
; IR INT
;*****************************************************************************
INT_VECTOR CODE 0x004 ; ORG 0x004 ; interrupt vector location
goto IR_INT
;---------------------- INT END ---------------------------------------------
;----------------------------------------------------------------------------
; MAIN begin
;****************************************************************************
START
;initial internal oscillator
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
; MY CODE BEGIN
bcf STATUS,RP0 ;Bank 0
clrf GPIO ;Init GPIO
movlf CMCON, 0x07 ;Set GP<2:0> to digital IO
bsf STATUS, RP0 ;Bank 1
movlf TRISIO, 0x2c ;Set GP<3:2, 5> as inputs and set GP<4,1:0> as outputs
bcf STATUS, RP0
call DELAY150MS
bcf GPIO, GP0
bcf GPIO, GP4
bsf GPIO, GP1
clrf bit_flag
;*********************** debug *********************
;btfss GPIO, GP2
;goto $-1
;call IR_HEAD
;TIMER1 INIT
;bsf STATUS,RP0
; MOVF OPTION_REG,W
; MOVLW b'xx0x0110'
; MOVWF OPTION_REG
;movff or_temp, OPTION_REG
;movf or_temp&0xD0 ;T0CS(BIT5) = 0, PSA(BIT3) = 0, PRESCALE = 128
;movf or_temp|0x06
;movff OPTION_REG,or_temp
;bcf STATUS,RP0
;movlf TMR0, H'
;TIMER1 INIT
;movlf T1CON, 0x30
;movlf TMR1L, 0xc2
;movlf TMR1H, 0xb6
;bsf T1CON, TMR1ON ; start timer1
; 启动外部中断 GP2
;bsf STATUS, RP0
;bcf OPTION_REG, INTEDG ; down eage
;bcf STATUS, RP0
;bcf INTCON, INTF ; clear pend bit
;bsf INTCON, INTE ; enable GP2/int interrupt
;bsf INTCON, GIE ; enable global interrupt
MAIN
btfss GPIO, GP5
call POWERON ; GP5 = 0
;btfsc bit_flag, PWON
;goto SLEEPON ; PWON = 1
btfsc bit_flag, GP2PWON
call IR_POWERON ; GP2WON = 1
btfss GPIO, GP2
call IR_HEAD
goto MAIN
;SLEEPON
; btfss GPIO, GP3
; goto SLEEPON1 ; GP3 = 0
; goto MAIN
;SLEEPON1
; call DELAY150MS
; btfsc GPIO, GP3
; goto MAIN ; GP3 = 1
; bcf bit_flag, PWON ; clear PWON
; bcf GPIO, GP4 ; GP4 = 0
; bcf GPIO, GP0 ; GP0 = 0
; goto MAIN
;---------------------- MAIN END --------------------------------------------
IR_INT
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
; isr code can go here or be located as a call subroutine elsewhere
bsf GPIO, GP4 ; DEBUG
;call IR_HEAD
;sublw 0x01
;btfsc STATUS, Z
;goto INTEND
;call IRD_GET
bcf INTCON, INTF ; clear pend bit
INTEND
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
;----------------------------------------------------------------------------
; Routine
;----------------------------------------------------------------------------
; press the poweron key by ir key
IR_POWERON
bcf bit_flag, GP2PWON ; clear GP2WON
;bsf bit_flag, PWON ; PWON = 1
bsf GPIO, GP0 ; GP0 = 1
call DELAY3S
bsf GPIO, GP4 ; GP4 = 1
return
; press GP5 key
POWERON
call DELAY150MS
btfsc GPIO, GP5
goto POWERON_END ; GP5 = 1
ONPOWER
bsf bit_flag, PWON ; PWON = 1
bsf GPIO, GP0 ; GP0 = 1
call DELAY3S
bsf GPIO, GP4 ; GP4 = 1
POWERON_END
return
; delay 3 s
DELAY3S
movlf d_num2, .20 ; 20*(150000+7)
LOOP3S
call DELAY150MS ; 2 tcy
decfsz d_num2 ; 1 tcy
goto LOOP3S ; 2 tcy
return
; delay 150 ms
DELAY150MS
movlf d_num1, .149 ; 149600us
LOOPD1
movlf d_num, 0xfa ; 2 tcy 250 -> d_num
LOOPD2
nop ; 1 tcy
decfsz d_num ; 1 tcy
goto LOOPD2 ; 2 tcy
decfsz d_num1 ; 1 tcy
goto LOOPD1 ; 2 tcy
return
;STTIME1
; bcf T1CON, TMR1ON ; close timer1
; movlf TMR1L, 0xc2 ; initial
; movlf TMR1H, 0xb6
; bcf PIR1, T1IF ; clear int pend bit
; bsf INTCON, GIE ; global int enable
; bsf INTCON, PEIE ; periphery int enable
; bsf STATUS,RP0 ; bank1
; bsf PIE1, TMR1IE ; timer1 int enable
; bcf STATUS,RP0 ; bank0
; bsf T1CON, TMR1ON ; start timer1
; return
; delay 842 us
DELAY842
movlf d_num, 0xd2 ; 210 -> d_num
LOOP
nop ; 1 tcy
decfsz d_num ; 1 tcy
goto LOOP ; 2 tcy
return
; delay 100 us
DELAY100
movlf d_num, .25
LOOP100
nop ; 1 tcy
decfsz d_num ; 1 tcy
goto LOOP100 ; 2 tcy
return
; check the pulse whether be ir head
IR_HEAD
btfsc GPIO, GP2
goto IR_END ; GP2 == 1
movlf cir_num, .70
CHECK
call DELAY100
btfsc GPIO, GP2
goto IR_END ; GP2 == 1
decfsz cir_num
goto CHECK
CHECK1
btfss GPIO, GP2
goto CHECK1 ; GP2 == 0
clrf cir_num
CHECK2
call DELAY100
incf cir_num
btfsc GPIO, GP2
goto CHECK2 ; GP2 == 1
movlw .35
subwf cir_num
btfsc STATUS, C
goto IRD_GET ; C = 1 cir_num - 30 > 0
goto IR_END ; C = 0 cir_num - 30 < 0
; 获得IR数据
IRD_GET
clrf ir_num
IR
movlf ir_circle, 0x08
clrf ir_data
IR1
; wait until high
btfss GPIO, GP2
goto IR1 ; GP2 == 0
bcf STATUS, C ; C = 0
rlf ir_data ; << 1
call DELAY842
btfss GPIO, GP2
goto IR_ZERO ; GP2 = 0
bsf ir_data, 0 ; BIT0 = 1
goto IR2
IR_ZERO
bcf ir_data, 0 ; BIT0 = 0
IR2
; wait until low, if is low then pass
btfsc GPIO, GP2
goto IR2 ; GP2 == 1
; if ir_circle == 0
decfsz ir_circle
goto IR1 ; ir_circle != 0
movff tmp_data1, ir_data ; tmp_data1 = ir_data
incf ir_num ; ir_num++
movlw 0x01
subwf ir_num, w
btfsc STATUS, Z
goto IDFY ; ir_num == 0x01
movlw 0x02
subwf ir_num, w
btfsc STATUS, Z
goto IDFY_REV ; ir_num == 0x02
movlw 0x03
subwf ir_num, w
btfsc STATUS, Z
goto USR_DATA ; ir_num == 0x03
movlw 0x04
subwf ir_num, w
btfsc STATUS, Z
goto USR_DATA_REV ; ir_num == 0x04
IDFY
movf tmp_data1 ; check if tmp_data1 == 0
btfss STATUS, Z
goto IR_END ; tmp_data1 != 0
goto IR ; tmp_data1 == 0 and goto IR and continue
IDFY_REV
movlw 0x77
subwf tmp_data1 ; check if tmp_data1 == 0x77
btfss STATUS, Z
goto IR_END ; tmp_data1 != 0
goto IR ; tmp_data1 == 0
USR_DATA
movf tmp_data1 ; check if tmp_data1 == 0
btfss STATUS, Z
goto IR_END ; tmp_data1 != 0
goto IR ; tmp_data1 == 0
USR_DATA_REV
movlw 0xff
subwf tmp_data1 ; check if tmp_data1 == 0xff
btfss STATUS, Z
goto IR_END ; tmp_data1 != 0
bsf bit_flag, GP2PWON ; set GP2PWON bit
IR_END
return
; initialize eeprom locations
;EE CODE 0x2100
; DE 0x00, 0x01, 0x02, 0x03
END
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -