
Site Search
Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 600, 800, & 1 Second



Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 600 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(600); // 600 Millisecond delay
LED = 0; // LED off
__delay_ms(600); // 600 Millisecond delay
LED = 1; // LED on
}
}
Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 800 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(800); // 800 Millisecond delay
LED = 0; // LED off
__delay_ms(800); // 800 Millisecond delay
LED = 1; // LED on
}
}
Microchip PIC16F877A, Three LED's Blinking, Code Using ' C ' Language, Time Delay 1 Second:
/* 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(1000); // 1000 Millisecond or 1 Second delay
LED = 0; // LED off
__delay_ms(1000); // 1000 Millisecond or 1 Second delay
LED = 1; // LED on
}
}
- 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 600, 800 and 1000 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 600ms, 800ms and 1000ms 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: |
---|
|
Peripheral Features: |
---|
|
Analog Features: |
---|
|
Special Microcontroller Features: |
---|
|
CMOS Technology: |
---|
|