Setting latx instead of portx before trisx


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2014
    Posts
    12

    Default Setting latx instead of portx before trisx

    Hi.
    To initialize my ports I have always done something like this:
    'set all pins on portb high
    portb = %11111111
    'set all pins on portb to outputs
    trisb = %11111111

    A couple of days ago I just realised that i had to use latx.y = z instead of portx.y = z to set some pins high on the pic I was using.
    I have seen some code here on the forum that initialise the ports like I do above and then set their outputs with latx.y = z.

    Should not pics with output latches be initialized like this instead?
    'set all pins on portb high
    latb = %11111111
    'set all pins on portb to outputs
    trisb = %11111111

    Regards
    /Matias

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


    Did you find this post helpful? Yes | No

    Default Re: Setting latx instead of portx before trisx

    Hi,
    Both methods work equally well - on paper. In practice though, using LATx can be better.
    If you discovered that you "had to use" LATx instead of PORTx then you most likely have something misconfigured (analog inputs not disabled) or you're trying to several consecuitive bit operations on the same port with capacitive loads on the pins and high oscillator frequency resulting in read-modify-write issues (which is also what happens when you don't disable the analog stuff).

    There's, to my knowledge, no drawbacks in using LATx (where available) so go ahead and use that but change your TRIS bits to 0 for the pins you want to be outputs.

    /Henrik.

  3. #3
    Join Date
    Dec 2014
    Posts
    12


    Did you find this post helpful? Yes | No

    Default Re: Setting latx instead of portx before trisx

    Thanks Henrik.
    Of cource it should be %00000000 to set as outputs, hjärnsläpp.
    I do like below @ 64mHz, but no capcitive load, and all A/Ds is off. I'll just use latx.
    porta.0 = 1
    porta.1 = 1
    porta.2 = 1
    porta.3 = 1

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,518


    Did you find this post helpful? Yes | No

    Default Re: Setting latx instead of portx before trisx

    Hi Matias,
    Yes, when you do things like that
    Code:
    porta.0 = 1
    porta.1 = 1
    porta.2 = 1
    porta.3 = 1
    Running at high speed can cause RMW issues. What happens is that for each "bit-flip" the port is read, bit is flipped, port is being written.
    Because the "limited" current capability of the output driver the voltage at the output doesn't rise instanously. So when PORTA.1=1 executes and the port is read the voltage at PORTA.0 hasn't reached a voltage above the threshold for a logic high so when the port is written back after flipping bit 1 bit 0 will be re-written as 0.

    /Henrik.

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,796


    Did you find this post helpful? Yes | No

    Default Re: Setting latx instead of portx before trisx

    So the recommendation is not to do consecutively bit operation but one port write.

    for example:
    Code:
    temp=porta
    temp=temp & $FF
    porta=temp
    Ioannis

Similar Threads

  1. USBBufferSize setting
    By Ronald123 in forum USB
    Replies: 4
    Last Post: - 18th April 2008, 13:33
  2. Setting up the oscillator
    By J_norrie in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 2nd October 2007, 15:53
  3. Setting a pin twice
    By RUBiksCUbe in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th September 2006, 02:45
  4. Setting up ADC....
    By robertmark68 in forum General
    Replies: 1
    Last Post: - 11th August 2006, 19:08
  5. setting register
    By volcane in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 19th April 2006, 23:46

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