PIC16F887 cant handle HIGH and LOW?


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2007
    Posts
    48

    Default PIC16F887 cant handle HIGH and LOW?

    If I make my code like this my three leds blinks..

    Code:
    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?

    Code:
    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

  2. #2
    Join Date
    Jul 2007
    Posts
    53


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    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'?

  5. #5


    Did you find this post helpful? Yes | No

    Default

    pause is in milli seconds, pauseus is in micro seconds.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Oops..!

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

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    Jul 2007
    Posts
    48


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    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!


    Code:
    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

  8. #8
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    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.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  9. #9
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by xobx View Post
    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)

Similar Threads

  1. SERIN MIDI out of Synch?
    By jncortes in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 9th June 2009, 20:08
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. Pic16f84 and RC5 kode
    By terminator in forum Bluetooth
    Replies: 5
    Last Post: - 18th June 2007, 21:40
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. sample code for M25P32
    By Pedro Santos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 9th January 2007, 02:37

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts