PIC18 input difficulties

Thread Starter

Peaches41

Joined Dec 15, 2016
70
Hello all,
I am running into a issue which I can not seem to solve. In migration from PIC16K690 to PIC18F26K22 I am not able to have my inputs function correctly(meaning I have no input functionality at all). Lets say for example I am using PORTA RA1 as my pin here. Now I have set my ansela to "clrf ansela" and have turned off my comparators in cm1con0 and cm2con0. I have set my trisa to 1 for RA1. When tested this pin works as an output when set up as such, so I know the pin is operable. There are no other peripherals on this pin as per the data sheet. I then had written a short piece of code:
btfss porta, ra1
goto $-2 ;for pic18 move back one line
btg latb, rb5 ;I am using this pin as my output to test.
goto $

I have a switch connected to RA1 and checked to make sure I met the voltage requirements for the pin to go high.

I can not figure out why this is not working. Please note this is the first time that I have used inputs on this new to me chip. I have never had problems getting an input to function properly.

Could anyone help me to see what I could have missed? This seems pretty basic but I am embarrassed that I cant get this to work.

Thank you in advance for you suggestions.

Peaches
 

Thread Starter

Peaches41

Joined Dec 15, 2016
70
I believe that I may have stumbled upon the reason why I can not get the inputs to function. On page 75 of the data sheet in Note 1 it states that:
Addresses F38h through F5Fh are
also used by SFRs, but are not
part of the Access RAM. Users
must always use the complete
address or load the proper BSR
value to access these registers.

So ANSELA is not in the access bank and I do believe this is why the inputs will not function. I will change accordingly my program. Can anyone verify this?

Peaches.
 

nsaspook

Joined Aug 27, 2009
13,315
Thank you for your response. I am confused as to what I should be looking at here as I am using RA1 as my input pin. Could you guide me a little closer to what I should be looking at?

Peaches
On my sheet that page shows how to setup PORTA bits as input ports using asm.
 

JohnInTX

Joined Jun 26, 2012
4,787
ANSELx is indeed not part of ACCESS RAM in this one. (sorry I missed that in the other thread..) That means that you have to set BSR to bank15 to get to it as well as all of the other SFRs in the last column of table 5-1 (Special Function Register Map). You can also use an FSR to point to these without setting the banks.
From the datasheet:
Code:
MOVLB 0xF ; Set BSR for banked SFRs
CLRF PORTA ; Initialize PORTA by
; clearing output
; data latches
CLRF LATA ; Alternate method
; to clear output
; data latches
MOVLW E0h ; Configure I/O
MOVWF ANSELA ; for digital inputs
MOVLW 0CFh ; Value used to
; initialize data
; direction
MOVWF TRISA ; Set RA<3:0> as inputs
; RA<5:4> as outputs
Note that the value shown for ANSELA is incorrect, it should be 00h. It also won't assemble as written (E0h is not a valid number, 0E0h is..)

To do it with an FSR and avoid messing with banks:
Code:
lfsr 0,ANSELA ; FSR0points to ANSELA
clrf POSTINC0 ; clear ANSELA, increment FSR0 to point to ANSELB
clrf POSTINC0 ; clear ANSELB, increment FSR0
clrf POSTINC0 ; clear ANSELC .. and so on
The IO ports and TRIS registers are in ACCESS RAM so actual reading and writing them is not bank-sensitive.

Good luck.
 
Last edited:
Top