0
\$\begingroup\$

In a PIC microcontroller, people use PORTB to access all pins of the PORTB at once and PORTBbits.RB0 for bit specific addressing, for me RB0 itself is working, why not I use that instead of PORTBbits.RB0 I use the latest XC8 compiler Thank You

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Short answer: PORTBbits.RB0 is defined in xc.h, but RB0 is not.

Long answer: PORTBbits is defined in your microcontroller specific xc8 header file. For ex: pic18f45k22.h, it's included in xc.h. When you include xc.h, you also include your microcontroller specific xc8 header file, automatically. You can check where the PORTBbits.RB0 is defined via right click on PORTBbits.RB0 and "Go to Implementation". It opens your pic18fxxxx.h.

  • Easy way to use RB0, add #define RB0 PORTBbits.RB0 to top of your main file.
  • Better way to use RB0, create new header file and add all define statements for all I/Os. Then include it to your main file.
\$\endgroup\$
2
  • \$\begingroup\$ I have used a PIC16F1xxxx many ICs and have worked on many programs by just using RB0 and have faced no issues. I want to know if this way is Okay.. \$\endgroup\$ Commented Jul 11, 2020 at 17:00
  • \$\begingroup\$ According to Microchip Forums using RB0 is older method. Probably there is no standard and both are viable for some ICs. \$\endgroup\$ Commented Jul 11, 2020 at 17:43
0
\$\begingroup\$

You can use both forms. But be aware that in some rare cases, some bit names are shared between peripherals which have multiple instances. In those cases, you need to use the longer form.

Also, you may prefer to use the longer form to remind yourself the register name. When you always see them in pairs, it's easier to memorize register-bit relations and therefore it becomes easier to navigate in datasheet.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.