Page 1 of 1

Indirect memory access to read ports

Posted: Mon Dec 09, 2019 8:55 am
by bernardj
Hi

Some assistance pls:
For the first time I have tried to obtain the value of registers PORTA and PORTB using indirect memory access, but I do not get the values back which I expect.

I am using an 18F27K40.

Code: Select all

  Dim portNumber, res as byte

  portNumber = 1
  FSR2 = $0F8D + portNumber        '$0F8D is the register address of PORTA (as per my understanding of the datasheet), 
                                                   'add one for PORTB
  res = INDF2
The value obtained in variable res is always 0.

Does the value in the register of PORTA or PORTB actually update only when a read is done on that port directly, such as i = PORTA?
I am missing something?
Any suggestions, please?

Using the FSR approach to update ports LATA and LATB work as expected.

Regards

Bernard

Re: Indirect memory access to read ports

Posted: Mon Dec 09, 2019 11:08 am
by Jerry Messina
You can read the PORTx registers indirectly using FSR, but you must:
- make sure the port is set for input (TRISx)
- set the port analog/digital mode to digital (ANSELx)

Ports with analog functions default to analog mode and will read '0'.
Disable the analog functions (or use SetAllDigital())