Indirect memory access to read ports

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Posts: 10
Joined: Fri Jul 10, 2015 7:52 pm

Indirect memory access to read ports

Post by bernardj » Mon Dec 09, 2019 8:55 am


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.



Jerry Messina
Swordfish Developer
Posts: 1220
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Indirect memory access to read ports

Post by Jerry Messina » Mon Dec 09, 2019 11:08 am

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())

Post Reply