Discussion:
[PIC] PIC16F18854 PORTB Read problem
James Wages
2017-08-23 02:56:24 UTC
Permalink
I've discovered something unusual while programming a PIC16F18854 in Assembly.

I have PORTB set to be all DIGITAL INPUTS. (In the case of the 16F18854, PORTB is RB0 through RB7.) Normally on other PICs, the following code works just fine to read such inputs:

banksel PORTB ; [Bank 0]
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM (0x077, RAM common to all banks)

The problem is that when I use to above code, RB5 (pin 26) is misread. It is misread even when I use this code (repetition, just for a test):

banksel PORTB ; [Bank 0]
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM
nop
nop
nop
nop
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM
nop
nop
nop
nop
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM


But when I manually check the state of every pin in PORTB, nothing is misread and the problem is solved:

banksel PORTB ; [Bank 0]
clrf RAMa
btfsc PORTB, RB0
bsf RAMa, 7
btfsc PORTB, RB1
bsf RAMa, 6
btfsc PORTB, RB2
bsf RAMa, 5
btfsc PORTB, RB3
bsf RAMa, 4
btfsc PORTB, RB4
bsf RAMa, 3
btfsc PORTB, RB5
bsf RAMa, 2
btfsc PORTB, RB6
bsf RAMa, 1
btfsc PORTB, RB7
bsf RAMa, 0


I'm pleased I have a solution, but I don't understand why moving the entire PORT to W and then to RAM causes a misread pin. Do any of you know? (And again, PORTB is setup via TRISB to be all INPUTS, ANSELB is cleared, making the pins DIGITAL.)

Thanks.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
James Cameron
2017-08-23 03:33:21 UTC
Permalink
Is there anything attached to pin 26?

Port B is complicated on the PIC16F18854.

What is INLVLB5 set to?

What is WPUB5 set to?

What is ODCB5 set to?

What is CCDPB5 set to?

What is CCDNB5 set to?

Does changing SLRB5 fix it?
--
James Cameron
http://quozl.netrek.org/
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
James Wages
2017-08-24 05:14:01 UTC
Permalink
TRISB is b'11111111' at Reset, but I also manually write that value into TRISB in my init routine, ensuring without a doubt that all of PORTB is set to INPUTS. Each of those 8 inputs (including pin 26) each have an external 100k-ohm pull-up to 5V. Furthermore, there is an 8-switch DIP connected to each of those 8 INPUTS that when switched ON will short the individual INPUTS to Ground. So the problem is that when I have my DIP SW tied to pin-26 to be ON/Ground, the PIC still thinks pin-26 is OFF/5V. And yes, I verified with a DMM that there are no problems at all with my DIP SW and voltage levels.

INLVLB is b'11111111' at Reset, but I also tested with it manually set to 0, to no avail.

WPUB is set to 0 at Reset, and I also use clrf on that register in my code to ensure internal pullups are Disabled.

ODCONB is set to 0 at Reset, and it shouldn't matter since it pertains to sourcing or sinking current on OUTPUT pins -- all my PORTB pins are defined as INPUTS. In any case, I leave that register at its default of 0.

I searched the entire 16F18554 datasheet in vain for CCDPB5 & CCDNB5, but they do not exist. The only explanation I can imagine as to why you mentioned them is because you were looking at the wrong datasheet. When I Google those two registers I find the "PIC16(L)F18855/75" datasheet, which is WRONG. The PIC I am referring to is the 16F18854. I provide the correct datasheet link for you below.

SLRCONB is set to b'11111111' ("limited") at Reset. I normally leave it that way. But per your request, I used clrf on it in my init routine, but the problem remains -- changing slew rate has no positive impact.

ANSELB is b'11111111' at Reset, but I use clrf on it in my init routine to ensure all pins are DIGITAL I/O. I do NOT have any analog pins.

I don't use LATB because that pertains to Outputs.


With the exception of your mention of "CCDPB5 & CCDNB5", the above are all the registers associated with PORTB, as per TABLE 12-3 given on page 196 of the Revision B datasheet:

http://ww1.microchip.com/downloads/en/DeviceDoc/40001826B.pdf



And although I do have an ISR, it does not make calls to any routine that writes to my RAM (at 0x077). Furthermore, when I do write to that RAM, interrupts are disabled.


Further thoughts would be appreciated. But honestly, I wouldn't be surprised if it was flawed silicon because to date I've found and reported (to Microchip) no less than 15 distinct errors in that silly datasheet. In all my years of using PICs I've not encountered that many errors in a datasheet. I think it reflects badly on Microchip. I am using this particular chip because I need more pins than a PIC16F1508 affords me, and I also need an NCO module, a CWG module, two CLC modules, and a PWM module in a low-priced PIC.

The reason I submitted my query to this list is because even though I've found a work-around (i.e., use "btfsc" on each and every silly PORTB input) I do not understand why "movf PORTB, W" is misreading RB5 (pin 26). That concerns me.

Thank you,

James Wages




Wed, 23 Aug 2017 13:33:21 +1000, James Cameron <***@laptop.org>:

Is there anything attached to pin 26?

Port B is complicated on the PIC16F18854.

What is INLVLB5 set to?

What is WPUB5 set to?

What is ODCB5 set to?

What is CCDPB5 set to?

What is CCDNB5 set to?

Does changing SLRB5 fix it?
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
a***@stfc.ac.uk
2017-08-24 08:17:22 UTC
Permalink
There have been chips in the past where there was some strange interaction with between port A and Port B that meant some bits of the a port (I can't remember which one) were not usable in some situations.

I think you are at the 'contact the FAE, here are the symptoms, what is the problem' point.
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
s***@agilent.com
2017-08-30 00:03:29 UTC
Permalink
Hi James -

When you say it is misread, do you mean that it is always HI or always LO?
Also, are you sure about your individual tests? It seems you are reversing the bit order- RB0 => RAMa,7 etc... Hence RB5 will be in RAMa,2, not RAMa,5 as might be expected.

Stephen

-----Original Message-----
From: piclist-***@mit.edu [mailto:piclist-***@mit.edu] On Behalf Of James Wages
Sent: Wednesday, 23 August 2017 12:56 PM
To: ***@mit.edu
Subject: [PIC] PIC16F18854 PORTB Read problem

I've discovered something unusual while programming a PIC16F18854 in Assembly.

I have PORTB set to be all DIGITAL INPUTS. (In the case of the 16F18854, PORTB is RB0 through RB7.) Normally on other PICs, the following code works just fine to read such inputs:

banksel PORTB ; [Bank 0]
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM (0x077, RAM common to all banks)

The problem is that when I use to above code, RB5 (pin 26) is misread. It is misread even when I use this code (repetition, just for a test):

banksel PORTB ; [Bank 0]
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM
nop
nop
nop
nop
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM
nop
nop
nop
nop
movf PORTB, W ; Move state of PORTB input pins to W register.
movwf RAMa ; Save W in RAM


But when I manually check the state of every pin in PORTB, nothing is misread and the problem is solved:

banksel PORTB ; [Bank 0]
clrf RAMa
btfsc PORTB, RB0
bsf RAMa, 7
btfsc PORTB, RB1
bsf RAMa, 6
btfsc PORTB, RB2
bsf RAMa, 5
btfsc PORTB, RB3
bsf RAMa, 4
btfsc PORTB, RB4
bsf RAMa, 3
btfsc PORTB, RB5
bsf RAMa, 2
btfsc PORTB, RB6
bsf RAMa, 1
btfsc PORTB, RB7
bsf RAMa, 0


I'm pleased I have a solution, but I don't understand why moving the entire PORT to W and then to RAM causes a misread pin. Do any of you know? (And again, PORTB is setup via TRISB to be all INPUTS, ANSELB is cleared, making the pins DIGITAL.)

Thanks.


--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
James Wages
2017-09-01 08:20:25 UTC
Permalink
Stephen,

I resolved it in the Microchip forum here:

http://www.microchip.com/forums/m/tm.aspx?m=1011143

James Wages
Post by s***@agilent.com
Hi James -
When you say it is misread, do you mean that it is always HI or always LO?
Also, are you sure about your individual tests? It seems you are reversing the bit order- RB0 => RAMa,7 etc... Hence RB5 will be in RAMa,2, not RAMa,5 as might be expected.
Stephen
-----
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
s***@agilent.com
2017-09-03 22:54:56 UTC
Permalink
Hi James - I am pleased you have a working solution . It would be good to really understand what happened in the first versions though.

Stephen


-----Original Message-----
From: piclist-***@mit.edu [mailto:piclist-***@mit.edu] On Behalf Of James Wages
Sent: Friday, 1 September 2017 6:20 PM
To: ***@mit.edu
Subject: RE: [PIC] PIC16F18854 PORTB Read problem

Stephen,

I resolved it in the Microchip forum here:

http://www.microchip.com/forums/m/tm.aspx?m=1011143

James Wages
Post by s***@agilent.com
Hi James -
When you say it is misread, do you mean that it is always HI or always LO?
Also, are you sure about your individual tests? It seems you are reversing the bit order- RB0 => RAMa,7 etc... Hence RB5 will be in RAMa,2, not RAMa,5 as might be expected.
Stephen
-----
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Loading...