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



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 (54) 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 (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) 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 (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)