PDA

View Full Version : Reading Input on Port B



bodgetts
- 30th July 2011, 05:53
Hi guys,

I can read the input on any individual pin on portb like this:

if (portb.2 = 0) Then do something

However, if I have successive reads such as this it doesn't work properly:

if (portb.2 = 0) Then do something1
if (portb.3 = 0) Then do something2
if (portb.4 = 0) Then do something3

Do I need to do something with the hardware in-between reading the pins? I am using a 16f887 40-pin PIC.

Thanks

mister_e
- 30th July 2011, 08:50
http://forums.basicmicro.net/download/file.php?id=115&t=1
The pinout usually give some great hints... AN like ANALOG.. so you want to disable the analog features of the offending pins...and have a look at this thread
http://www.picbasic.co.uk/forum/showthread.php?t=561

bodgetts
- 30th July 2011, 18:14
I am able to read each pin separately without any problems.

This works:
if (portb.2 = 0) Then do something

So does this:
if (portb.3 = 0) Then do something

So does this:
if (portb.4 = 0) Then do something



However, if I check all three immediately after one another, I am getting funny results. Do I need to let portb 'rest' in-between reads, etc? Is there something I am missing?

I have set the registers as per the datasheet for the 16f887:

CM1CON0=%00000000 'disable COMPARATOR 1
CM2CON0=%00000000 'disable COMPARATOR 2
trisb = %11111111 'set all pins to input

Any ideas?

cncmachineguy
- 30th July 2011, 20:39
Maybe the victim of R-M-W? I am guessing the do something part may be setting output pins high? If they are on the same port, maybe this is the problem. I like to grab the input port and put it in a variable. Then I can check the bits in the variable and do something -or not.

Just suggestions, I am not sure of the problem.

Bruce
- 30th July 2011, 23:17
Maybe the victim of R-M-W?
Nope.

Pins that power-up as analog inputs, when read (assuming it's a digital input), will read ZERO unless you have disabled the analog peripheral on the pin you're reading.

TIP: Look at ANSELH.

I'll bet, if you set all of these pins to logic 1 (externaly), they still all read zero until you disable the analog periperal!

bodgetts
- 30th July 2011, 23:47
I have set:

ANSELH=%00000000

If I check for say a portb.3 button press, it works fine. If I check for a portb.5 button press it works fine.

However, if I check for say portb.3 and then portb.5 straight away, sometimes portb.5 comes back as 1 instead of 0, etc.

Bruce
- 31st July 2011, 00:02
Post your code. It's tough to help troubleshoot what we can't see..;)

mister_e
- 31st July 2011, 01:01
But, in 2011, everything's possible ;)
http://image.shutterstock.com/display_pic_with_logo/10574/10574,1245252169,1/stock-photo-ghost-hand-coming-through-lcd-screen-isolated-on-white-32199052.jpg