View previous topic :: View next topic |
Author |
Message |
alexbilo
Joined: 01 Jun 2004 Posts: 39 Location: Trois-Rivières
|
reading 16-bits registers |
Posted: Sun Feb 06, 2005 12:37 pm |
|
|
Hi all,
Does any1 would know how to do read a 16-bits register (TMR1H:TMR1L per example) without using the built-in make16 function?
I built one using pointers and it works, but it access the registers using an indirect addressing operation. What I would like to do is the equivalent of the make16 function, which directly stores the upper byte in the upper byte of the variable and samething for the lower byte.
The reason for all this is that I must produce portable code that doesn't rely on the built-in functions of a compiler.
Thanks, _________________ Alex |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Mon Feb 07, 2005 12:16 pm |
|
|
If your really accessing the timer variables I suggest you use the built in functions.
GET_TIMER1()
At least compile using the built-in functions and observe the assembly created and emulate it. Reading from a 16-bit counter in bytes has a risk of reading incorrectly during a overflow/underflow of the upper or lower byte. |
|
|
Hans Wedemeyer
Joined: 15 Sep 2003 Posts: 226
|
|
Posted: Mon Feb 07, 2005 12:46 pm |
|
|
Neutone wrote: | If your really accessing the timer variables I suggest you use the built in functions.
GET_TIMER1()
At least compile using the built-in functions and observe the assembly created and emulate it. Reading from a 16-bit counter in bytes has a risk of reading incorrectly during a overflow/underflow of the upper or lower byte. |
This is true when using PIC16 parts.
There has been a change in the PIC18 parts, and CCS has not kept up to date when reading Timer registers.
This works for most PIC18xxxx (check address)
#byte T1LO = 0XFCE
#byte T1HI = 0XFCF
int8 T1L, T1H;
T1L = T1LO; // read low first, that latches the hi byte.
T1H = T1HI;
The data sheet explains why this works.
Quote:
11.5 Timer1 16-Bit Read/Write Mode
Timer1 can be configured for 16-bit reads and writes
(see Figure 11-2). When the RD16 control bit
(T1CON<7>) is set, the address for TMR1H is mapped
to a buffer register for the high byte of Timer1. A read
from TMR1L will load the contents of the high byte of
Timer1 into the Timer1 high byte buffer. 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 is valid,
due to a rollover between reads.
End Quote:
LST file for the above code shows it is a little fatser than Get_Timer1();
.................... T1lo = T1L;
0052: MOVFF FCE,09
.................... T1hi = T1H;
0056: MOVFF FCF,0A
It saves one cycle time and reduces code size.
Get_Timer1(); lst
.................... T1lo = Get_Timer1();
004A: MOVF FCE,W
004C: MOVFF FCF,03
0050: MOVWF 09
LST for reading PIC16 timers is horrible... ! |
|
|
|