Wednesday, February 4, 2009

Basic Port I/O

In this post, we will examine basic IO port control for the PIC18. In C18, it is very straightforward to control I/O pins of your chip.

To set the data direction, use the TRISX register like you normally would. For example, if I want to make my PORTC all output, I would use:

TRISC = 0x00; In hexadecimal notation

or

TRISC = 0b00000000; In binary notation.

Once the tristate registers have been set, you can begin to send data through the I/O pins. You can do this directly by writing to the PORTX register or control the individual bits with PORTXbits.RXn.

For example, to make pin 6 of PORTC a logic "1", you would do:

PORTCbits.RC6 = 1;

Reading the data from the ports follows the same pattern. Remember to set the TRISX variable to the right value in order to read data.

No comments:

Post a Comment