PDA

View Full Version : PIC16F887 cant handle HIGH and LOW?



xobx
- 14th October 2007, 16:45
If I make my code like this my three leds blinks..


loop:

PORTB = %00000111
pause 250
PORTB = %00000000
pause 250

goto loop

END


BUT if I do it likes this does only PORTB.0 blink..
Why cant I do it like this?


loop:

HIGH PORTB.0
HIGH PORTB.1
HIGH PORTB.2
PAUSE 250
LOW PORTB.0
LOW PORTB.1
LOW PORTB.2
PAUSE 250

goto loop

END

GrandPa
- 14th October 2007, 17:27
Try adding TRISB=%00000000 to set all PORTB pins as outputs.
You can also simply write TRISB = 0.
(This goes on the beginning of your program.)


J-P

Bruce
- 14th October 2007, 17:33
What you're seeing with the 2nd version is read-modify-write. The HIGH/LOW commands
use BSF/BCF to toggle port bits. Doing this too quickly, one after another, causes the
read-modify-write problem. This is in most PIC data sheets and the midrange ref manual.

Try this. If it works as expected, then you're 100% sure the problem is read-modify-write.
The only difference here is you're allowing each port bit time to change before the next
read-modify-write operation.

loop:

HIGH PORTB.0
PAUSEUS 25
HIGH PORTB.1
PAUSEUS 25
HIGH PORTB.2
PAUSE 250
LOW PORTB.0
PAUSEUS 25
LOW PORTB.1
PAUSEUS 25
LOW PORTB.2
PAUSE 250

goto loop

END

xobx
- 14th October 2007, 20:35
What you're seeing with the 2nd version is read-modify-write. The HIGH/LOW commands
use BSF/BCF to toggle port bits. Doing this too quickly, one after another, causes the
read-modify-write problem. This is in most PIC data sheets and the midrange ref manual.

Try this. If it works as expected, then you're 100% sure the problem is read-modify-write.
The only difference here is you're allowing each port bit time to change before the next
read-modify-write operation.

loop:

HIGH PORTB.0
PAUSEUS 25
HIGH PORTB.1
PAUSEUS 25
HIGH PORTB.2
PAUSE 250
LOW PORTB.0
PAUSEUS 25
LOW PORTB.1
PAUSEUS 25
LOW PORTB.2
PAUSE 250

goto loop

END

That didnt work, quess I haft to stick with PORTB = %...

..but whats the difference between 'PAUSE' and 'PAUSEUS'?

nomad
- 14th October 2007, 22:36
pause is in milli seconds, pauseus is in micro seconds.

Bruce
- 15th October 2007, 00:08
Oops..!

Looks like this one has A/D on PORTB, so you'll also want to disable that. ANSELH=0 should
do the trick.

xobx
- 15th October 2007, 15:59
Oops..!

Looks like this one has A/D on PORTB, so you'll also want to disable that. ANSELH=0 should
do the trick.

Thanks, now does this code work too!



loop:

HIGH PORTB.0
HIGH PORTB.1
HIGH PORTB.2
PAUSE 250
LOW PORTB.0
LOW PORTB.1
LOW PORTB.2
PAUSE 250

goto loop

END

Bruce
- 15th October 2007, 16:09
Most of the time this approach works fine, but it can cause read-modify-write problems
depending on the oscillator speed, and external capacitance present on each pin.

RodSTAR
- 16th October 2007, 04:46
That didnt work, quess I haft to stick with PORTB = %...

..but whats the difference between 'PAUSE' and 'PAUSEUS'?

ensure you pins are connected to ground and not to +5V (and don't forget resistors)