Saturday, August 7, 2021

PIC16F818 Port B and its Internal Resistors

 

Introduction

Port B of PIC16F818/819 is an 8-bit wide bi-directional digital I/O port. It’s input/output direction is controlled by its data direction register TRISB. Optionally it has an internal weak pull up resistors individually connects to each pin of Port B. However this 8 resistor network is programmable on/off by software.

PIC16F818 Port B and its Internal Resistors
Pin Diagram of PIC16F818 from device’s datasheet

Port Data and Its Direction Control

Port B locates at 06h and 106h in the Special Function Register (SFR). It’s bi-directional read/write.

Port B Data Direction Register (TRISB) locates at 86h and 186h. Writing ‘1’ to any bit of this register forcing that bit to an input pin, otherwise forcing that bit to an output pin.

Internal Weak Pull Up Resistor

Port B is programmable connect to an internal weak pull up resistors network. There is only one global switch that turn this resistor network on and off. It it’s the PORTB Pull-up Enable bit (nRBPU), of the OPTION register (OPTION_REG) locates at 81h and 181h.

PIC16F818 Port B and its Internal Resistors
Option Register of PIC16F818

This bit locates at bit 7. Clearing it to enable the internal weak pull up resistor.

Internal Weak Pull Up Resistor Programming in XC8

Using this free version of embedded C compiler, the overall coding is done within a light afford. A simple example shows how to use this feature of PIC microcontroller without adding external resistors to the digital input.

Schematic Diagram

This schematic diagram uses in both physical hardware wiring and software simulation.

PIC16F818 Port B and its Internal Resistors
Schematic diagram drawn in Proteus

RB0 connects to SW2 to turn of the LED D2 whenever it’s pressed. SW1 connects to SW1 to turn off the LED D2 whenever it’s pressed. It’s clock source is an internal oscillator inside the device with a frequency of 31.25kHz.

XC8 Programming

Embedded C program prepare the internal oscillator, I/O direction, and turning on its internal weak pull up resistor.

/*
 XC8 Program to use PIC16F818
 * internal weak pull up resistor
 */
#include <xc.h>
// PIC16F818 Configuration Bit Settings
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CCPMX = RB2      // CCP1 Pin Selection bit (CCP1 function on RB2)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
/*Label the inputs outputs*/
#define _ON     RB0==0
#define _OFF    RB1==0
#define _LED1   RB7
void main(void){
    /*Set internal oscillator to 31.25kHz*/
    OSCCONbits.IOFS=0x00;
    /*Clear Port B*/
    PORTB=0x00;
    /*RB0.1 Digital Input*/
    TRISB=0x03;
    /*Turn On Pull Up Resistors*/
    OPTION_REGbits.nRBPU=0;
    /*Main Program Loop*/
    while(1){
        if(_ON){
            while(_ON);
            _LED1=1;
        }
        if(_OFF){
            while(_OFF);
            _LED1=0;
        }
    }
}

This program requires only 2.4% of program memory (Flash).

Simulator and Physical Hardware Testing

Both simulator and external physical hardware testing produce the same result.

PIC16F818 Port B and its Internal Resistors
Program simulation in Proteus


PIC16F818 Port B and its Internal Resistors
Physical hardware test on breadboard

Click here to download source file.

No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)