Microchip PIC16F84A Input Pin Code,RA0 Pin as an Input, RB0 Pin Output to LED, Using ' C 'Language

Microchip PIC16F84A Input Pin Code,RA0 Pin as an Input, RB0 Pin Output to LED, Using ' C 'Language

Microchip PIC16F84A Input Pin Code,RA0 Pin as an Input, RB0 Pin Output to LED, Using ' C 'Language:

/* Main.c file generated by New Project wizard
  * Created:   Wed Jul 1 2015
 * Processor: PIC16F84A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Main.c:    Programming file written in 'C' Language


// Config word

// Define CPU Frequency
// This must be defined, if __delay_ms() or 
// __delay_us() functions are used in the code
#define _XTAL_FREQ   20000000    

// Main function
void main()
    TRISA0 = 1;                     // Make RA0 pin input
    TRISB0 = 0;                    // Make RB0 pin output
    RB0    = 0;                     // Make RB0 pin value zero
        if(RA0)                       // If RA0 is high
            RB0 = 1;                // Make RB0 high as well
        else                          // Otherwise if RA0 is low
            RB0 = 0;             // Make RB0 zero as well

Explanation and Working Procedure of PIC16F84A:

  • This article provides an example code to use PIC16F84A pins as inputs. After going through this example, we will understand how to make PIC16F84A pins as inputs and how to read their value in the code. This code is written in C language using HI-TECH C for PIC10/12/16 compiler.
  • Following figure above shows the circuit used to demonstrate how to get input in PIC16F84A micro-controller.
  • In the figure above, PIC16F84A is running on external crystal of 20MHz. RA0 pin is being used as the input pin. When the push button is in the pushed state then, RA0 pin is high, otherwise RA0 pin is low. Whenever RA0 pin is high, then RB0 pin (Attached with the LED) is also made high just to indicate correct reading of RA0 pin status in the micro-controller.
  • In the main function, firstly RA0 pin is made an input and RB0 is made an output. Using TRISx register, For details please refer to PIC16F84A datasheet, we can set the direction of any pin i-e if it is an input or output. So, TRISA0 = 1; makes RA0 pin an input pin. And TRISB0 = 0; makes RB0 pin an output pin. Also, using the statement RB0 = 0; RB0 pin is made low.
  • In the while(1) loop, status of RA0 pin is constantly being checked, if it becomes high then RB0 is also made high. If RA0 is low, then RB0 is also made low as well.
  • Because every PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, when CPU frequency is defined to be 20MHz, then actual speed of this PIC microcontroller will be 5 MIPS (Million of instructions per second).
  • We can attach any crystal from 0 to 20MHz with PIC16F84A.
  • Input pin code using PIC16F84A was compiled in HI-TECH C for PIC10/12/16 v9.83 compiler and simulation was made in Proteus v8.

Understanding and Setting the "Configuration bits" in PIC16F84A:

  • We have to set configuration bits in order to make PIC microcontroller work correctly. Here we are going to explain the procedure of selecting configuration bits for PIC16F84A. A typical example code for setting configuration bits in the code Using compiler HI-TECH C for PIC10/12/16 is provided. Following steps are required to setup configuration bits for PIC16F84A.
  • Step 1:
    • The first step to start writing the configuration bits is to check "Special Features of the CPU" section in the datasheet of PIC16F84A. In this section, first chapter is about "Configuration bits". We should read this section and identify what values we want to put in the configuration register.
  • Step 2:
    • Open the file "pic16f84a.h" from the directory "C:\Program Files\HI-TECH Software\PICC\9.83\include". In the start of this file configuration bit macros are defined. Few lines of sample code is written below.
"pic16f84a.h" file @ C:\Program Files\HI-TECH Software\PICC\9.83\include

#ifndef    _HTC_H_
#warning Header file pic16f84a.h included directly. Use #include <htc.h> instead.

/* header file for the MICROCHIP PIC microcontroller
 *  16F84A

#ifndef __PIC16F84A_H
#define __PIC16F84A_H

// Configuration mask definitions

// Config Register: CONFIG
#define CONFIG               0x2007
// Oscillator Selection bits
// RC oscillator
#define FOSC_EXTRC           0xFFFF
// HS oscillator
#define FOSC_HS              0xFFFE
// XT oscillator
#define FOSC_XT              0xFFFD
// LP oscillator
#define FOSC_LP              0xFFFC
// Watchdog Timer
// WDT enabled
#define WDTE_ON              0xFFFF
// WDT disabled
#define WDTE_OFF             0xFFFB
// Power-up Timer Enable bit
// Power-up Timer is disabled
#define PWRTE_OFF            0xFFFF
// Power-up Timer is enabled
#define PWRTE_ON             0xFFF7
// Code Protection bit
// Code protection disabled
#define CP_OFF               0xFFFF
// All program memory is code protected
#define CP_ON                0xC00F

  • Step 3:
    • Here we have to choose appropriate macros to be used in the code to set configuration bits correctly. For each configuration bit we have a choice between some macros. For example, for watchdog timer enable bit, we have a choice between WDTE_ON or WDTE_OFF. If we want to enable the watchdog timer, then WDTE_ON macro should be used in the code. But if we want to disable watchdog, then WDTE_OFF macro should be used in the code to set the configuration bit accordingly. Similarly, we can select appropriate macros for other configuration bits.
  • Code for PIC16F84A Input Pin:
    • We can program configuration bits in the code using __CONFIG macro, For details check manual HI-TECH C® for PIC10/12/16 User’s Guide Chapter 3.2.2. We can see how __CONFIG macro is used here, to set the desired configuration bits in the code.
  • We have to place the __CONFIG macro outside the main function, preferably in the start of the file. All the selected macros are separated with the & (AND operator) in code. In this code selected configuration bits for PIC16F84A are:
  1. External HS crystal is selected as CPU clock source.
  2. Watchdog is disabled.
  3. Power up timer is enabled.
  4. Program memory code protection is disabled.

For details of these settings, check the PIC16F84A datasheet section "Special features of the CPU".

Features of PIC16F84A, 18-pin Enhanced FLASH/EEPROM 8-Bit Microcontroller​:

High Performance RISC CPU Features:
  •  Only 35 single word instructions to learn
  • All instructions single-cycle except for program branches which are two-cycle
  • Operating speed: 
    • DC - 20 MHz clock input 
    • DC - 200 ns instruction cycle
  • 1024 words of program memory
  • 68 bytes of Data RAM
  • 64 bytes of Data EEPROM
  • ​14-bit wide instruction words
  • 8-bit wide data bytes
  • 15 Special Function Hardware registers
  • Eight-level deep hardware stack
  • Direct, indirect and relative addressing modes 
  • Four interrupt sources:
    • External RB0/INT pin
    • TMR0 timer overflow
    • PORTB<7:4> interrupt-on-change
    • Data EEPROM write complete
Peripheral Features:
  •  13 I/O pins with individual direction control
  • High current sink/source for direct LED drive
    • 25 mA sink max. per pin
    • 25 mA source max. per pin
  • TMR0: 8-bit timer/counter with 8-bit programmable prescaler
Special Microcontroller Features:
  • 10,000 erase/write cycles Enhanced FLASH Program memory typical
  • 10,000,000 typical erase/write cycles EEPROM Data memory typical
  • EEPROM Data Retention > 40 years
  • In-Circuit Serial Programming™ (ICSP™) - via two pins
  • Power-on Reset (POR), Power-up Timer (PWRT), Oscillator Start-up Timer (OST)
  • Watchdog Timer (WDT) with its own On-Chip RC Oscillator for reliable operation
  • Code protection
  • Power saving SLEEP mode
  • Selectable oscillator options
CMOS Enhanced FLASH/EEPROM Technology:
  • Low power, high speed technology
  • Fully static design
  • Wide operating voltage range:
    • Commercial: 2.0V to 5.5V
    • Industrial: 2.0V to 5.5V
  • Low power consumption:
    • < 2 mA typical @ 5V, 4 MHz
    • 15 micro A typical @ 2V, 32 kHz
    • < 0.5 micro A typical standby current @ 2V 

More From Iamtechnical.com