View Full Version : Setting latx instead of portx before trisx
bmoe79
- 27th January 2015, 09:47
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
HenrikOlsson
- 27th January 2015, 10:57
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.
bmoe79
- 27th January 2015, 11:20
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
HenrikOlsson
- 27th January 2015, 12:04
Hi Matias,
Yes, when you do things like that
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.
Ioannis
- 30th January 2015, 10:31
So the recommendation is not to do consecutively bit operation but one port write.
for example:
temp=porta
temp=temp & $FF
porta=temp
Ioannis
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.