CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Stopping and re-starting a timer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

Stopping and re-starting a timer
PostPosted: Sat May 14, 2022 1:46 am     Reply with quote

The PIC18LF46K22 datasheet has this paragraph in Section 12.5.1 (READING AND WRITING TIMER1/3/5 IN ASYNCHRONOUS COUNTER MODE):
"Reading TMRxH or TMRxL while the timer is running from an external asynchronous clock will ensure a valid read (taken care of in hardware). However, the user should keep in mind that reading the 16-bit timer in two 8-bit values itself, poses certain problems, since the timer may overflow between the reads. For writes, it is recommended that the user simply stop the timer and write the desired values. A write contention may occur by writing to the timer registers, while the register is incrementing. This may produce an unpredictable value in the TMRxH:TMRxL register pair."

Questions:
1. How does one stop and re-start Timer1, or for that matter, any other timer, without accessing the register directly and without affecting the other settings of the timer?
2. Is it correct that if we use the get_timer1() function, which returns a 16-bit integer, the above read problem will never happen?
3. Similarly, is it correct that if we use the set_timer1() function to write a value to Timer1, the above write problem will never happen?


Last edited by kgng97ccs on Sat May 14, 2022 3:33 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21644

View user's profile Send private message

PostPosted: Sat May 14, 2022 2:34 am     Reply with quote

For question #1, see the #int_timer1 routine in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=45458&start=5
PCM programmer



Joined: 06 Sep 2003
Posts: 21644

View user's profile Send private message

Re: Stopping and re-starting a timer
PostPosted: Sat May 14, 2022 12:13 pm     Reply with quote

For question #2:
kgng97ccs wrote:

2. Is it correct that if we use the get_timer1() function, which
returns a 16-bit integer, the above read problem will never happen?

Look at the code for 18F46K22:
Code:

...... setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_ENABLE_SOSC);
00022:  MOVLW  8F   // Set RD16 bit (bit 1)
00024:  MOVWF  T1CON
00026:  CLRF   T1GCON
......
...... temp = get_timer1();
00028:  MOVF   TMR1L,W
0002A:  MOVWF  temp
0002C:  MOVFF  TMR1H,temp+1

The method above follows the 18F46K22 data sheet in section:
12.6 Timer1/3/5 16-Bit Read/Write Mode
So yes, this method should safely get the 16-bit value without
risk of rollover between bytes.

The compiler also sets the RD16 bit =1 if the internal oscillator is used
as the Timer1 clock source:
Code:

...... setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
00022:  MOVLW  07     // Set RD16 bit (bit 1)
00024:  MOVWF  T1CON
00026:  CLRF   T1GCON
......
...... temp = get_timer1();
00028:  MOVF   TMR1L,W
0002A:  MOVWF  temp
0002C:  MOVFF  TMR1H,temp+1


You should be able to get the answer to your 3rd question just by
following the same method as above.
Ttelmah



Joined: 11 Mar 2010
Posts: 17981

View user's profile Send private message

PostPosted: Mon May 16, 2022 12:28 am     Reply with quote

And, on '1', this depends on the timer, and what you actually 'mean'.
For example if you look at Timer3, there is an 'on' bit, but it makes the
comment (for off), "Clears Timer1/3/5 Gate flip-flop". So turning this
'off' (which will stop the timer), also clears this flip-flop. Means you cannot
stop it without this changing.
Timer0, does not have this problem, and this has a T0_OFF setting.
Generally, you don't stop timers. Just reset them, read and write them.
If you want to stop their 'effect', just disable the interrupt from them.
kgng97ccs



Joined: 02 Apr 2022
Posts: 46

View user's profile Send private message

PostPosted: Wed May 18, 2022 10:10 pm     Reply with quote

Thank you, PCM Programmer, for the link and for directing me to the assembly code. I am not familiar with assembly language but will make an effort to understand the code.

Thank you, Ttelmah, for your advice regarding stopping a timer and for highlighting its effect on the Timer1/3/5 gate flip-flop. I have taken note of that.

If you know of any good resources for learning assembly language relevant to PIC MCU programming, please do let me know. Thank you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group