
Site Search
Microchip PIC18F452 LED's Blinking Code Using ' C ' Language

Microchip PIC18F452 LED's Blinking Code Using ' C ' Language:
Blinking LED Time Delay Code:
#include<htc.h>
// PIC 18F452 fuse configuration:
// Config word 1 (Oscillator configuration)
// 40Mhz crystal input
__CONFIG(1, OSCSDIS & HSPLL);
// Config word 2
__CONFIG(2, BORDIS & PWRTDIS & WDTDIS);
// Config word 3
__CONFIG(3, CCP2RC1);
// Config word 4
__CONFIG(4, DEBUGEN & LVPEN & STVREN);
// Config word 5, 6 and 7 (protection configuration)
__CONFIG(5, UNPROTECT);
__CONFIG(6, WRTEN);
__CONFIG(7, TRU);
#define _XTAL_FREQ 40000000 //MHz
void delay_sec(unsigned char seconds) // This function provides delay in terms of seconds
{
unsigned char i,j;
for(i=0;i<seconds;i++)
for(j=0;j<50;j++)
__delay_ms(10);
}
void main()
{
RB0 = 0; // Make RB0 zero
TRISB0 = 0; // Make RB0 output
while(1)
{
RB0 = 1; // Make RB0 one
delay_sec(1); // delay of one second
RB0 = 0; // Make RB0 zero
delay_sec(1); // delay of one second
}
}
-------------------------------------------------------------------------------------------------------------------------------
Note: The code was compiled in MPLAB X IDE Version 2.35 with Compiler HI-TECH C for PIC18 Version 9.8 and simulation was made in Proteus version 8.0
Description & Working Procedure:
In this circuit a crystal of 10MHz is used with PIC18F452. This crystal frequency is fed into PLL of PIC18F452, which boosts it to 40MHz. As we know that any PIC microcontroller has an architecture which executes an instruction in 4 CPU cycles, hence this 10Mhz crystal + PLL [Phase Locked Loop] makes this PIC run at 10MIPS (Million of instructions per second). The code for this circuit is shown above.
The header file htc.h is always required whenever you are compiling the code in MPLAB using HI-TECH C compiler. After the header file, configuration word is being programmed. That crystal frequency is being defined as 40Mhz [ which is basically the output of Phase Locked Loop PLL ]. This definition of crystal frequency is used in the built in function of __delay_ms [ used in delay_sec function [ 'delay_sec' function provides the delay in seconds. For example for one second delay just write delay_sec(1).
In the main function, RB0 pin is defined to be an output using the TRISB0 = 0 statement. In the while loop RB0 is toggled continuously. RB0 is attached to the LED's as can be seen in the animated circuit above. Hence we have a blinking LED's.
Features of PIC18F452, 40-pin High Performance, Enhanced FLASH Microcontrollers with 10-Bit Analog to Digital Convertor:
High Performance RISC CPU: |
---|
|
Peripheral Features: |
---|
|
Analog Features: |
---|
|
Special Microcontroller Features: |
---|
|
CMOS Technology: |
---|
|