PDA

View Full Version : Help on PORTB



aarothepharo
- 11th January 2013, 21:01
I made this short 2min video with my question. If anybody can answer it, I will greatly appreciate it.

http://www.youtube.com/watch?v=XpWZ8o8bhP4

pedja089
- 11th January 2013, 21:10
RMW problem...
Add resistor to leds, it might help.

HenrikOlsson
- 11th January 2013, 22:06
Hi,
Most likely read-modify-write issue as peja089 says.

If you remove the LEDs and measure the voltage on the pins you'll see that they all goes "high" and close to 5V (if that's your powersupply voltage). If you put the LEDs back you'll get the phenomenon you see and if measure the voltage you'll see that it's no longer 5V due the LED clamping the voltage.

A LED operate on current and produce a certain voltage drop across it. You must put a resistor in series with the LED to "set" the correct current. Simply wiring the LED to the PIC output pin like that does indeed light up the LED but the output voltage of the PIC will be clamped by the forward voltage drop of the LED which, depending on the color might be somewhere between 1.8 and 2.1V.

Now, for an input to be concidered "high" the voltage at the pin must be atleast 2V when the power supply voltage is 5V. Why does it matter, you're not using any inputs?
It matters because when you do HIGH PortB.2 the PIC reads all the bits in PortB into an internal register, it then sets bit 2 of that register "high" and finally writes the register back out to PortB (Read-Modify-Write). Because the voltage of the output pin is being clamped by the forward voltage drop of the LED the PIC reads that pin as low thus setting the output low when it writes back the internal working register to the port.

So, it's a hardware problem, not a software bug.

/Henrik.

aarothepharo
- 11th January 2013, 22:15
Added resistors and its still doing it.

HenrikOlsson
- 11th January 2013, 22:30
Great, now the hardware is sorted.
On this chip there are analog functions (the ADC and comparators) on PortB, these are turned on by default.
When reading a pin that is configured as analog it will be read as low resulating in the same behaviour as before. To enable the digital functionality on all the pins add the following at the beginning of your program:


ANSEL = 0
ANSELH = 0

/Henrik.

aarothepharo
- 15th January 2013, 19:42
Yes that fixed it.. thanks