# Wrong increment of TMR0

Status
Not open for further replies.

#### PA3040

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

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:

#### Tahmid

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.

PA3040

### PA3040

points: 2

#### PA3040

Dear Tahmid,
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)

Last edited:

#### Tahmid

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.

PA3040

### PA3040

points: 2

#### PA3040

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

#### Tahmid

Also it would be much appreciated if you can provide me the DNS file that you checked above codes
I've attached it.

#### Attachments

• 25.1 KB Views: 4
PA3040

### PA3040

points: 2

#### PA3040

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?

PA3040

### PA3040

points: 2

#### aashitech

##### Member level 4
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

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 arent good then try increasing the pull-up resistor value...this simple arrangement may help in eliminating noise generated at keypress...

PA3040

### PA3040

points: 2

#### internetuser2k12

##### Banned
Why don't uou use Proteus for simulation and see.

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?

PA3040

### PA3040

points: 2

#### PA3040

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

- - - Updated - - -

Dear aashitech,
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... andyes..first start from a pullup value of say 10k..and if still results arent good then try increasing the pull-up resistor value ...this simple arrangement may help in eliminating noise generated at keypress...
Can you please give me a mathematical solution instead of changing the resister value manually

Last edited:

#### Tahmid

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

- - - Updated - - -

Dear aashitech,

Can you please give me a mathematical solution instead of changing the resister value manually
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).

http://www.softwareforeducation.com/wikileki/index.php?title=Schmitt_Trigger
Go down to the section labelled "Noise Clean Up".

Hope this helps.
Tahmid.

PA3040

### PA3040

points: 2

#### aashitech

##### Member level 4

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

- - - Updated - - -

Dear aashitech,

Can you please give me a mathematical solution instead of changing the resister value manually
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....

PA3040

### PA3040

points: 2

#### Tahmid

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.

PA3040

points: 2