• AVR Freaks

Helpful ReplyHot!Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ?

Page: 12 > Showing page 1 of 2
Author
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
2020/03/20 04:50:22 (permalink)
0

Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ?

I have a word union and try accurately retrieve/ reading the 16bit Timer1 value,
by the 8bits register pair(s) TMR1L and TMR1H, like this
word.byte2=TMR1H
word.byte1=TMR1L
 
Or maybe better like this swap the reading order
word.byte1=TMR1L
word.byte2=TMR1H
 
But what if the Timer1 increments in between the two operation(s)/ instruction(s).. :) :(, not to speak about interrupts yet :)
hmm yeah disable and re-enable should work (if it is that critical),
 
or are there other tricks Atomically ways ?
else worst case latency could be +/-1 but can be far more if the value is just about to cross the 8bit LSB boundary
i mean the SFR registers TMR1L and TMR1H are not double buffered by the peripheral architecture right
like a shadow registers reading resort

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#1
1and0
Access is Denied
  • Total Posts : 10929
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 05:15:16 (permalink) ☄ Helpfulby Neiwiertz 2020/03/20 05:35:25
+2 (2)
Neiwiertz
Or maybe better like this swap the reading order
word.byte1=TMR1L
word.byte2=TMR1H

When the timer is setup for 16-bit operation, reading TMR1L will load the content of the high byte of Timer1 into TMR1H.
 
Neiwiertz
i mean the SFR registers TMR1L and TMR1H are not double buffered by the peripheral architecture right
like a shadow registers reading resort

TMR1H is a buffer register for the high byte of Timer1.
 
Edit: XC8 has macros for PIC18 devices that guarantee the correct byte order is used; e.g. foo = READTIMER1().
post edited by 1and0 - 2020/03/20 05:26:34
#2
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 05:38:24 (permalink)
0
1and0
When the timer is setup for 16-bit operation, reading TMR1L will load the content of the high byte of Timer1 into TMR1H.

Cool never know that :) i go adjust it to first use/ read TMR1L and then next use TMR1H
 
Cheers

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#3
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 05:59:23 (permalink)
0
Good catch :) @1and0
 
Its into the DataSheet aswell

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#4
1and0
Access is Denied
  • Total Posts : 10929
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 06:08:20 (permalink)
+2 (2)
Neiwiertz
Its into the DataSheet aswell

Yup, read TMR1L then read TMR1H, and write TMR1H then write TMR1L.
 
#5
NKurzman
A Guy on the Net
  • Total Posts : 18804
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 06:30:32 (permalink)
+2 (2)
You did not specify the chip you are talking about. Assuming it is a 8 bit PIC that is probably true. If it is true you would know it by reading the data sheet.

Note the compiler will do 16 bit operations on TMR1. But in less they fixed it, The two bite operations may not be in the same order. So verify before you use this feature.
#6
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/20 06:43:43 (permalink)
0
NKurzman
You did not specify the chip you are talking about. Assuming it is a 8 bit PIC that is probably true. If it is true you would know it by reading the data sheet.

Yep its a 16F18346
and reading the DS closely is wisdom and the key, yeah i had should pay more attention
 
NKurzman
Note the compiler will do 16 bit operations on TMR1. But in less they fixed it, The two bite operations may not be in the same order. So verify before you use this feature.

Cool to be-aware off, i go check the disassembly listing :) verify L and then H order

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#7
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/24 07:53:29 (permalink)
0
For some reason it is key to still swap the order for reading this means first High byte then Low byte,
I debugged this by running code and helpers safety check variables to detect incorrect value(s) of this variable: word
It took a couple of days to find out :( but last hour i swapped the order and all of a sudden the debug helpers safety check variables never did trigger by a software breakpoint :)
 
word.byte2=TMR1H
word.byte1=TMR1L
 
But the DataSheet DS40001839D-page 286 should be Correct therefore disclaimer ;),
still weird i need to swap them in my particular case.. i go swap them once again and compare/ check the list file :)

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#8
dan1138
Super Member
  • Total Posts : 3651
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/24 08:49:39 (permalink)
+2 (2)
Neiwiertz
For some reason it is key to still swap the order for reading this means first High byte then Low byte,
I debugged this by running code and helpers safety check variables to detect incorrect value(s) of this variable: word
It took a couple of days to find out :( but last hour i swapped the order and all of a sudden the debug helpers safety check variables never did trigger by a software breakpoint :)
 
word.byte2=TMR1H
word.byte1=TMR1L
 
But the DataSheet DS40001839D-page 286 should be Correct therefore disclaimer ;),
still weird i need to swap them in my particular case.. i go swap them once again and compare/ check the list file :)



From decades of experience with the Microchip TIMER1 in 16-bit mode reading the Special Function count Registers for TIMER1 in 8-bit controllers the low 8-bits should be read first then the high 8-bits.
 
What you have posted for your specific case seems to be the opposite of my experience.
 
Could you post an example program that clearly shows how TIMER1 is failing to report the correct count value?
#9
RISC
Super Member
  • Total Posts : 5765
  • Reward points : 0
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/24 10:14:43 (permalink)
+2 (2)
Hi,
The section 27.4 and figure 27.2 from the PIC16F18346 datasheet explains clearly the order for High or Low register  in 16bits mode depending upon read (Low first) or write (High first) operation.
It seems similar to the old PIC16 devices to me
Regards
 
#10
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/25 01:59:08 (permalink)
0
Hello dan1138 and RISC
 
When have time i see if can isolate it within in a short program
and if i can reproduce it with using XC8 compiler due i used CCS compiler now
 
CPU/ MCU Device: PIC16F18346
At DS40001839D-page 32:
I checked and verified (see assembly listing further below) the SFR Addresses: 019h TMR1L  and  01Ah TMR1H
 
At DS40001839D-page 286:
27.4 Timer1 16-Bit Read/Write Mode
Timer1 can be configured for 16-bit reads and writes. When the RD16 control bit (T1CON<1>) is set, the
address for TMR1H is mapped to a buffer register for the high byte of Timer1.
A read from TMR1L loads the contents of the high byte of Timer1 into the Timer1 High
Byte Buffer register. This provides the user with the ability to accurately read all 16 bits of Timer1 without
having to determine whether a read of the high byte, followed by a read of the low byte, has become invalid
due to a rollover between reads.

A write to the high byte of Timer1 must also take place through the TMR1H Buffer register. The Timer1 high
byte is updated with the contents of TMR1H when a write occurs to TMR1L. This allows a user to write all 16
bits at once to both the high and low bytes of Timer1. The high byte of Timer1 is not directly readable or writable in
this mode.

All reads and writes must take place through the Timer1 High Byte Buffer register. Writes to TMR1H do not clear
the Timer1 prescaler. The prescaler is only cleared on writes to TMR1L.
 
assembly listing merged in the #if(0) part ;)

#BYTE TMR1Lbits=0x019
#BYTE TMR1Hbits=0x01A


             ...........  #if(0)  // FAIL
             ...........          word.byte1=TMR1Lbits;
14BF 0020    MOVLB  00
14C0 0819    MOVF   19,W
14C1 0027    MOVLB  07
14C2 00CB    MOVWF  4B
             ...........          word.byte2=TMR1Hbits;
14C3 0020    MOVLB  00
14C4 081A    MOVF   1A,W
14C5 0027    MOVLB  07
14C6 00CC    MOVWF  4C
             ...........  #else  // PASS
             ...........          word.byte2=TMR1Hbits;
14BF 0020    MOVLB  00
14C0 081A    MOVF   1A,W
14C1 0027    MOVLB  07
14C2 00CC    MOVWF  4C
             ...........          word.byte1=TMR1Lbits;
14C3 0020    MOVLB  00
14C4 0819    MOVF   19,W
14C5 0027    MOVLB  07
14C6 00CB    MOVWF  4B
             ...........  #endif

 
At  h t t p://www.microchip.com.tw:  Example of ReadTimer1

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#11
RISC
Super Member
  • Total Posts : 5765
  • Reward points : 0
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/25 12:21:54 (permalink)
+2 (2)
Hi,
As TMR1H is latched when you read TMR1L there cannot be any issue ...or ???
Regards
#12
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/26 00:39:00 (permalink)
0
Hi ReducedInstructionSetComputer :)
 
Yeah i understand i should try to reproduce it on XC8, my debug safeguard check are triggered when the value is too far out of reach/ what it should be and then i see also those FAIL timer read-out 16bit value is not like a rollover but like a jumped value when perform the swap it works without a glitch,
 
i double checked the SFR register address of TMR1H and TMR1L those have correct address value compared to
the datasheet
 
when have time left and can reproduce/ isolate on XC8 i certain let know, we can do a decent investigate then on this
 
Cheers

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#13
1and0
Access is Denied
  • Total Posts : 10929
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/26 04:37:51 (permalink)
+3 (3)
Neiwiertz
Yeah i understand i should try to reproduce it on XC8, my debug safeguard check are triggered when the value is too far out of reach/ what it should be and then i see also those FAIL timer read-out 16bit value is not like a rollover but like a jumped value when perform the swap it works without a glitch,
 
i double checked the SFR register address of TMR1H and TMR1L those have correct address value compared to
the datasheet

I don't quite understand what you are saying. Like Dan said, "could you post an example program that clearly shows how TIMER1 is failing to report the correct count value" even if it's CCS code?  What value you are getting and what value do you expect?
#14
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 01:40:43 (permalink)
0
"TMR1H is latched when you read TMR1L"
the TMR1H msb-value should be available at the Very next instruction right ? just after reading TMR1L
(i even tested with an additional nop instruction in between)
 
i am counting high speed external output-stream of IO pulse(s) which are feed into this timer as input-stream,
this external output-stream is generated by a other CCP peripheral this output-stream change(s) at exact the instruction rate therefore high speed
post edited by Neiwiertz - 2020/03/27 10:08:33

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#15
ric
Super Member
  • Total Posts : 27752
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 02:12:46 (permalink)
+2 (2)
Neiwiertz
the TMR1H msb-value should be available at the Very next instruction right ? just after reading TMR1L

Yes
 
I don't think you've shown your code to init Timer 1.
Have you actually set the RD16 bit?

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#16
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 04:06:41 (permalink)
0
ric
Yes

Cool to verify TMR1H it available at the Very Next instruction therefore no latency
ric
I don't think you've shown your code to init Timer 1.
Have you actually set the RD16 bit?

Hmmm.. No
RD16 operation bit is only mentioned at DS40001839E-page 286 and further searches at the DataSheet no reference of RD16 at pages 32, 284, 293
 
See DS40001839E-page 32
N0000 00-0 uuuu uu-u

"Timer1  can  be  configured  for  16-bit  reads  and  writes.When  the  RD16  control  bit  (T1CON<1>)  is  set"
Hmmm.. i think RD16 is un-documented.. i go test it with RD16 (T1CON<1>) set
 
 
i go have look if RD16 still exist at p16F18346.h

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#17
Neiwiertz
Super Member
  • Total Posts : 2112
  • Reward points : 0
  • Joined: 2004/09/01 02:58:52
  • Status: offline
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 04:09:49 (permalink)
0
Is the forum message Edit feature broken ? i get access denied

Flying With --|Explorer 16|HardWare|SoftWare|-- Fav(s) Gallery Lists
#18
ric
Super Member
  • Total Posts : 27752
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 04:16:33 (permalink)
+1 (1)
Neiwiertz
i go have look if RD16 still exist at p16F18346.h

It's not, but that is auto generated form the dataasheet, so they might both be wrong.
Test it and see.
 

Is the forum message Edit feature broken ? i get access denied

That's because your message contains a live URL.
 
post edited by ric - 2020/03/27 04:17:58

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#19
ric
Super Member
  • Total Posts : 27752
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Reading Timer1 register pair TMR1L and TMR1H into a 16bit integer Atomically ? 2020/03/27 04:18:56 (permalink) ☄ Helpfulby Neiwiertz 2020/03/27 04:57:06

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5