P16F887 PortB internal pull-ups

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

P16F887 PortB internal pull-ups

Post by fotios »

Internal pull-ups is a great feature of micros, when are enabled eliminate the need of external pull-up resistors when a Port is used as input. Especially in P16F887 internal pull-ups can be individually enabled per each pin of PortB. That is usefull when both "Port" (or IOC) and "INT0" interrupts are used. Let me explain a case:
An IR receiver is connected at Port RB.0 and the INT0 is enabled, so the internal pull-up of this port should be disabled since the IR receiver output is HI in iddle state and when a signal is received it drops to LO state causing the corresponding INT0 interrupt. From RB.3 to RB.7 pins, 5 switches are connected in "Active LO" mode and the "Port Interrupt" is also enabled. When a switch is pressed, applies a logic "0" to the corresponding input and thus the Port interrupt is activated. In old micros, there was the need of external pull-up resistors connected at inputs in conjunction with switches while in new micros this ensured by internal pull-up circuits connected at each pin. Mid familly AVRs like ATmega16 have internal pull-ups in all Port pins individually selectable, while PIC16 familly only in PortB pins (i don't know for higher PIC famillies). The internal weak pull-ups of PICs, are managed by the bit7 of OPTION register which of the name is RBPU. This bit is automatically set to "1" during any reset of PIC, so we have to clear it at the beginning of program. In small PICs like P16F88 when the bit RBPU is cleared activates all internal pull-ups of PortB, while P16F887 includes an extra register named WPUB allowing us to enable or disable the weak pull-up of any pin we want. That is usefull in my case for the reason i described above. After any reset, all bits of WPUB register are automatically set to "1" like the bit RBPU. While the WPUB register is configured separatelly, to take effect (i.e. to enable pull-ups) the RBPU bit should be cleared first. That is obvious ONLY in the schematic diagram of a PortB pin, the description in P16F887 datasheet regarding initialization condition of registers is some messy. Each pin is pulled in HI state by a FET which of the gate is driven by a NAND gate. One of the inputs of this NAND is driven by the RBPU bit of OPTION register. I tried this configuration with success - from a first view - in my project, but i have queries and i am not sure if the C command that i use is correct.
For first, i tried this:
1 option_reg.RBPU = 0;
2 wpub = 0xF8;
but during compiling FlowCode told me that RBPU is unknown.
Then, i tried this:
1 option_reg = 0x7F; (7Fh = 0111 1111b)
2 wpub = 0xF8; (F8h = 1111 1000b).
This time the code compiled by FlowCode with success without error messages and the project it seems to work correctly regarding internal pull-ups.
My questions are:
1) Why RBPU is not recognized like other bits of OPTION register e.g. T0SE, T0CS?
2) For clearing the bit RBPU is correct the "direct" command option_reg = 0x7F; or is better an "indirect" command like option_reg = option_reg & 0x7F;?
Thanks
Fotis
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

Hi Fotis,

1) Not too sure, will see if I can find out.

2) You can clear and set bits directly
I use

Code: Select all

clear_bit(option_reg , 7);
to enable weak pull-ups
Since there is a direct clear_bit and set_bit commands you can use.

Martin
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

Re: P16F887 PortB internal pull-ups

Post by fotios »

Good morning Martin
1) WPUB is individual register.
2) I know this command "clear_bit (option_reg , 7);" i remember that you are using it on "rotary encoder thread" to enable internal pull-ups. Just from curiosity i asked if command "option_reg = 0x7F;" can do the same i.e. to clear the bit RBPU without affecting any other bit of OPTION register. From first view it seems correct, but i don't know if it can cause side effects since is executed after each reset. For this reason i also asked for the indirect command "option_reg = option_reg & 0x7F".
3) Why OPTION register RBPU bit is not recognized by FlowCode like T0SE or T0CS?
4) Do you think that there is issue of excess current when pull-up is enabled (i measured +4.9V on input pin) and the input is shorted to Gnd thru an "active LOW" switch? Is there any internal current limiting resistor? In PortB schematics i haven't saw a such resistor and the max source - sink current is given at 25mA per each pin in P16F887 datasheet "electrical specs". In AVR ATmega16 schematics is shown an internal current limiting resistor in series with the pull-up Fet drain.
Thanks for the reply
Fotis
Last edited by fotios on Wed Aug 24, 2011 9:59 am, edited 3 times in total.
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

Sorry Fotis, not woke up properly this morning I know now why option_reg.RBPU = 0; does not work.
Just had a look in the TDF folder (boostc/config directory), and the weak pull-up bit is labelled NOT_RBPU
So if you use

Code: Select all

option_reg.NOT_RBPU = 0;
That should work

Martin
Last edited by medelec35 on Wed Aug 24, 2011 10:00 am, edited 2 times in total.
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: P16F887 PortB internal pull-ups

Post by Benj »

Hello,

The command

option_reg = 0x7F;

Would not do the same really, it would clear bit 7 but it would also set all the other bits in the register.

If you wish to do the same command without using the clear_bit() function then you would do it like this.

option_reg = option_reg & 0x7F;

This way only bit 7 of the register gets modified.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

Just a comment.
I know why NOT_RBPU is used since it is literally that.
It may have been better for BoostC to leave the NOT out to avoid confusion perhaps?

Martin
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

Re: P16F887 PortB internal pull-ups

Post by fotios »

Benj wrote:Hello,

The command

option_reg = 0x7F;

Would not do the same really, it would clear bit 7 but it would also set all the other bits in the register.

If you wish to do the same command without using the clear_bit() function then you would do it like this.

option_reg = option_reg & 0x7F;
Welcome back Ben, we missed you, really :D

My command "option_reg = 0x7F;" was based on that, after ANY reset the OPTION register is automatically set to "FFh" (or 1111 1111b).

Do you think that there is issue of excess current when pull-up is enabled (i measured +4.9V on input pin) and the input is shorted to Gnd thru an "active LOW" switch? Is there any internal current limiting resistor? In PortB schematics i haven't saw a such resistor and the max source - sink current is given at 25mA per each pin in P16F887 datasheet "electrical specs". In AVR ATmega16 schematics is shown an internal current limiting resistor in series with the pull-up Fet drain.

Thanks
Fotis
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

fotios wrote: 4) Do you think that there is issue of excess current when pull-up is enabled (i measured +4.9V on input pin) and the input is shorted to Gnd thru an "active LOW" switch? Is there any internal current limiting resistor? In PortB schematics i haven't saw a such resistor and the max source - sink current is given at 25mA per each pin in P16F887 datasheet "electrical specs". In AVR ATmega16 schematics is shown an internal current limiting resistor in series with the pull-up Fet drain.
Thanks for the reply
Fotis
In my opion The pull-ups should be sufficanly weak to allow shorting directly to GND. I have done this many times. After all by definition weak should be weak!

A suggestion. Short the weak pull-up i/p to GND with multimeter set to current range. Then you could calculate the equivalent value of internal weak pull-up resistor?

Since there will be 5V across it, using R = V/I

I'm gradually going though all the questions as I wake up more :P

Also Welcome back Ben from me as well!
Hope you had a good Holiday?

Martin
Martin

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

Re: P16F887 PortB internal pull-ups

Post by fotios »

medelec35 wrote:Just a comment.
I know why NOT_RBPU is used since it is literally that.
It may have been better for BoostC to leave the NOT out to avoid confusion perhaps?

Martin
Hi Martin
I hope your bad awaking was not from health problem :( . It was just from some extra beers yesterday the night. :mrgreen:
Is that you refer (NOT_RBPU) related with the "invert line" above RBPU term in Microchip nomenclature?
Wish you quick recovery
Fotis
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

fotios wrote:Hi Martin
I hope your bad awaking was not from health problem :( . It was just from some extra beers yesterday the night. :mrgreen:
I just some times have trouble sleeping, so waking up part can be a bit awkward. Since I am on hols from work until Tuesday, it's taking a bit longer to wake up, since not franticly rushing around trying not to be late for work :lol:
fotios wrote:Is that you refer (NOT_RBPU) related with the "invert line" above RBPU term in Microchip nomenclature?
Wish you quick recovery
Fotis
Yes, technically is the logic NOT which just means active low instead of active high.
Im OK but thanks any way :)
Re Weak pull-up
The lower the voltage on the i/p pin, then the pull up resistance value connected to it is higher.
Whats happening is when your measuring Voltage your meter is acting as a resistance (20 Meg is standard) from the i/p pin to GND. if the weak pull-up was also 20 MEG then your meter will measure 1/2 the supply value on the i/p pin. Since the weak pull-up is lower then 20 Meg then the voltage is going to be higher than this but because the weak pull-up is not a low resistance, then you will not read 5V. If the 5V rail is lower that's when you need to worry.
I have measured the current from i/p pin to GND and only 213 uA is flowing. Therefore the internal pull-up is 5/213uA = 23.5K
So no excess current is drawn.
Nothing to worry about.
Hope I have explained it OK.

Martin
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: P16F887 PortB internal pull-ups

Post by Benj »

Hello,

Thanks guys, had a great holiday on the English Riviera though weather was not as good as I had hoped.

Same goes for me Martin, cannot fall asleep and cannot wake up in the morning, need to hard wire a switch for my brain I think :D Always been an issue for me but thankfully Matrix took this on board and allowed me fairly flexible hours.

Fotios, I agree with Martin, there should be no problems shorting an input pin with weak pull up to ground. The only thing to be careful of is if the pin ever gets converted to an output and set to output a 1 then shorting this to ground will likely cause issues. A series resistor of say 220 ohms between the pin and the ground connection would guarantee that no damage can occur no matter what happens, this is the technique we use with E-blocks.

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times

Re: P16F887 PortB internal pull-ups

Post by fotios »

OUT OF TOPIC

I am so happy to have both of my best friends (and mentors) of this forum, in my thread exclusivelly! :D for Martin and :D for Ben ... seperately.

With big gratitude
Fotis
Best Regards FOTIS ANAGNOSTOU

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: P16F887 PortB internal pull-ups

Post by medelec35 »

Benj wrote:Hello,

Thanks guys, had a great holiday on the English Rivier
It's aright for some! Glad it was great.
Benj wrote:allowed me fairly flexible hours.
It has been noted :P
fotios wrote:I am so happy to have both of my best friends (and mentors) of this forum, in my thread exclusivelly! :D for Martin and :D for Ben ... seperately.

With big gratitude
Fotis
Your welcome Fotis, Just happy I can actually help someone :)

You have been helping people within forums as well, so just glad your questions are answered quickly

Martin
Martin

Post Reply