Jump to content
Sign in to follow this  

Weak Pull-up Portb Problems

Recommended Posts

I have been trying to turn on the weak pull-up feature of portb on a pic16f882 but can't seem to get it to work. I have been working my way through the options learning about PIC microcontrollers but I'm no expert...


Here is some sample code I have been trying to make work:


#include <system.h>
#include <stdlib.h>

#pragma CLOCK_FREQ 8000000

#pragma DATA _CONFIG2, _WRT_OFF & _BOR40V

void main() {

  	char porta_buf, portb_buf = 0;

//set outputs
porta = trisa = 0;

// set inputs (jumpers)
set_bit(portb_buf, 2);
set_bit(portb_buf, 3);
set_bit(portb_buf, 4);
set_bit(portb_buf, 5);

portb = trisb = portb_buf;

// enable pull up on portb inputs
option_reg.7 = 0;
wpub.2 = 1;
wpub.3 = 1;
wpub.4 = 1;
wpub.5 = 1;

// startup delay

// main program loop
while (1) {
	porta_buf = 0;

	if (!portb.2) {
		set_bit(porta_buf, 5);
	if (!portb.3) {
		set_bit(porta_buf, 4);
	if (!portb.4) {
		set_bit(porta_buf, 3);
	if (!portb.5) {
		set_bit(porta_buf, 2);

	// set pins
	porta = porta_buf;



What I'm observing is that the input pins on portb are always low. When I break out the voltmeter and check the pins, there isn't any voltage on them.

Maybe I misunderstood the purpose of enabling the pull-up on the input pins. I thought I could turn it on and thus, avoid adding pull-up resistors in hardware?


Or, I have made an error in my above code. Either is a likely possibility...


Anybody see my mistake?

Share this post

Link to post
Share on other sites

The pins RB<0:5> are set as analog inputs by default and - as datasheet clearly states - "setting a pin to an analog input automatically disables the digital input circuitry, weak pull-ups, and interrupt-on-change if available".


So, you need to add the line anselh = 0; at the begin of your program; this disables analog features and enables all the pins for digital I/O.


Hope it helps.

Share this post

Link to post
Share on other sites

That is definitely what I was missing. I guess I missed the part where it said that defaulted to on.


Thank you for pointing out my mistake!

Edited by nrheckman

Share this post

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...