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.

Problems programming an interupt.

Status
Not open for further replies.

andy257

Member
Hi all

I am currently doing a project which requires the timing of signals via a PIC.

i am using the timer counter to count the time it takes between one input going high (input switch) and another input going high (external sensor). I am using the PIC16F84 and pin RBO to configure the external interupt. The external sensor is connected to RBO.

The problem lies with the PIC while it is waiting for the external interupt. If i put the pic in a loop until it recieves the external interupt, when i come out of the interupt it goes back into the loop. This is no good if i want to do something with the time/count after the interupt. It means all the stuff i want to do after the interupt i have to include inside the interupt.

Is there a way around this???? I was thinking about the sleep function instead of a loop but that would stop the clock which is needed for the PIC to continue counting.

Any ideas?

Cheers

Andy
 
Do as per Nigel's suggestion.

Or use a single instruction:

loop Goto loop ;wait here for an interrupt to happen

After an interrupt has occurred, the next instruction following the "loop" will then be executed.
 
An RETFIE should return to where it was called from, in this case it wouldn't cause it to leave the loop if it did - unless you've proved it differently, which is why I suggested it as a 'bug' if it does.
 
Nigel Goodwin said:
An RETFIE should return to where it was called from, in this case it wouldn't cause it to leave the loop if it did - unless you've proved it differently, which is why I suggested it as a 'bug' if it does.

It is basically how subroutines works. If it is like what you said, no subroutine call will ever works.

All subroutine calls, and interrupt included, in every CPU I know of, would store the address of the NEXT instruction onto the stack so that a RET or RETFIE will branch and starts execute the next instruction.

It will definitely not returning to where it was called, but to the instruction that follows the current instruction while interrupt occurs.
 
Last edited:
I think you got it wrong there Mr Chung. Or, you are right, the next instruction is executed. As the next instruction is Goto Loop, then that is what it will do.

Mike.
 
I see where I'm wrong.

For normal instructions except branching instructions, the next instruction is the instruction following the current instruction.

For branching instruction, the next instruction is the target address of the branching instruction and thus this target address will be saved onto the stack.
It is true we are in an endless loop.

Thanks all for pointing out my mistake.
 
eblc1388 said:
Do as per Nigel's suggestion.

Or use a single instruction:

loop Goto loop ;wait here for an interrupt to happen

After an interrupt has occurred, the next instruction following the "loop" will then be executed.

I am not sure if you understood my problem but what you have said it exactly my problem. After an interupt it goes back into the loop.

thanks Nigel i will try your suggestion.
Cheers

Andy

UPDATE: sorry i did not read the other posts. eblc1388 thank you for you reply, i may have other questions later.

thank You.
 
Last edited:
Do you have any suggestions which flag i might check. I was thinking of checking the global interupt flag but i think its best practise to reset that flag once inside interupt otherwise if another interupt comes along the program wont respond to another interupt. Am i correct in this???

Cheers

Andy
 
You create your own flag in a GPR, this is a sample from a file created by my BASIC compiler.

Code:
;Flags register bit definitions
LEADZ         EQU   0x00   ;set to show leading zeros
OverFlow      EQU   0x01   ;set for add/sub overflow

;Pre-defined variables
Flags         EQU   60

So to set a flag you would use 'BSF Flags, OverFlow', or to clear one use BCF, and you test with the usual bit test instructions. So your main routine would clear the flag before the loop, and the loop would test for the flag being set - the routine run when the flag is set would clear it again.
 
Alternatively, don't use interrupts at all. Simply pole the RB0 bit.

I.E.
Code:
Loop	btfss	INTCON,INTF
	goto	Loop
	bcf	INTCON,INTF

;	do your other stuff here

	goto	Loop

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top