Help on interrupt

Status
Not open for further replies.

andyy07

New Member
Hi all.

I have a problem with interrupt service on a PIC16F877A with 32768khz crystal

The thing is that i've set the timer0 on overflow interrupt and once at 10 seconds an A/D sample routine that is in page 1, must be called.

The problem is as follows:

If I set a flag in the isr and test that flag in the main then call the A/D routine, everything is ok,
but the thing is I don't want it to be like that, the A/D routine must be called from the isr

When the A/D routine is called from the isr the program locks or has a strange behaviour

here is an example of how it looks like


org 0x04
bcf INTCON,GIE

movwf w_safe ;given multiple banks, and no global memory
;you need to make a w_temp in each because you cannot change status prior to saving it.
swapf STATUS,W ;The swapf instruction, unlike the movf, affects NO status bits, which is why it is used here.
clrf STATUS ;point to bank 0 and clear all the flags
movwf status_safe
movf PCLATH,W ;save PCLath
movwf pclath_safe
clrf PCLATH ;assume that this ISR is in page 0
movf FSR,W
movwf fsr_safe

; ISR

;************************************************

btfss INTCON,T0IF
goto isr_1
bcf INTCON,T0IF
banksel tmr_cnt
incf tmr_cnt,F
movlw d'36'
subwf tmr_cnt,W
btfss STATUS,Z
goto isr_1
incf tmr_cnt2,F
clrf tmr_cnt

movlw d'10'
subwf tmr_cnt2,W
btfss STATUS,Z
goto isr_1
bsf chg_sample,0 ;sample it
clrf tmr_cnt2

pagesel AD_conv_chg ;when is called from here it's not working
call AD_conv_chg

isr_1
;***********************************************

movf fsr_safe,W
movwf FSR

;to complete the restore sequence.

movf pclath_safe,W
movwf PCLATH
swapf status_safe,W
movwf STATUS
swapf w_safe
swapf w_safe,W

retfie


Main

banksel chg_sample ;but it works like this
btfss chg_sample,0
goto m_1
pagesel AD_conv_chg
call AD_conv_chg
bcf PCLATH,3 ;main is in page 0

m_1
; rest of code

end
 
Cay you post the AD_conv_chg routine. Please put
Code:
 and [/co de] (without the space) before and after your code.

Mike.
 
Yes the chg_sample is cleared in the AD routine.

The bcf PCLATH,3 is put because those routines are in page 0

AD_conv_chg

bcf STATUS, RP1
bsf STATUS, RP0 ;bank 1

movlw b'10000101' ;RA0,RA1 analog input, Vref+,Vref- internal
movwf ADCON1

bcf STATUS,RP0 ;bank 0

movlw b'11001001' ;A/D module on, conversion clock RC, channel selected 1
movwf ADCON0

bsf ADCON0,2 ;start conversion

;wait for the conversion to end
btfsc ADCON0,2
goto $-1

movf ADRESH,W
andlw 0x03 ;clear upper 6 bits of ADRESH to make sure
movwf NumH
movwf adresH2_tmp

bsf STATUS, RP0 ;bank 1
movf ADRESL,W

bcf STATUS, RP0 ;bank 0
movwf NumL
movwf adresL2_tmp

bcf PCLATH,3
call Convert ;these lines are used to see the value of the A/D result

movlw 0x0A
bcf PCLATH,3
call LCD_Line2W

movf Hund,W
bcf PCLATH,3
call LCD_CharD

movf Tens,W
bcf PCLATH,3
call LCD_CharD

movf Ones,W
bcf PCLATH,3
call LCD_CharD

res_compare

;here the result is compared with a previously result

banksel adresH3_tmp
movf adresH3_tmp,W ;Xhi
banksel adresH2_tmp
subwf adresH2_tmp,W ;Yhi

btfss STATUS,Z

goto res_16

banksel adresL3_tmp
movf adresL3_tmp,W ;Xlo
banksel adresL2_tmp
subwf adresL2_tmp,W ;Ylo

res_16

;if X<=Y then STATUS,C=1,and store the result in adresH3_tmp, and andresL3_tmp

btfss STATUS,C
goto ad2_3

movlw 0x09
bcf PCLATH,3
call LCD_Line2W

movlw 'X' ;these are here to see if compare works and it does
bcf PCLATH,3
call LCD_Char
bsf PCLATH,3

goto ad2_1

ad2_3
movlw 0x09
bcf PCLATH,3
call LCD_Line2W

movlw '<'
bcf PCLATH,3
call LCD_Char
bsf PCLATH,3

bcf INTCON,T0IE ;disable timer0 int

goto ad2_2
ad2_1

banksel adresL2_tmp
movf adresL2_tmp,W
banksel adresL3_tmp
movwf adresL3_tmp


banksel adresH2_tmp
movf adresH2_tmp,W
banksel adresH3_tmp
movwf adresH3_tmp

ad2_2
bcf STATUS,RP0
bcf STATUS,RP1


clrf chg_sample

return

The chg_sample flag is no longer needed if the routine is called from isr, but as i said before it dosen't really work when is called from isr
 
OMG that return is too much long you should end that return instruction after this line movwf adresL2_tmp.So the AD_conv_chg routine is seperate from LCD routines.Keep your ISR as short as possible.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…