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.

external interrupt frequency counter pic

Status
Not open for further replies.

neelam29

New Member
hi m using this program to make a frequency counter using pic16f628a and external interrupt is on pin rb0 . method m using is measuring time betwenn two rising pulses. one the rising edge comes i start the timer and on another i stop it .and then calculate the frequency.code is as shown below.

m not getting the correct timer values.
Code:
;=====================================================================
;		INTERRUPT SERVICE ROUTINE
;=====================================================================

OVFL_ISR
		movwf   TEMP              ;Store the value of w temporarily 

		btfss	INTCON, INTF		;check if it's a external interrupt or not!
			retfie			;else check other interuppt or return

		bcf		INTCON, INTF
		
		MOVLW	B'00000001'
		XORWF	T1CON, F
		BTFSC	T1CON, TMR1ON
			RETFIE
		
		MOVF	TMR1H, W
		MOVWF	TIMER1_HIGH
		MOVF	TMR1L, W
		MOVWF	TIMER1_LOW
		
		CLRF	TMR1H
		CLRF	TMR1L	
		movfw   TEMP              	
		retfie
;=====================================================================

please let me know is my code correct?
 
Your ISR has got a number of flaws.

When an interrupt comes along, you don't know what the processor may be doing at that point. It may be about to store W somewhere and so you need to save W (as you do - but don't always restore it.). It may be about to test a flag and so you need to save the STATUS register.

This is done like so,
Code:
interrupt
                movwf   int_work
                swapf   STATUS,W
                movwf   int_status
                clrf    STATUS;  ensure were in bank 0

     ; other ISR stuff

exit_isr
                swapf   int_status,W
                movwf   STATUS
                swapf   int_work,F;	swap to file
                swapf   int_work,W;	swap to work
                retfie

Because of this, you can't use retfie in the middle of your ISR you have to use goto exit_isr.

The above maybe causing your program to crash. Correct these errors and see what happens.

I don't understand what you are trying to do with Timer1. Your code for Timer2 should work fine except you should clear TMR1L before TMR1H.

HTH

Mike.
 
P.S. the variables int_status and int_work should be in the common ram area I.E. between 0x70 and 0x7f.
And, for the more experienced out there, I know that strictly speaking, only status needs to be common.
 
Re: Frequency Counter Project.

PIC Coder said:
Hi Neelam,
Can you please send me full project for frequency counter.
Bye

Try searching on google, MicroChip have a very old application note for a 50MHz frequency counter, and there's an updated version, originally by Weeder Technology.
 
Pommie said:
P.S. the variables int_status and int_work should be in the common ram area I.E. between 0x70 and 0x7f.
And, for the more experienced out there, I know that strictly speaking, only status needs to be common.

My understanding is that only int_work need to be in the common as we must restore W last. Int_status can be situated in Bank0.
 
eblc1388 said:
Pommie said:
P.S. the variables int_status and int_work should be in the common ram area I.E. between 0x70 and 0x7f.
And, for the more experienced out there, I know that strictly speaking, only status needs to be common.

My understanding is that only int_work need to be in the common as we must restore W last. Int_status can be situated in Bank0.

You are of course correct. The correct way would be,
Code:
                movwf   int_work 
                swapf   STATUS,W 
                bcf     STATUS,RP0
                bcf     STATUS,RP1
                movwf   int_status ;  this can now be in bank 0
Note to self, must remember to engage brain before moving fingers.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top