Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 100, 300, & 500 Millisecond

Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 100 Millisecond
Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 300 Millisecond
Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 500 Millisecond

Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 100 millisecond:

/* Main.c file generated by New Project wizard
 *
 * Created:   Tue Jun 30 2015
 * Processor: PIC16F84A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Main.c:    Main Programming file using 'C' Language
 */

#include<htc.h>

// Config word
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);

// Define LED pin
#define LED  RA0

// 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 = 0;                      // Make RA0 pin output
    LED    = 0;                        // Make RA0 low
    
    while(1)
    {
        __delay_ms(100);       // 100 millisecond Delay
        LED = 0;                      // LED off
        __delay_ms(100);      // 100 millisecond Delay
        LED = 1;                     // LED on
    }
}

Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 300 millisecond:

/* Main.c file generated by New Project wizard
 *
 * Created:   Tue Jun 30 2015
 * Processor: PIC16F84A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Main.c:    Main Programming file using 'C' Language
 */

#include<htc.h>

// Config word
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);

// Define LED pin
#define LED  RA0

// 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 = 0;                       // Make RA0 pin output
    LED    = 0;                         // Make RA0 low
    
    while(1)
    {
        __delay_ms(300);        // 300 millisecond Delay
        LED = 0;                       // LED off
        __delay_ms(300);       // 300 millisecond Delay
        LED = 1;                      // LED on
    }
}

Microchip PIC16F84A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 500 millisecond:

/* Main.c file generated by New Project wizard
 *
 * Created:   Tue Jun 30 2015
 * Processor: PIC16F84A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Main.c:    Main Programming file using 'C' Language
 */

#include<htc.h>

// Config word
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);

// Define LED pin
#define LED  RA0

// 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 = 0;                       // Make RA0 pin output
    LED    = 0;                         // Make RA0 low
    
    while(1)
    {
        __delay_ms(500);        // 500 millisecond Delay
        LED = 0;                       // LED off
        __delay_ms(500);       // 500 millisecond Delay
        LED = 1;                       // LED on
    }
}

Explanation and Working Procedure of PIC16F84A:

  • This article provides the code to make an LED's blink using PIC16F84A microcontroller. This code is written in C language using HI-TECH C for PIC/10/12/16 compiler. This code is intended to be the first step in learning how to use PIC16F84A microcontroller in our projects. 
  • Following circuits above shows the minimum circuit required to make an LED blink with PIC16F84A.
  • There is a crystal of 20MHz used with PIC16F84A. We can use any crystal from 0 to 20MHz with PIC16F84A
  • MCLR master reset pin is pulled high to keep PIC16F84A out of reset. RA0 pin is being toggled in the code.
  • "htc.h" file must be included in programming file, whenever writing a code in 'C' language. After including "htc.h" file, configuration bits are being set in the code shown above. To understand the details of how these configuration bits are being programmed is already explained below.
  • After the configuration bits, LED pin is being defined as the RA0 pin. We can replace RA0 with any other pin name if we want (e-g RA1, RA2, RA3, RA4 or RB0, RB1, RB2, RB3, RB4, RB5, RB6, RB7 etc). After LED pin definition, CPU frequency is being defined.We have to define _XTAL_FREQ macro, if we want to use built in delay functions like __delay_us() and __delay_ms(). Here CPU frequency is defined to be 20MHz, which is the crystal oscillator frequency attached with PIC16F84A in the circuit.
  • In the main function, firstly RA0 pin direction is set to be an output pin using TRISA0 = 0; statement. Using TRISx register, For details  about TRISX register, please refer to PIC16F84A datasheet we can set the direction of any pin i-e if it is an input or output. Then LED pin is made low using LED = 0; statement.
  • LED pin is being toggled in the while loop after every half second. In this way we can easily make LED blink using PIC16F84A microcontroller.
  • 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). 
  • LED blinking Circuit and code using PIC16F84A was designed in Proteus 8.0 and compiled in HI-TECH C for PIC10/12/16 compiler.

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.
#endif

/* 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 LED Blinking Using PIC16F84A:
    • 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. The code to make an LED's blink with PIC16F84A with time delays 100ms, 300ms and 500ms is already shown above. 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

Advertisement:

SiteLock