最近想做个测速的设备
但是碰到一些问题,是关于中断的,在关掉中断的情况下led能正常动态显示。但开中断后
一旦中断响应,就出不了中断。
不知道为什么,我已经清楚标志位并保护了现场。以下是问题程序。
附件为开发板原理图
list p=16f877
#include "p16f877a.inc"
__config b'0x3b31
cunt equ 30h;ccp中断计数暂存
cunt_pro equ 31h;用于计算中间寄存器
temp_cunt equ 32h;用于显示代码暂存
ge equ 33h;个位
shi equ 34h;十位
bai equ 35h;百位
mpy_h equ 36h;乘积高8位
mpy_l equ 37h;乘积低8位
mpy_temp1 equ 38h;乘法暂存器1
mpy_temp2 equ 39h;乘法暂存器2
mpy_temp3 equ 4ah;乘法暂存器3
time_cunt equ 4bh;时间寄存器
flg equ 4ch;标志寄存器(bit0为2s时间到,bit1表示ccp1中断,bit3表示允许tmr1中断)
temp equ 4dh;中间寄存器
status_temp equ 4eh;状态位暂存
w_temp equ 4fh;w暂存
pcl_temp equ 50h;pcl暂存
org 000h
nop
movlw 0ah
movwf time_cunt
goto main
org 004h
nop
goto int
main
call io_reset;io初始化
call int_reset;中断初始化
clrf flg
banksel portc
loop_check
btfss portc,5
goto loop_check
call delay750us
btfss portc,5
goto loop_check;按键程序
movlw b'
movwf portd
call sc;按键后点亮灯
banksel pie1
bsf pie1,2;允许ccp中断
banksel pir1
goto loop_dsp
;----------主程序--------
loop_dsp
movf ge,0
movwf temp_cunt
call table
movwf portb
call sc_ge;374锁存
movf shi,0
movwf temp_cunt
call table
movwf portb
call sc_shi;374锁存
movf bai,0
movwf temp_cunt
call table
movwf portb
call sc_bai;374锁存
goto loop_dsp
;-----------动态显示----------------
;------------主程序--------------
io_reset
banksel trisc
movlw 0ffh
movwf trisc
movlw 00h
movwf trise
movwf trisd
movwf trisb
banksel portc
clrf portc
clrf portd
clrf portb
clrf porte
clrf ge
clrf shi
clrf bai
clrf flg
clrf time_cunt
call sc
return
;-----------io初始化---------------
int_reset
banksel intcon
movlw b'开总中断,开第二梯队中断
movwf intcon
banksel pie1
movlw b'
movwf pie1;只允许tmr1,ccp未开中断
banksel pir1
movlw b'
movwf t1con;分频比1:8,不同步,关tmr1
movlw b'
movwf ccp1con
clrf pir1;清第二中断标志
clrf tmr1l
clrf tmr1h
return
;----------中断初始化--------------
int
banksel pie1
clrf pie1
banksel portc
movwf w_temp;保护w内容
movf status,0
movwf status_temp;保护状态位
movf pcl,0
movwf pcl_temp;保护pcl
btfss pir1,2;ccp中断否
goto int_retfie;goto int_next
goto int_retfie;goto ccp_int
int_next
btfss flg,3;是否允许tmr1中断
goto int_retfie
btfss pir1,0;tmr1中断否
goto int_retfie
bcf pir1,0
decfsz time_cunt,1;减一为0则转移
goto int_retfie
bsf flg,0;2s时间到,flg bit0置位
movlw 0ah
movwf time_cunt;时间计数器重置
goto int_retfie
;-----------中断服务----------------
ccp_int
movlw 0ah
movwf temp
incf cunt,1;ccp中断计数器加1
incf ge,1 ;个位加1
movf temp,0
subwf ge,0
btfss status,2
goto int_retfie
incf shi,1
clrf ge
movf temp,0
subwf shi,0
btfss status,2
goto int_retfie
incf bai,1
clrf shi
movf temp,0
subwf bai,0
btfss status,2
goto int_retfie
clrf ge
clrf shi
clrf bai
goto int_next
int_retfie
banksel pie1
bsf pie1,0
bsf pie1,2
banksel portd
movf pcl_temp,0
movwf pcl
movf status_temp,0
movwf status
movf w_temp,0
retfie
;---------ccp中断服务---------
pro_2s
banksel pie1
movlw 00h
movwf pie1;关所ccp,tmr1中断
banksel pir1
bcf t1con,0;关闭定时器1
clrf tmr1l
clrf tmr1h
movf cunt
movwf cunt_pro;把cunt(ccp中断次数计数器)值送往cunt_pro计数器
clrf cunt;清ccp中断计数器
clrf ge
clrf shi
clrf bai
banksel pie1
movlw 05h ;b',允许tmr1,ccp中断
movwf pie1
bankpir1
bsf t1con,0;开定时器1
bsf flg,3 ;开定时器软件中断标志
bcf flg,0 ;清时2s间到标记
pro2s_check
btfss flg,0
goto pro2s_check
banksel pie1
movlw 00h
movwf pie1
banksel pir1
bsf portd,6
call sc
goto loop_dsp
;---------------计算转速-------------
delay1ms: movlw 09fh
movwf 20h
lp01: movlw 0ffh
movwf 21h
lp11: movlw 02ch
movwf 22h
lp21: decfsz 22h
goto lp21
decfsz 21h
goto lp11
decfsz 20h
goto lp01
return
;----------1ms延时程序---------------
delay750us:
movlw 0ffh
movwf 26h
loop750
nop
nop
nop
nop
decfsz 26h,1
goto loop750
return
;----------750us延时程序----------------
delayn
nop
nop
nop
nop
nop
return
sc
movlw 06h
movwf porte
call delay750us
clrf porte
return
;--------374锁存程序--------------------
sc_ge
movlw 01h
movwf portd
movlw 06h
movwf porte
call delayn
clrf porte
return
;-------动态个位显示锁存----------------
sc_shi
movlw 02h
movwf portd
movlw 06h
movwf porte
call delayn
clrf porte
return
;-------动态十位显示锁存----------------
sc_bai
movlw 04h
movwf portd
movlw 06h
movwf porte
call delayn
clrf porte
return
;-------动态百位显示锁存----------------
table
movf temp_cunt,0
addwf pcl,1
retlw 0d7h
retlw 012h
retlw 0cbh
retlw 05bh
retlw 01eh
retlw 05dh
retlw 0ddh
retlw 013h
retlw 0dfh
retlw 05fh
;-----------显示解码程序-----------------
end