Special PIC18 Register Issues

Some of the SFRs used by PIC18 devices can be grouped to form multi-byte values, e.g., the TMRxH and TMRxL register combine to form a 16-bit timer count value. Depending on the device and mode of operation, there can be hardware requirements to read these registers in certain ways, e.g., often the TMRxL register must be read before trying to read the TMRxH register to obtain a valid 16-bit result.

It is not recommended that you read a multi-byte variable mapped over these registers as there is no guarantee of the order in which the bytes will be read. It is recommended that each byte of the SFR should be accessed directly, and in the required order, as dictated by the device data sheet. This results in a much higher degree of portability.

The following code copies the two timer registers into a C unsigned variable count for subsequent use.

count = TMR0L;
count += TMR0H << 8;

Macros are also provided to perform reading and writing of the more common timer registers. See the macros READTIMERx and WRITETIMERx in Library Functions. These guarantee the correct byte order is used.