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

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

Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 100 Milliseconds:

/* Main.c file generated by New Project wizard
 * Created:   Fri Jun 26 2015
 * Processor: PIC16F877A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Website:   www.iamtechnical.com
 * Main.c :   Programming file for LED blinking with PIC16F877A.
 */

#include <htc.h>

// Configuration word for PIC16F877A

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
         & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

// Define LED pin

#define LED RB0

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

#define _XTAL_FREQ 20000000  

void main(void)
{
    TRISB0 = 0;                         // Make RB0 pin output
    LED    = 0;                          // Make RB0 low
    while(1)
    {
        __delay_ms(100);         // 100 Millisecond delay
        LED = 0;                       // LED off
        __delay_ms(100);      // 100 Millisecond delay
        LED = 1;                    // LED on
    }
}

Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 300 Milliseconds:

/* Main.c file generated by New Project wizard
 * Created:   Fri Jun 26 2015
 * Processor: PIC16F877A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Website:   www.iamtechnical.com
 * Main.c :   Programming file for LED blinking with PIC16F877A.
 */

#include <htc.h>

// Configuration word for PIC16F877A

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
         & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

// Define LED pin

#define LED RB0

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

#define _XTAL_FREQ 20000000  

void main(void)
{
    TRISB0 = 0;                         // Make RB0 pin output
    LED    = 0;                          // Make RB0 low
    while(1)
    {
        __delay_ms(300);         // 300 Millisecond delay
        LED = 0;                       // LED off
        __delay_ms(300);      // 300 Millisecond delay
        LED = 1;                    // LED on
    }
}

Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 500 Milliseconds:

/* Main.c file generated by New Project wizard
 * Created:   Fri Jun 26 2015
 * Processor: PIC16F877A
 * Compiler:  HI-TECH C for PIC10/12/16
 * Author:    Azhar Ahmed
 * Website:   www.iamtechnical.com
 * Main.c :   Programming file for LED blinking with PIC16F877A.
 */

#include <htc.h>

// Configuration word for PIC16F877A

__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON
         & LVP_OFF & CPD_OFF & WRT_OFF & DEBUG_OFF);

// Define LED pin

#define LED RB0

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

#define _XTAL_FREQ 20000000  

void main(void)
{
    TRISB0 = 0;                         // Make RB0 pin output
    LED    = 0;                          // Make RB0 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:
  • This article provides the code to make an LED blink using PIC16F877A microcontroller. This code is written in C language using with HI-TECH C for PIC10/12/16 compiler. This code is intended to be the first step in learning how to use PIC16F877A microcontroller in your projects. 
  • Above three circuit diagram shows the minimum circuit required to make an LED blink with PIC16F877A
  • There is a crystal of 20MHz used with PIC16F877A. You can use any crystal from 3 to 20MHz with PIC16F877A
  • MCLR master reset pin is pulled high to keep PIC16F877A out of reset. RB0 pin is being toggled in the code.
  • The codes for making LED blink using PIC16F877A is shown above with times delays 100, 300 and 500 Millisecond.
  • "htc.h" file in the code must be included whenever writing code for PIC microcontrollers. After including "htc.h" file, configuration bits are being set in the code shown above.
  • After the configuration bits, LED pin is being defined as the RB0 pin. You can replace RB0 with any other pin name if you want (e-g RB1 etc). After LED pin definition, CPU frequency is being defined. You have to define _XTAL_FREQ macro, if you 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 PIC16F877A in the circuit.
  • In the main function, firstly RB0 pin direction is set to be an output pin using TRISB0 = 0; statement. Using TRISx register. refer datasheet for detail about TRISX register , 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 100ms, 300ms and 500ms in three circuits above. In this way one can easily make LED blink using PIC16F877A microcontroller.
  • 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). 

Features of PIC16F877A 40 Pin Enhanced Flash Microcontrollers:

High-Performance RISC CPU:
  • Only 35 single-word instructions to learn
  • All single-cycle instructions except for program branches, which are two-cycle
  • Operating speed:
    • DC – 20 MHz clock input
    • DC – 200 ns instruction cycle
  • Up to 8K x 14 words of Flash Program Memory, Up to 368 x 8 bytes of Data Memory (RAM), Up to 256 x 8 bytes of EEPROM Data Memory
  • Pinout compatible to other 28-pin or 40/44-pin PIC16CXXX and PIC16FXXX microcontrollers
  • Program Memory 14.3K bytes
  • 8192 Single word instructions
  • Data SRAM 368 bytes
  • EEPROM 256 bytes
  • Input / Output 33 pins
  • 10-bit analogue to digital 8 channels
Peripheral Features:
  •  Timer0: 8-bit timer/counter with 8-bit prescaler
  • ​Timer1: 16-bit timer/counter with prescaler, can be incremented during Sleep via external crystal/clock
  • Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
  • ​Two Capture, Compare, PWM modules
    • Capture is 16-bit, max. resolution is 12.5 ns
    • Compare is 16-bit, max. resolution is 200 ns
    • PWM max. resolution is 10-bit
  • Synchronous Serial Port (SSP) with SPI (Master mode) and I2C™ (Master/Slave)
  • Universal Synchronous Asynchronous Receiver Transmitter (USART/SCI) with 9-bit address detection
  • Parallel Slave Port (PSP) – 8 bits wide with external RD, WR and CS controls (40/44-pin only)
  • Brown-out detection circuitry for Brown-out Reset (BOR)
Analog Features:
  • • 10-bit, up to 8-channel Analog-to-Digital Converter (A/D)
  • Brown-out Reset (BOR)
  • Analog Comparator module with:
    • Two analog comparators
    • Programmable on-chip voltage reference (VREF) module
  • Programmable input multiplexing from device inputs and internal voltage reference
  • Comparator outputs are externally accessible
Special Microcontroller Features:
  • • 100,000 erase/write cycle Enhanced Flash program memory typical
  • 1,000,000 erase/write cycle Data EEPROM memory typical
  • Data EEPROM Retention > 40 years
  • Self-reprogrammable under software control
  • In-Circuit Serial Programming™ (ICSP™) via two pins
  • Single-supply 5V In-Circuit Serial Programming
  • Watchdog Timer (WDT) with its own on-chip RC oscillator for reliable operation
  • Programmable code protection
  • Power saving Sleep mode
  • Selectable oscillator options
  • In-Circuit Debug (ICD) via two pins
CMOS Technology:
  • Low-power, high-speed Flash/EEPROM technology
  • Fully static design
  • Wide operating voltage range (2.0V to 5.5V)
  • Commercial and Industrial temperature ranges
  • Low-power consumption

 

More From Iamtechnical.com

Advertisement:

SiteLock