PDA

View Full Version : Using portb as inputs PIC goes crazy



peterdeco1
- 14th November 2005, 10:08
Hello Everyone. For the first time I tried using all 8 portb's as inputs to switches. The are all held low with a 47K resistor network. The switches all have a common +. When I attempt to use them as inputs, my PIC goes crazy. It activates when no buttons are pressed, and activates intermittently when buttons are pressed. When I duplicated everything and switched porta with portb, using porta as inputs and portb as the outputs as shown below, everything works perfectly. Any suggestions would be appreciated. Thank you.

OSCCON = $60 'set int osc to 4mhz using PIC16LF818 at 3VDC
adcon1 = 7 ' set inputs to digital
@ DEVICE MCLR_OFF, INTRC_OSC, WDT_ON, LVP_OFF, BOD_OFF, PWRT_ON, PROTECT_ON
TRISA = %11111111 'PORTA INPUT SWITCHES (THIS WORKS PERFECTLY)
TRISB = %00000001 'ALL PORTB OUTPUTS EXCEPT RB0 (NEED HIGH IMPEDANCE STATE)
PORTB = %00000000 'PORTB OUTPUTS LOW
Pause 100 'SETTLE DOWN

START:
NAP 0
IF PORTA.1 = 1 Then OFF1
IF PORTA.0 = 1 Then ON1
IF PORTA.7 = 1 Then OFF2
IF PORTA.6 = 1 Then ON2
IF PORTA.2 = 1 Then OFF3
IF PORTA.3 = 1 Then ON3
IF PORTA.4 = 1 Then OFF4
IF PORTA.5 = 1 Then ON4
GoTo START

dmairspotter
- 14th November 2005, 14:15
Not sure I understand your circuit. One end of 47k resistor at GND, other end at Portb.x. One end of switch to portb.x, other end to +5. If this is it, it would seem that you could get floating voltage at portb pin. I believe a more common circuit would be resistor between +5 and portb as pullup, switch between portb and ground.

I use portb for inputs on 16F876A with no problems.

Others with more expertise feel free to correct me.