Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Wrong increment of TMR0

Status
Not open for further replies.

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
879
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,907
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:

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
879
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,907
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:

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
879
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,907
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
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
Also it would be much appreciated if you can provide me the DNS file that you checked above codes
I've attached it.
 

Attachments

  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
879
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,907
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
 

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

aashitech

Member level 4
Joined
Oct 7, 2008
Messages
77
Helped
33
Reputation
66
Reaction score
32
Trophy points
1,298
Location
Uttar pradesh (INDIA)
Activity points
1,900
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
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...
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
395
Reputation
784
Reaction score
379
Trophy points
1,363
Location
Pakistan
Activity points
7,263
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?
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

PA3040

Advanced Member level 3
Joined
Aug 1, 2011
Messages
879
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Activity points
6,907
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
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 are`nt 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
Please advice
 

ALERTLINKS

Advanced Member level 4
Joined
Dec 13, 2003
Messages
1,259
Helped
395
Reputation
784
Reaction score
379
Trophy points
1,363
Location
Pakistan
Activity points
7,263
Last edited:

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
Dear ALERTTLINK,
Thanks for reply and advice



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
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.
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

aashitech

Member level 4
Joined
Oct 7, 2008
Messages
77
Helped
33
Reputation
66
Reaction score
32
Trophy points
1,298
Location
Uttar pradesh (INDIA)
Activity points
1,900
Dear ALERTTLINK,
Thanks for reply and advice



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
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....
 
  • Like
Reactions: PA3040

    PA3040

    points: 2
    Helpful Answer Positive Rating

Tahmid

Advanced Member level 5
Joined
Jun 17, 2008
Messages
4,758
Helped
1,791
Reputation
3,574
Reaction score
1,650
Trophy points
1,393
Location
Silicon Valley, California, USA (from Dhaka, Bangl
Activity points
30,545
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.
 
  • Like
Reactions: PA3040

    PA3040

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

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top