Toggle led and turn off another


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Apr 2011
    Location
    Welches, Oregon
    Posts
    198


    Did you find this post helpful? Yes | No

    Default Re: Toggle led and turn off another

    Ok, I think I get it. Doesn't help me to explain it, but I get it.

    I do not see anything in the code snippets to explain this so, I think it must be a hardware anomaly. Darrel once mentioned to me that output pins are first read before set and that setting individual pins(PORTA.1=1 : PORTA.2=1 : ...) is less desirable than the style of PORTA = %00000110, or that a brief PAUSE between may be necessary between statements to allow for this. I don't claim that this is happening in your code, only that their are some issues and limitations beyond the obvious. Sadly, I am not advanced enough in knowledge or equipment to troubleshoot at that level... Perhaps someone with more of one or the other will happen by.

    Wish I'd been more help...

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,617


    Did you find this post helpful? Yes | No

    Default Re: Toggle led and turn off another

    That is called Read-Modify-Write or RMW for short. It was my first thought when I read the thread earlier but since a PAUSE between the two statements did't help it sounded less likely - not that I know what else it would be.....

    On a PIC ALL "bit flipping" are performed as a Read-Modify-Write operation - it's how the hardware works internally.
    So, when you do PortB.0 = 1 the PortB register is read to a working register in the PIC (Read), bit 0 is set (Modify), the working register is written back to PortB (Write). This has nothing to do with PBP it's how the hardware works and it's basically what's happening during the 4 internal cycles of the oscillator which makes up for a full instruction cycle.

    Now lets look at what CAN happen with something like
    Code:
    PortB.0 = 1
    PortB.1 = 0
    The first operation, by it's own, works as expected. But when the second line was added the first one stopped working. What's happening is that whatever is connected to PortB.0 prevents the voltage at the pin to rise quick enough so by the time the second operation performs the Read-part of the RMW PortB.0 is still at a logig LOW level so that's what is being read. When the Write-part of the RMW is performed a logic LOW will then be written to PortB.0.

    Inserting a PAUSE between the two lines allows the voltage to rise to level where the second operation works properly.

    If the device in question has LAT-register then using those instead totaly circumvents the issue. TOGGLE however won't work on LAT as far as I know.

    Now, TOGGLE pwr_lowled is a high level commad, it takes longer time to execute than pwr_led = 0 which would explain why it matters in which order you put them. But again, a PAUSE in between the two instruction should have "fixed" it - which it supposedly didn't.

    Make sure you're not trying to pull more current thru the pin that it's rated for. Always use current limiting resistors when driving leds - always. Not doing it CAN work but it's just BAD practice - really bad practice.

    /Henrik.

Similar Threads

  1. TOGGLE will not go LOW
    By SUNFLOWER in forum mel PIC BASIC Pro
    Replies: 17
    Last Post: - 10th August 2014, 16:53
  2. Problem getting button to turn on LED using 12F675
    By Jeff in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th June 2011, 08:14
  3. Replies: 1
    Last Post: - 14th October 2010, 18:33
  4. Replies: 10
    Last Post: - 30th November 2009, 15:58
  5. turn-off lcd and turn-on again
    By mehmetOzdemir in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 5th September 2009, 12:57

Members who have read this thread : 0

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