Книга: Advanced PIC Microcontroller Projects in C

3.2 PIC Microcontroller Input-Output Port Programming

3.2 PIC Microcontroller Input-Output Port Programming

Depending on the type of microcontroller used, PIC microcontroller input-output ports are named as PORTA, PORTB, PORTC, and so on. Port pins can be in analog or digital mode. In analog mode, ports are input only and a built-in analog-to-digital converter and multiplexer circuits are used. In digital mode, a port pin can be configured as either input or output. The TRIS registers control the port directions, and there are TRIS registers for each port, named as TRISA, TRISB, TRISC, and so on. Clearing a TRIS register bit to 0 sets the corresponding port bit to output mode. Similarly, setting a TRIS register bit to 1 sets the corresponding port bit to input mode.

Ports can be accessed as a single 8-bit register, or individual bits of a port can be accessed. In the following example, PORTB is configured as an output port and all its bits are set to a 1:

TRISB = 0;    // Set PORTB as output
PORTB = 0xFF; // Set PORTB bits to 1

Similarly, the following example shows how the 4 upper bits of PORTC can be set as input and the 4 lower bits of PORTC can be set as output:

TRISC = 0xF0;

Bits of an input-output port can be accessed by specifying the required bit number. In the following example, variable P2 is loaded with bit 2 of PORTB:

P2 = PORTB.2;

All the bits of a port can be complemented by the statement:

PORTB = ~PORTB;

Оглавление книги


Генерация: 1.094. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз