problem with toggle command


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2016
    Posts
    3

    Default problem with toggle command

    having problem with the toggle command; (using PIC18f13k22)
    this works;
    Code:
    LOOP1:                    
        PORTA.1 = 1     
        pause 100
        PORTA.1 = 0
        pause 100
        goto LOOP1
    now, using the toggle command, this does not work;
    Code:
    LOOP1:                    
        PORTA.1 = 1     
        pause 100
        TOGGLE PORTA.1
        pause 100
        goto LOOP1
    what am I missing?

  2. #2
    Join Date
    Feb 2011
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    Something I ran into also. Has something to do with reading state of pin for toggle, a problem if pin is set to analog, I think. Anyway, a simple reliable method to toggle a pin is to add 1 to the pin value -- portA.1=portA.1 + 1

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    There must be hundreds of posts about this across the forum by now.
    Turn off any analog functions (comparator, ADC) that's on the pin(s) you're going to use as digital.

    When an analog pin is read, as in myBit=PortA.1 it will always return the same value no matter what the logic state actually is. Toggle then flips the bit and writes it back to the pin. What happens next time TOGGLE executes?

    On devices whit LAT registers you're better off using them instead of PORT but I don't Think Toggle works with them.

    /Henrik.

  4. #4
    Join Date
    Sep 2016
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    Thanks for the answers, that was it. The problem was that the ANSEL register was set to disable the digital input buffer, which caused all reads of that pin to return '0'. What threw me off was that I was thinking of the toggle command is an output function, & since the state of the ANSEL register has no affect on digital output functions, I thought I was OK with that setting. But toggle is really both an output & an input function in that it has to read the state of the pin to be able to change it to the opposite state.

    Actually, after considering what SUNFLOWER said, it's not obvious that the toggle command necessarily has to have an input (read) function to it, but evidently it does work that way.
    Last edited by INVTLG; - 3rd September 2016 at 18:11.

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    How else could it work? If it's supposed to toggle a bit it needs to know what state that bit is currently in, right?

  6. #6
    Join Date
    Sep 2016
    Posts
    3


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    Henrik,
    Well, I guess I don't have to know If I just add 1 to whatever is there, so why should it have to know? - just kidding
    From your original answer, I see that you obviously understand the inner workings of these microcontrollers & what the compiler is doing behind the scenes. I do not, & my last comment about the 2nd part of SUNFLOWER's answer is just this very thing; before I read your answer, I might think that somehow it could just add 1 to whatever is there.
    Last edited by INVTLG; - 4th September 2016 at 01:24.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: problem with toggle command

    Exactly, when you think about it for a bit you'll realise that even PortA.1 = PortA.1 + 1 won't work because of the exact same reason and in this case it's even more "obvious" than with the TOGGLE command because here you have it spelled out. You're telling the compiler to read the state of PortA.1, add 1 to it and write it back. If, when Reading PortA.1 it always reads high because it's got its digital buffer disabled then it will work just as bad as TOGGLE will.

    On top of that you should know that ALL bit operations are executed as Read-Modify-Write by the internal PIC hardware, has nothing to do with the compiler. So, if you're using some pins on PortA as digital outputs but you don't bother to enable the digital buffers and then you do something simple like PortA.0 = 1 what will happen is that the PIC (ie the actual processor) will read all 8 bits from PortA, set bit 0 and write all 8 bits back to the Port register and the state of "the other" pins might change "unexpectedly".

    So, whenever you get "weird" results on any I/O pins, even if you're not actually writing to that specific pin you should always think about the analog peripherals.

    And again, on the device with LAT register you should use those instead of port since they're pretty much unaffected by RMW issues, no matter the actual cause.

    /Henrik.
    Last edited by HenrikOlsson; - 4th September 2016 at 07:49.

Similar Threads

  1. Toggle command question
    By AvionicsMaster1 in forum General
    Replies: 3
    Last Post: - 31st January 2012, 15:34
  2. Toggle command
    By mr.sneezy in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 16th December 2011, 02:07
  3. problem with I2CWRITE command
    By hatzukitzuki in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th October 2010, 22:35
  4. toggle command w/ multiple switches
    By earltyso in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 15th February 2007, 17:47
  5. PIC16F88 problem with TOGGLE command?
    By russman613 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th September 2006, 23:31

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