Enhanced Mid-Range Digital I/O

Basic I/O Structure

Almost every pin on the Enhanced Mid-Range PIC® Microcontroller (MCU) can be used as a digital input or output pin. Digital pins share these attributes:

  • Ability to monitor digital inputs
  • Control digital output signals
  • Internal weak pull-ups
  • Multiplexed with peripherals
  • High drive capability (up to 25 mA sink/source on many I/O pins)
  • Direct single-cycle bit manipulation
  • 4 kV ESD protection diodes

At RESET:

  • Digital pins revert to input (Hi-Z)
  • Analog-capable pins revert to analog

Digital I/Os are controlled by software in the MCU. The MCU program configures, reads, and outputs the values to digital pianos.

I/O Ports

ports.svg
Figure 1. I/O Port.

Individual Digital I/O pins are combined into groups called ports. I/O ports contain up to eight digital pins. Digital I/O pins can be accessed individually on a pin-by-pin basis. All the members of a particular I/O port can also be accessed in one instruction cycle by one of the MCU's byte-access instructions.

I/O ports are referred to by letter (e.g., PORTA, PORTB, PORTC) The number of I/O ports will vary depending on the PIC MCU being used. Consult the individual data sheet to determine the PORT assignments for a PIC MCU.

Typical Digital I/O Pin

basic-digital-io.svg
Figure 2. Digital Pin I/O.

Five registers are available to configure and control digital I/O pins.

  1. TRISx - Sets the direction as either input or output.
  2. ANSELx - Determines if an analog capable pin works as an analog input or digital I/O.
  3. LATx- Used to output values for a digital pin.
  4. PORTx- Reads the input value of a digital pin.
  5. WPUx- Enables the internal weak pull up.

There are five I/O control registers for each port.

  • For port A the control registers are: TRISA, ANSELA, LATA, PORTA, and WPUA.
  • For port B the control registers are: TRISB, ANSELB, LATB, PORTB, and WPUB.


Program examples showing how to use the I/O control registers are given throughout this page.

Identifying the I/O Pins on the Datasheet

Digital I/Os can share pins with other peripherals and MCU control lines. Some digital I/O pins are analog capable and can be configured to operate as analog input pins. Consult the pin diagram of the device datasheet to determine which pins are available as digital I/O.

Digital pins are identified by three sequential identifiers:

  • The first identifier for a digital pin is the letter R.
  • The second identifier is a letter of the PORT in which the pin is associated (such as A for PORTA, B for PORTB, etc.).
  • The final identifier is a number from 0 - 7 indicating the position in the PORT held by the Pin.
portb.png
Figure 3. All the digital pins associated with PORTB are highlighted in Yellow.

PORTB members range from RB0 - RB7.

For the other pins on the MCU:

  • Analog-capable pins are designated with the two letters AN followed by a number.
  • MCU Peripheral and Control pins are designated by name on the datasheet.
pin.png
Figure 4. In the close-up of the RB3/pin 24, the options for the PIC16L/F1936 are shown.

Pin 24 can be configured as digital Pin PORTB bit 3, analog channel 9, or one of several peripherals

In order for a pin to operate as a digital pin, all peripherals associated with the pin must NOT be enabled.

If an analog capable pin is to be used as a digital pin, in addition to not enabling the peripheral, the pin must be specifically configured as a digital pin.

Analog vs Digital Configuration

Depending upon which Enhanced Mid-Range PIC MCU is used, up to 30 digital pins can be configured to be analog pins. The ANSELx registers are used to configure the mode of the analog capable pins. (ANSELA controls the mode of all the analog capable pins on PORTA, ANSELB controls the mode for PORTB, ANSELD for PORTD, etc).

The analog capable pins in the port are mapped to individual bits in an ANSELx register. A value of 1 in a bit of an ANSELx register will enable the analog mode of the corresponding port pin. A value of 0 configures the pin to be digital.

At RESET all the analog capable pins revert to analog mode. The MCU sets all the relevant ANSELx bits to 1.

Example:

Pin 24, in Figure 4, can either be configured as analog channel 9 or as digital pin RB3.
To use this pin as a digital pin, bit 3 of ANSELB must be cleared.

When working with analog-capable digital pins remember:

  • If an Analog-to-Digital (ADC) conversion is attempted on an analog-capable pin configured as digital, the conversion will return an unchanging value which is not reflective of the voltage on the pin.
  • If a pin is configured as an analog pin, any digital value read from the pin will always be 1 regardless of the voltage on the input pad.
  • If an analog configured pin is written to as if it were a digital pin, the pin output level will not change.

Digital Outputs

The TRISx register controls the direction of data on each bit of a port. Each pin in a port is mapped to a bit in a TRIS register. The data direction for each pin can be set by writing an 8-bit value to the TRIS register individually, by setting/clearing a TRIS register bit, or the direction of all the bits in a port.

At RESET all the bits associated with pins in the TRIS registers are set to 1 making all pins High-Z inputs.

Making a pin a Digital Output pin

To configure a pin as a digital output, 0s need to placed in corresponding TRISx register bits.

Writing to a Digital Pin

The output value for each port can be loaded by writing to the port's LAT register. (Just as all the other port control registers, the LATx registers' names are lettered. LATx registers start with LATA, and proceed through LATB, LATC…)

Writing a 1 to a bit in the LAT register will drive the pin to Vdd. A 0 in a LAT bit will pull the pin to Vss.

Writing to the POTRTx register will also drive the output signal just as writing to the LATx register.

However, under high loads or at high frequency, if multiple bit modification commands (BSF, BCF) are written sequentially to an output PORT register, it is possible the last bit-manipulation instruction will overwrite a previous instruction, resulting in an incorrect value on the output port. To avoid the possibility of this occurring it is highly recommended that output always be made to the LATx register.

Example code:

The code shown here is an example of configuring all the pins in PORTB as digital outputs. Once configured as output pins, the lower four bits of the port are driven high while the upper four bits are set to 0.

Why was the LAT register cleared before the TRIS register was set?
At RESET the contents of the LATx registers are unknown. It is recommended the value of all output LAT register bits be set to a known (and safe) value before the output pins are enabled. This will prevent any spurious, unintended output pulses.

In this example, only the configuration of RB3 is changed to a digital output. Once configured, RB3 is driven high. All other pins (and their respective control register bits) are left unchanged.

Digital Inputs

Making a pin a Digital Input pin:

To configure a pin as a digital input, 1s need to placed in the corresponding TRISx register bits.

Reading a Digital Pin

The value for each input pin can be observed by reading the corresponding PORTx register. If the voltage level on a particular pin is above the input threshold the bit in the PORTx register associated with the pin will contain a 1. Voltages below the threshold will contain a 0.

Writing to a LATx register bit of a pin configured as an input will not affect the pin value.

Example code:

This sample program sets up RA3 (PORTA bit 3) as a digital input pin. The program then continually monitors the value of RA3. When RA3 goes high, program control is transferred to an exception routine.

Internal Weak Pull-ups

Internal weak pull-up resistors is enabled for each I/O pad using the WPUx register.

wpu.svg

The pull-up will be disabled automatically when either TRIS is set to an output or the pin is set as an analog input. These changes to TRIS and ANSEL will override WPU settings

Enabling Pull-ups

The internal pull-up for a digital input pin is enabled by writing a 1 to the appropriate WPUx register. Writing a 0 to the WPUx register disables the pull-up.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.