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.

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)