Introduction
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.
Oscillator Types of PIC16F818/19 |
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Circuit diagram and simulation in software |
D2 LED connects to RB0 monitor the stability of the controller’s internal oscillator.
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.
Click here to download source file.
No comments:
Post a Comment