728x90

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.

Sunday, August 1, 2021

Using the Internal RC Oscillator of PIC16F818 in XC8

Introduction

Using the Internal RC Oscillator of PIC16F818 in XC8
Breadboard prototyping

PIC16F818/19 comes with 8 types of oscillator from Low Power (LP) crystal to ECIO. Device’s specification sheet lists all its types below.

Using the Internal RC Oscillator of PIC16F818 in XC8
Oscillator Types of PIC16F818/19
From that list we have see two options of internal oscillator – INTIO1 and INTIO2. With INTIO2 we can access its internal oscillator with an advantage of getting two full I/O control on RA6 and RA7 pins.

Using the Internal RC Oscillator of PIC16F818 in XC8
An old used PIC16F818-I/P I stock in my own DIY lab.

Using this feature we can save components requirement at the time of physical hardware prototyping. The two clock pins – RA6 and RA7 can be used as a digital I/O. Furthermore, this internal Resistor/Capacitor (RC) oscillator is precise than the external crystal. As it’s placed internally it can withstand from noisy environment.

Using the Internal RC Oscillator of PIC16F818 in XC8
Clock diagram of PIC16F818/19

However it has a maximum frequency of 8MHz. PIC16F818/19 can operate up to its maximum external crystal oscillator of the frequency 20MHz. This highest frequency is useful for some applications.

Getting Ready for Programming and Interfacing

XC8 C compiler that run in its MPLABX IDE is a preferred embedded C compiler for PIC microcontroller. I have been using it many years now.

This programming example, I configure the embedded program to access the internal oscillator of PIC16F818 to its maximum frequency of 8MHz. The embedded program is just blinking an output LED connects to RA7.

Setting Up the Device

First we need to prototype this example program in software simulator for safety reason. The circuit diagram below is drawn using Proteus VSM.


Using the Internal RC Oscillator of PIC16F818 in XC8
Circuit diagram for simulation and hardware prototyping

Pin 14 is VDD pin while pin 5 is for the GND. These pins are hidden and it’s internally connected to both supply pin in this simulator.

Programming in MPLABX XC8

There are some register setting in C source code to configure this device properly. They are,

  • Configuration Word
  • Oscillator Tuning Register (OSCTUNE)
  • Oscillator Control Register (OSCCON)

Configuration Word

Oscillator Selection bits of the Configuration Word allows the user to select between some types of micro-controller’s oscillator.

Using the Internal RC Oscillator of PIC16F818 in XC8
Configuration Register of PIC16F818/819 (Address 2007h)

We will need to set FOSC2:FOSC0 to “100” in binary to select the internal oscillator inside the controller, and I/O function on its two clock pins.

Using the Internal RC Oscillator of PIC16F818 in XC8
Oscillator Selection bits

However in MPLABX XC8 C compiler we don’t need to access Configuration Word directly. It’s just done using the #pragma config directive.

pragma config FOSC = INTOSCIO

We can get these codes from the Configuration bits Source Window in MPLABX IDE.

Oscillator Tuning Register – OSCTUNE

This register allows the controller to fine tune to any frequency between 31.25kHz and its maximum 8MHz frequency. However we rarely use this register setting.

Using the Internal RC Oscillator of PIC16F818 in XC8
Oscillator Tuning Register – OSCTUNE (Address 90h)

In this tutorial we don’t need to use this register. We work with the Internal Oscillator Frequency Select bits of this register only.

Programming in XC8

This programming tutorial is very simple. The main program just configure the internal oscillator with a frequency of 8MHz, and repeatedly toggle an output LED on RA7 with a rate of one second.

/* 
 * File:   main.c
 * Author: admin
 *
 * Created on December 16, 2020, 6:45 PM
 */
#include <stdio.h>
#include <stdlib.h>
#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 = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#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)
#define _XTAL_FREQ 8000000
int main(int argc, char** argv) {
    /*Use its maximum 8MHz Internal
     Oscillator*/
    OSCCONbits.IRCF=0x07;
    /*Clear Port A*/
    PORTA=0x00;
    /*RA7 output*/
    TRISAbits.TRISA7=0;
    /*Main Loop*/
    while(1){
        /*Toggling RA7*/
        RA7^=1;
        __delay_ms(1000);
    }
    return (EXIT_SUCCESS);
}

Getting the Result

There is no differences between software simulator and real physical hardware testing of this programming tutorial. Click here to download source file.

Using the Internal RC Oscillator of PIC16F818 in XC8
Circuit diagram and simulation result from Proteus VSM

RA7 regularly blinks its output green LED. Red LED connects to RB0 turned on a moment after the microcontroller powered on.

It’s the same to one’s in simulator testing. LED connects to RB0 indicate the stability of the internal oscillator. It wait for a while to turn on after the controller started up.


Oscillator Stability

Program Preparation

INTOSC Frequency Stable bit (bit 2) of OSCCON register indicates the stability of internal oscillator. It’s set whenever the operation of oscillator is stable.

Using the Internal RC Oscillator of PIC16F818 in XC8
bit 2 – IOFS : INTOSC Frequency Stable bit

Now let test the stability solidity of this oscillator with an XC8 example. RB0 of Port B connects to an red LED via a current limiting resistor, specify the stability of internal oscillator. Logic value of RB0 is no other than the IOFS bit of the OSCCON register.

/*
 An XC8 Program to test the
 * INTOSC Frequency Stability
 */
#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 = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#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)
#define _XTAL_FREQ 8000000
void main(void) {
    /*Use its maximum 8MHz Internal
     Oscillator*/
    OSCCONbits.IRCF=0x07;
    /*Clear Port B*/
    PORTB=0x00;
    /*RB0 Output To LED*/
    TRISBbits.TRISB0=0;
    /*Clear Port A*/
    PORTA=0x00;
    /*RA7 output*/
    TRISAbits.TRISA7=0;
    /*Main Loop*/
    while(1){
        /*Toggling RA7*/
        RA7^=1;
        __delay_ms(1000);
        /*RB0 is high whenever the
         the oscillator is stable*/
        RB0=IOFS;
    }
}

We are going to test this embedded program in either simulator and physical hardware.

Getting the Result

Primarily this embedded programming example was tested using Proteus VSM simulator. The circuit diagram preparation has an additional component – the LED connects to RB0.

Using the Internal RC Oscillator of PIC16F818 in XC8
Circuit diagram and simulation result from Proteus VSM

RA7 regularly blinks its output green LED. Red LED connects to RB0 turned on a moment after the microcontroller powered on.

Using the Internal RC Oscillator of PIC16F818 in XC8
Hardware test on breadboard

It’s the same to one’s in simulator testing. LED connects to RB0 indicate the stability of the internal oscillator. It wait for a while to turn on after the controller started up. Click here to download source file. 


Using Clock Pin As a Digital Input

Using the INTOSCIO configuration, both RA6 and RA7 is digital I/O controlled by its Tri-State register.

An input button connects to RA6 is an active signal. Whenever it’s pressed, the program waits until it’s released, and toggle an output LED connects to RA7.

Using the Internal RC Oscillator of PIC16F818 in XC8
Circuit diagram and simulation in software

D2 LED connects to RB0 monitor the stability of the controller’s internal oscillator.

Using the Internal RC Oscillator of PIC16F818 in XC8
Physical hardware test of Using Clock Pin As a Digital Input

Hardware testing produced a similar result to the simulator. XC8 program comes with additional codes that continuously check the input logic state of push button.

/*
 Internal oscillator example
 * with digital input
 */
#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 = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is MCLR)
#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)


#define toggleButton    RA6
#define toggleLed       RA7

void main(void) {

    /*Use its maximum 8MHz Internal
     Oscillator*/
    OSCCONbits.IRCF=0x07;
    /*Clear Port B*/
    PORTB=0x00;
    /*RB0 Output To LED*/
    TRISBbits.TRISB0=0;
    /*Clear Port A*/
    PORTA=0x00;
    /*RA7 output*/
    TRISAbits.TRISA7=0;
    /*RA6 input for button*/
    TRISAbits.TRISA6=1;
    /*Main Loop*/
    while(1){
        if(toggleButton==0){
            while(toggleButton==0);
            toggleLed^=1;
        }
        /*RB0 is high whenever the
         the oscillator is stable*/
        RB0=IOFS;
    }
}

Click here to download source file.

Saturday, July 10, 2021

PIC16F818 MCLR Pin As a Digital Input

 

Introduction

Typically a microcontroller requires an external reset pin as an example of the Master Clear (MCLR) pin of PIC microcontroller. Some PIC device have an optional function on this reset pin, as an example of PIC16F818.

Pin RA5 of Port B is multiplexed with Master Clear module input. This reset pin could be turned off, and could be used as a digital input pin via software setting.

Configuration Register of PIC16F818

First, we need to turn MCLR function in the Configuration register off.

PIC16F818 MCLR Pin As a Digital Input
PIC16F818/819 Configuration Register addresses at 2007h

MCLRE is bit 5 of this configuration register. Clearing this bit to disable the MCLRE input function.

PIC16F818 MCLR Pin As a Digital Input
MCLRE selection bit
This setting just turn off the external reset function. So we don’t need a resistor that pull this pin to logic high.

Port Setting

Additionally, Port A of PIC16F818 is multiplexed with analog input function (10-bit ADC Module). Embedded programmer need to clear the analog input function to enable the digital I/O mode.

PIC16F818 MCLR Pin As a Digital Input
ADCON1 – A/D Control Register 1 addresses at 9Fh

The A/D Port Configuration Control bits of ADCON1 register must be set to 0x06 to enable all digital I/O on Port A. And then the user is able to use this digital input pin.

PIC16F818 MCLR Pin As a Digital Input
A/D Port Configuration Control bits of ADCON1

Next we are going to code this example.

Programming for MCLRE Digital Input Using XC8

This example, the controller toggle an output LED connects to RB0. Pin RA5/MCLRE is a digital input. Whenever it’s pressed, the controller waits until it’s released. After that the controller toggles RB0.

Schematic for Software Simulation

We prepare a circuit diagram for this example in simulator. It can be used in both reference circuit and simulation on PC.

PIC16F818 MCLR Pin As a Digital Input
Schematic diagram for this example

Here a push button SW1 that connect to RA5 is not an external reset circuit for the controller. It’s a digital input pin. Pressing this switch doesn’t reset the microcontroller.

XC8 Programming in MPLABX IDE

Using embedded to code an 8-bit microcontroller, the main source code made of a little lines of code.

*
 XC8 Example of disabling the
 * Master Clear Input pin and
 * used it as a digital input
 */
#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)
/*Button and LED Label*/
#define buttonInput RA5
#define outputLed   RB0
/*oscillator enumeration*/
enum intOscFreq{_31kHz=0,_125kHz,_250kHz,_500kHz,_1MHz,_2MHz,_4MHz,_8MHz=7};
/*Internal oscillator selection routine*/
void oscConfig(char  oscFreq){
    OSCCONbits.IRCF=oscFreq;
}
void main(void){
    /*Oscillator Setting Up*/
    oscConfig(_4MHz);
    /*Clear Analog Input on Port A*/
    ADCON1=0x06;
    /*Clear I/O*/
    PORTB=0x00;
    PORTA=0x00;
    /*Input Button*/
    TRISAbits.TRISA5=1;
    /*Ouput LED*/
    TRISBbits.TRISB0=0;
    while(1){
        /*Check if button pressed*/
        if(buttonInput==0){
            /*Wait until it's released*/
            while(buttonInput==0);
            /*Toggle LED*/
            outputLed^=1;
        }
    }
}

This example use the internal oscillator of PIC16F818. For more information about accessing its internal oscillator click here.

Prototyping on Breadboard

I wired this example on breadboard due to its simplicity. I worked like the one’s tested in Proteus simulator. Click here to download source file.

PIC16F818 MCLR Pin As a Digital Input
Prototyping on breadboard



Search This Blog