PortB of PIC16F887 could be used as digital inputs outputs. Pins RB0 to RB5 multiplexed with analog input channel for ADC reading.
Digital | Analog |
RB0 | AN12 |
RB1 | AN10 |
RB2 | AN8 |
RB3 | AN9 |
RB4 | AN11 |
RB5 | AN13 |
When using those pins as a digital input, we must clear related bits in the ANSELH register.
BIT 7 | BIT 0 | ||||||
N/A | N/A | ANS13 | ANS12 | ANS11 | ANS10 | ANS9 | ANS8 |
When any ANSx is equal to '0', the corresponding pin is a digital input, otherwise an analog input.
Optionally, PORTB has its own individual weak pull up resistor. We can turn it on/off one by one, or all of them.
To use weak pull up resistor, we must first clear the nRBPU of the OPTION_REG.
BIT 7 | BIT 0 | ||||||
nRBPU | INTEDG | T0CS | T0SE | PSA | PS2 | PS1 | PS0 |
Each weak pull up resistor stores in WPUB register.
BIT 7 | BIT 0 | ||||||
WPUB7 | WPUB6 | WPUB5 | WPUB4 | WPUB3 | WPUB2 | WPUB1 | WPUB0 |
To turn it on, set it to '1'.
In this example, I use PORTB to read digital input from switches. All weak pull up resistors are turned on.
Schematic diagram. PortB connects to input switches. PortD connects to bar graph LEDs output. We no need to add external pull up resistors. All pins of PORTB are internally pulled high. |
Source code is written using XC8.
#include<xc.h>
// PIC16F887 Configuration Bit Settings
// CONFIG1
#pragma config FOSC = XT
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config IESO = ON
#pragma config FCMEN = ON
#pragma config LVP = OFF
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF
void main(){
//Clear Ports
PORTB=0x00;
PORTD=0x00;
//PORTB is digital input
TRISB=0xFF;
//PORTD is digital output
TRISD=0x00;
//Disable analog input on PORTB
ANSELH=0x00;
//Enable Weak Pullups
nRBPU=0;
//Turn on all Weak Pullups
WPUB=0xFF;
while(1) PORTD=PORTB; //Read PORTB
}
A screen shot of the running program.
Proteus 8 simulation screen shot. PORTB read 0x0F. PORTD outputs 0x0F. |
No comments:
Post a Comment