I was browsing through the example code that MikroE provides for PIC programming using MikroC, and they always set PORTx before setting TRISx. What is the reason for this?

Since TRIS just selects whether the port or pin is output/input mode, why does it matter what the pin is set to before we set the mode?

Example code

  PORTA = 255;
  TRISA = 255;                        // configure PORTA pins as input
  PORTB = 0;                          // set PORTB to 0
  TRISB = 0;                          // designate PORTB pins as output
  PORTC = 0;                          // set PORTC to 0
  TRISC = 0;                          // designate PORTC pins as output
  • 1
    \$\begingroup\$ Whenever you see comments like PORTB = 0; // set PORTB to 0, you know for certain that the programmer was not paying attention to what they were doing. \$\endgroup\$ Mar 16 '14 at 9:16
  • \$\begingroup\$ Why is that? Isn't that what PORTB=0 does? \$\endgroup\$
    – Shubham
    Mar 18 '14 at 20:22

The PORTx bits are adjusted before the TRISx bits so you know what the output will be before you make the pin an output. If you are setting the TRISx bits to inputs, it doesn't matter. However, if you are clearing the TRISx bits and making them outputs, it is safer to determine what the output will be before it switches from input to output. Thus, if an alarm or something goes off when a pin is high, you would want to make sure when it becomes an output, it starts off low.


For this specific bit of code the answer is that the programmer wants to set the pins to a known state/voltage before setting them as outputs. But in general, the TRIS bits do more than just set a pin to input or output mode.

The TRIS mnemonic stands for tri-state. When a pin is tristated the resistance of that pin goes so high that they're effectively disconnected from the circuit.

If you've ever looked at busses you'd have seen tri-state circuits. They're the poor man's way of connecting multiple outputs together without using a multiplexer or a switch. All you need is to make sure that there is only one output on the bus that is not in tri-state mode at any point in time.

There is also a "safe" method of implementing busses by not allowing any device to output any voltage other than zero volts. The values on the bus gets VCC voltage via one or several pull-up resistors. Lots of serial busses do this. If I remember correctly CAN bus does this. Contention is never a problem with this scheme. You just need to take care of collisions which can be done in software.

Therefore in a lot of code I've worked with (and indeed in a lot of my own code) the PORT bits are hardcoded to 0 and the pins are driven by setting the TRIS bits to 1 or 0. So you will sometimes see code in production where the outputs are controlled via the TRIS bits rather than the PORT bits.

  • \$\begingroup\$ Just so it's written down, those types or outputs are "open collector" or "open drain" outputs. \$\endgroup\$ Mar 16 '14 at 2:39
  • \$\begingroup\$ The last bit you suggest, that would basically make them open drain outputs right? \$\endgroup\$
    – Shubham
    Mar 18 '14 at 20:25
  • \$\begingroup\$ As mentioned by jrtrzeciak, yes. \$\endgroup\$
    – slebetman
    Mar 18 '14 at 23:22

Maybe it's a way of preventing output glitching hi then low straight after a tris. If you set outputs low before tris, they are guaranteed to be low after the tris. Maybe! I guess this assumes the output port register may contain random bits or maybe they are all hi after a reset.

  • \$\begingroup\$ There is a chance that on start up the ram location for the output register has been set. This was an even worse problem when MCU eproms could only be erazed by UV and you forgot to cover the window ... \$\endgroup\$
    – Spoon
    Mar 16 '14 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.