PIC18 series single-chip microcomputer I/O port operation register and its application

The PIC18 series of single-chip microcomputers is a high-end series in the 8-bit microcontroller series of Microchip. The maximum sink current or maximum source current allowed by any I/O pin is up to 25mA, which can directly drive LEDs and relays. The total maximum sink current or maximum source current of PORTA, PORTB and PORTE is 200mA, the total maximum sink current or maximum source current of PORTC and PORTD is 200mA, and the total maximum sink current or maximum source current of PORTF and PORTG is 100mA (Note: PIC18F4550 Without these two ports).

Test chip: Microchip PIC 18F4550

Integrated development environment: MPLAB IDE v8.53

Compiler: Microchip C18

The PIC18 series of single-chip microcomputers is a high-end series in the 8-bit microcontroller series of Microchip. The maximum sink current or maximum source current allowed by any I/O pin is up to 25mA, which can directly drive LEDs and relays. The total maximum sink current or maximum source current of PORTA, PORTB and PORTE is 200mA, the total maximum sink current or maximum source current of PORTC and PORTD is 200mA, and the total maximum sink current or maximum source current of PORTF and PORTG is 100mA (Note: PIC18F4550 Without these two ports).

The interaction between the single-chip microcomputer and peripherals is carried out through the I/O port, and each I/O port has three operation registers:

1. TRISx-data direction register

Used to control the direction of I/O pins, that is, to control whether PORTx is input or output.

2. PORTx – Port register

Used to latch output data. When reading PORTx, the device directly reads the I/O pin level (not the latched value).

3. LATx-output data latch

Writing to the port means writing to the latch (LATx). The data latch can also be directly read and written. If the peripheral does not use the pin, and the TRISx bit configures the pin as an output, the data in the latch is output to the pin.

In the reset state, the reset value of TRISx is 0xff, that is, the value of the 8 bits (D0 ~ D7) of the TRISx register are all 1. At this time, the corresponding PORTx pin is defined as an input, and the corresponding output driver presents a high-impedance state. When set to 0, it means that the corresponding pin is defined as an output.

PIC18 series single-chip microcomputer I/O port operation register and its application

It should be noted here that writing PORT is writing LAT, but reading PORT is different from reading LAT. Reading PORT reads the status of the pin, regardless of whether the pin is set as an input pin or an output pin. The value obtained by reading LAT is the stored value of the output data latch, and the value obtained by reading LAT may be different from the value obtained by reading PORT.

PIC18 series single-chip microcomputer I/O port operation register and its application

In Microchip C18, the three operation registers of the I/O port can be operated in bit or byte.

For example, the direction register of port B is represented by TRISB (or DDRB), and a bit is represented by TRISBbits.TRISB0 (or DDRB bits.RB0). The byte is represented by TRISB (or DDRB).

For example, the PORT register of port B is represented by PORTB, and a certain bit is represented by PORTBbits.RB0. The byte is represented by PORTB.

For example, the output data latch of port B is represented by LATB, and a certain bit is represented by LATBbits.LATB0. The byte is represented by LATB.

Since the value of the LATx (PORTx) latch is random after the chip is reset, in order to eliminate the possibility of glitches in the I/O pin level, when initializing the port, first initialize the PORT data latch (LAT or PORT register), and then initialize the data direction register TRIS.

Let’s use an example to explain the specific application. The following figure PIC18F4550 and the power supply, crystal oscillator and light-emitting diode form the simplest 8-bit single-chip microcomputer system, which requires 8 light-emitting diodes to be lighted at the same time.

PIC18 series single-chip microcomputer I/O port operation register and its application

First of all, you can choose the method of bitwise operation. It is not difficult to see that the bit-wise operation does not actually realize the simultaneous lighting of the 8 LEDs connected to PORTB, but the delay effect of the LEDs illuminates the fact that they are sequentially lit, making the final effect achieve simultaneous lighting. The following is the implementation code of bitwise operation.

#include

void main(void)

{

PORTBbits.RB0=1;

TRISBbits.TRISB0=0;//light up the first LED

PORTBbits.RB1=1;

TRISBbits.TRISB1=0; //light up the second LED

PORTBbits.RB2=1;

TRISBbits.TRISB2=0; //light up the third LED

PORTBbits.RB3=1;

TRISBbits.TRISB3=0; //light up the 4th LED

PORTBbits.RB4=1;

TRISBbits.TRISB4=0; //light up the 5th LED

PORTBbits.RB5=1;

TRISBbits.TRISB5=0; //light up the 6th LED

PORTBbits.RB6=1;

TRISBbits.TRISB6=0; //light up the 7th LED

PORTBbits.RB7=1;

TRISBbits.TRISB7=0; //light up the 8th LED

while(1);

}

Secondly, it can be realized by byte operation, the code is much simpler than bitwise operation, and it truly realizes the requirement of simultaneous lighting. The following is the implementation code of byte-wise operation.

#include

void main(void)

{

PORTB=0xff;

TRISB=0x00;//light up 8 LEDs

while(1);

}

The Links:   MG75J2YS50 MDR250A40

Scroll to top