8-bit Enhanced Mid-Range Digital I/O
Contents
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
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
Figure 2. Digital Pin I/O.
Five registers are available to configure and control digital I/O pins.
- TRISx - Sets the direction as either input or output.
- ANSELx - Determines if an analog capable pin works as an analog input or digital I/O.
- LATx- Used to output values for a digital pin.
- PORTx- Reads the input value of a digital pin.
- WPUx- Enables the internal weak pull up.
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.
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.
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
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.
2
bcf ANSELB,3 ; designate bit 3 as digital
2
3
4
5
// -- or --
ANSELB3 = 0 ;
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.
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.
2
3
4
5
6
7
8
{
ANSELB = 0 ; // set PORTB as digital
LATB = 0 ; // set output latch to known value
TRISB = 0 ; // make PORTB an output port
LATB = 0x0F // set lower four bits of PORTB
while(1);
}
2
3
4
5
6
7
8
9
10
clr ANSELB ; make all pins digital
banksel LATB
clr LATB ; set latch to known value
banksel TRISB
clr TRISB ; set all PORTB pins outputs
banksel LATB
movlw 0x0F
movwf LATB ; set lower 4 pins of PORTB
goto $ ; loop forever
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.
2
3
4
5
6
7
8
9
bcf ANSELB,3 ; make all RB3 digital
banksel LATB
bcf LATB,3 ; set latch to known value
banksel TRISB
bcf TRISB,3 ; set RB3 as an output pin
banksel LATB
bsf LATB,3 ; set RB3 hight
goto $ ; loop forever
2
3
4
5
6
7
8
{
ANSELBbits.ANSB3 = 0 ; // set RB3 as digital
LATBbits.LATB3 = 0 ; // set output latch to known value
TRISBbits.TRISB3 = 0 ; // make RB3 an output pin
LATBbits.LATB3 = 1 ; // set RB3 high
while(1);
}
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.
2
3
4
5
6
7
8
void main(void)
{
ANSELAbits.ANSA3 = 0; // make RA3 a digital pin
TRISAbits.TRISA3 = 0; // set RA3 as an output
while(1)
if (input_pin) exception_routine();
}
2
3
4
5
6
7
8
9
10
11
12
13
banksel ANSELA
bcf ANSELA,3 ; set RA3 as digital
banksel TRISA
bsf TRISA3 ; set RA3 as an input pin
Loop:
banksel PORTA
btfsc PORTA,3 ; "test" RA3
goto Exception_Routnine ; if 1 do this
goto Loop
Exception_Routine
; exception code goes here
Internal Weak Pull-ups
Internal weak pull-up resistors is enabled for each I/O pad using the WPUx register.
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.