Wrong increment of TMR0

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Visit site
Activity points
6,936
Dear All,

Today I am working with TIMR0 of PIC 16f877a

I configured TIMER0 to increment from external clock (T0CKI) and assigned Pres caller to TMR0 Rate . I check both my hardware and PIC IDE SIMULATOR

Prescaler set to 1 : 8

I incremented T0CKI in the PIC IDE SIMULATOR. in the first time after four times switch press, the TMR0 incremented. (wrong increment ) from second steps onwards TIMER0 incremented every eight switch press ( Correct increment) what is the reason in the first time it was incremented by four times switch press
Please advice


Code:
#include <p16f877a.inc>
__config 3f39
			org		0x000
Start		goto	main
			org		0x004
			goto	isr
main		call	sys_init
			call	led

led			movf	TMR0,W
			movwf	PORTC
			goto	led

isr			btfss	INTCON,TMR0IF	;Clear TMR0 Overflow Interrupt Flag bit
			retfie
			bcf		INTCON,TMR0IF	;Clear TMR0 Overflow Interrupt Flag bit
			clrf	PORTC
			rlf		PORTD,F
			retfie

sys_init	clrf	PORTC
			clrf	PORTD
			banksel	TRISC
			clrf	TRISC
			clrf	TRISD
			movlw	b'00100010'
			movwf	OPTION_REG
			banksel	PORTC
			movf	TMR0,w
			clrf	TMR0
			bsf		PORTD,0
			bcf		INTCON,TMR0IF	;Clear TMR0 Overflow Interrupt Flag bit
			bsf		INTCON,TMR0IE	;Enables the TMR0 interrupt
			bsf		INTCON,GIE		;Global Interrupt Enable bit
			return


end
 
Last edited:

Well, I simulated this on Proteus and it's working fine. I don't face the issue you face. I also don't see anything such mentioned in the datasheet. Maybe it's a "fault" with PIC Simulator IDE.

Before you press the button for the first time, wait a few seconds. Then start pressing the switch.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Tahmid,
Thanks for reply
I checked with actual hardware. it is designed by me. It has micro switch connected to RA4
I sow that TIMR0 incrementing difference sequence

As per above program it reads TMR0 value and put the PORTC. as per the program PORTC must increment every eight switch pressed .
(Prescaler set to 1 : 8)
Please advice
 
Last edited:

I sow that TIMR0 incrementing difference sequence

Do you mean that there is no sequence/order to the incrementing? Is it not incrementing every eight switch presses?

I think you need to use a switch debounce circuit. Add a switch debounce circuit and then test again.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Tahmid,

Do you mean that there is no sequence/order to the incrementing? YES
Can I have the switch debounce sample circuit. can we implement switch debounce in software?

Also it would be much appreciated if you can provide me the DNS file that you checked above codes

Thanks in advance
 

Also it would be much appreciated if you can provide me the DNS file that you checked above codes

I've attached it.
 

Attachments

  • Wrong increment.zip
    25.1 KB · Views: 89
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Tahmid,
Great thanks for the help
Do you mean that there is no sequence/order to the incrementing? Is it not incrementing every eight switch presses?

Due to debouching issued. is it not incrementing every eight switch presses or any other reason?
Please advice
 

Due to debouching issued. is it not incrementing every eight switch presses or any other reason?

I didn't understand this.

Check here for debouncing:

Hope this helps.
Tahmid.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating

NO..in present situation you can not use any software means for switch denouncing as here you are not dealing with any IO pin...but you are here providing a external clock to internal timer...just think of this pin as a clock input path to the internal timer scalper .. presuming that you already had a pull up resistor connected on this pin....just try adding a simple 100 nf capacitor from this pin to ground... and yes..first start from a pullup value of say 10k..and if still results are`nt good then try increasing the pull-up resistor value...this simple arrangement may help in eliminating noise generated at keypress...
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
I simulated in PIC Simulator IDE, RealPIC Simulator and Proteus. It was only in PIC Simulator IDE that it incremented PortC with only four cycles on TOCkI pin in start (good catch). Other simulators were with eight pulses from start.
Personally i don't doubt "PIC simulator IDE" much. It was only possible for me to achieve otherwise impossible targets only using this software. Similar was a case when timer1 interrupt showed 2 cycle difference with "Real PIC Simulator" results. Did your hardware respond similar?
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear ALERTTLINK,
Thanks for reply and advice

Did your hardware respond similar?

Yes, my hardware also respond the same problem at all time not only first button press due to denouncing issue

- - - Updated - - -

Dear aashitech,
Thanks for reply

Can you please give me a mathematical solution instead of changing the resister value manually
Please advice
 

Switch de-bouncing can be implemented in software. It is checking input multiple of times with small delays to confirm switch hard press. If contact looses in between, discard input result and rescan.
Did your hardware gives output after 4 presses or it is different every time?

**broken link removed**


http://www.rentron.com/myke6.htm
**broken link removed**
 
Last edited:


Did you see the link I provided? There is a circuit employing a schmitt trigger input inverter.

Schmitt trigger: http://en.wikipedia.org/wiki/Schmitt_trigger


The effect of using a Schmitt trigger (B) instead of a comparator (A).


**broken link removed**
Go down to the section labelled "Noise Clean Up".



Hope this helps.
Tahmid.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating

Nothing special...just first give a try starting with 4.7k Resistor...then move up..next try 10k then 15k or 22k...hopefully...a 10k will suffice....
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
If an experimental method to find the required resistance is preferred, instead of using multiple resistors, removing and reconnecting them, you may just use a variable resistor in place of the resistor. Adjust the resistance of the variable resistor to find the resistance required.
 
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…