Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Using a PIC16F877A and having trouble with INTERRUPT and FLAGS

Status
Not open for further replies.

si2030

Member
Hi Guys,

OK I thought I had this working where I use a flag in the interrupt. Unfortunately its not now.

When its in the MAIN loop and i click on RB4 it interrupts (fine) and sets the flag bit 0 however when it returns to the loop again the btfss ignores the flag and it continues to loop... if I again hit the stimulus it now goes to that routine but when it does the FSR does not accept the value in W leaving the indirect address with H'00'.

Code:
SHIFT_KEYS
		clrf 	FSR
		
SET_FSR		
		movf	FCHAR_MAX,W
		subwf	FCHAR_STRT,W
		movwf	FSR			;FSR IS AT THE FIRST ADDRESS ENTERED.
		movf	FCHAR_MAX,W	;FCHAR_MAX IS OUR RUNNING COUNT.
		movwf	FCHAR_CNT	;WE CAN DEC THIS VAR WITHOUT SCREWING UP CHAR CNT.
		btfss	STATUS,Z	;CHECK FCHAR_CNT > 0?
		goto 	EXCEED_MAX_FREQ? 	;WE HAVE MORE THAN ONE CHARACTER IN OUR FREQ..
							;ELSE ITS THE FIRST CHAR...		
		movf	LAST_CHAR,W	;THIS IS THE FIRST CHAR SO CHECK IF ITS >"0".
		btfss	STATUS,C	;CHECK FCHAR_CNT = 0?
		goto	ZERO_ERROR	;DROP OUT. CANT HAVE "0" AS FIRST CHAR. 
		incf	FCHAR_MAX	;YES ITS > "0" SO WE CAN NOW INC OUR COUNTERS.
		incf	FCHAR_CNT
		movwf   INDF		;[COLOR="Red"]Happens here INDF is not applied to address 027[/COLOR]

I have included a video of it (including unknowingly some music)...

**broken link removed**

and I have also included the whole program including the two include files...

It would be much better to get this method to work rather than manage the functionality required from within the interrupt..

Kind Regards

Simon
 

Attachments

  • FUNCTION_GEN..zip
    41.4 KB · Views: 141
Hi Guys,

OK I thought I had this working where I use a flag in the interrupt. Unfortunately its not now.

When its in the MAIN loop and i click on RB4 it interrupts (fine) and sets the flag bit 0 however when it returns to the loop again the btfss ignores the flag and it continues to loop... if I again hit the stimulus it now goes to that routine but when it does the FSR does not accept the value in W leaving the indirect address with H'00'.

Simon

hi,
Look at this image.

AAesp01..gif
 
Hi there guys,

This program is still giving me significant problems with the way it works in the simulator...

Its quite simple but the simulator is ignoring a direction as I can see... I have set a flag in the interrupt.. the file register indicates the flag is set yet in a continuous loop the btfsc directive is ignored... As far as I can ascertain it shouldnt...

Here it is working (or not) in a quick video..

**broken link removed**

I have included the full set of files for MPLAB... including the stimulus file... I have set RB4 to toggle...

Kind Regards

Simon
 

Attachments

  • FUNCTION_GEN..zip
    40.8 KB · Views: 150
The reason it's not working is because bank 2 is selected in your main loop. The reason bank 2 is selected is because you don't save the status register correctly in your ISR. Check section 14.12 of the data sheet for the correct way to save the context in your ISR.

Mike.
 
I have to say I was floundering there... and you have really helped me thanks Mike.

As you suggested I went to the datasheet, copied over the entire interrupt section for saving off the registers etc including the movement to bank 0 correctly and then placed my code for debouncing the keypad button and setting the flag and it worked flawlessly...

Thanks again..

Simon

Here is the modified code courtesy of the datasheet.

Code:
INTERRUPT	org	ISR_V		;INTERRUPT VECTOR LOCATION.

		movwf	W_TEMP 		;COPY W TO TEMP REGISTER
		swapf	STATUS,	W 	;SWAP STATUS TO BE SAVED INTO W
		clrf	STATUS 		;BANK 0, REGARDLESS OF CURRENT BANK, CLEARS IRP,RP1,RP0
		movwf	STATUS_TEMP ;SAVE STATUS TO BANK ZERO STATUS_TEMP REGISTER
		movf	PCLATH, W 	;ONLY REQUIRED IF USING PAGES 1, 2 AND/OR 3
		movwf	PCLATH_TEMP ;SAVE PCLATH INTO W
		clrf	PCLATH 		;PAGE ZERO, REGARDLESS OF CURRENT PAGE

		;call	DELAY_15MS

		movlw   B'11110000'	;CHECK FOR KEY DOWN.
		movwF   TEMP  
		movf    PORTB,	W
		subwf   TEMP,	F
		btfsc	STATUS, Z
		goto    DONE		;ALL KEYS UP SO RETURN.
		
		bsf		FLAGS,	KEYS
		
DONE
		clrf    PORTB		;CLEAR PORT B.
		clrf    PORTD		;CLEAR PORT D.
		movf    PORTB,	F

 		bcf     INTCON,RBIF;CLEAR Port B interrupt FLAG

		movf	PCLATH_TEMP, W ;RESTORE PCLATH
		movwf	PCLATH 		;MOVE W INTO PCLATH
		swapf	STATUS_TEMP,W ;SWAP STATUS_TEMP REGISTER INTO W
							;(SETS BANK TO ORIGINAL STATE)
		movwf	STATUS 		;MOVE W INTO STATUS REGISTER
		swapf 	W_TEMP,F 	;SWAP W_TEMP
		swapf 	W_TEMP,W 	;Swap W_TEMP into W

		retfie
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top