-1
\$\begingroup\$

Why is meant by statement as TRIS = 0x00 or LATB = 0x00, Why 0x00 while programming for PIC microcontroller in C.

\$\endgroup\$
  • \$\begingroup\$ Because we want all bits of those registers to be 0. Did you look up (in the datasheet) what the bits of those register do? \$\endgroup\$ – Wouter van Ooijen Oct 15 '16 at 12:56
  • \$\begingroup\$ It means setting all ports of specific port, here I think is portB as output thus here RB0-RB7 all are initialized as output port \$\endgroup\$ – Rajat Oct 15 '16 at 13:01
2
\$\begingroup\$

I'm not sure exactly what you think is strange, but I'll cover both my ideas.

Why 0x00, why not 0?

0x00 is the same as 0. It's just written in hexadecimal notation. The reason for doing that is that you often initialize values using a hexadecimal notation, because it's relatively easy to see each individual bit. Good old C did not have the binary 0b00000000 notation. If you write 0x00 instead of 0, some tables may look slightly prettier:

TRISA = 0x00;
TRISB = 0xC0;
TRISC = 0x01;

Why initialize at all?

These hardware registers are initialized to a specific state, as described in the manual. Some are left in an unchanged state, or undefined after a reset.

Naturally, if you want them to have another value, you need to write that.

Even if they were already initialized to zero, you may want to be explicit about it:

  • It helps documenting your code. You don't have to look up the defaults, it's right there in your code. Someone who is not as familiar with the particular processor does not have to assume anything.
  • Some registers are initialized in a different way depending on the type of reset. This may be unwanted.
  • You may want to perform a "soft restart", jumping to the start of your program without going through a hardware reset. The registers may be left in an unknown state.
\$\endgroup\$
  • 1
    \$\begingroup\$ I would add one obvious but very important thing. Add informative comments to initializations, describing why register is set to specific value and which function each bit carries. It will save a lot of time during debugging and recalling what initialization was about! \$\endgroup\$ – Anonymous Oct 15 '16 at 16:24

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.