Google

Feb 17, 2010

LED blinking program for Pic microcontroller

Here is the program for Pic microcontroller PIC16F877A
to blink a LED connected to B3 of PORTC,

This program works for Mplab. or using HiTECH PRO Lite compiler.


PICClite code may be

#include
#include "delay.h"
#define _XTAL_FREQ 4000000
_CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);

void main()

{
TRISC=0;
while (1)
{
PORTC = 8;
DelayMs(250);DelayMs(250);
PORTC = 0;
DelayMs(250);DelayMs(250);
}

}

in PIC_Clite , "delay.h" has to be included because its MACRO, but in PRO its inbuilt

#include
#define _XTAL_FREQ 4000000
_CONFIG(MCLREN & UNPROTECT & BORDIS & WDTDIS & PWRTEN & INTIO);

void main()

{
TRISC=0;
while (1)
{
PORTC = 8;
__delay_ms(100);_delay_ms(100); // use this delay to test the code
__delay_ms(100);_delay_ms(100);
PORTC = 0;
__delay_ms(100);_delay_ms(100);
__delay_ms(100);_delay_ms(100);
}

}
i didnt use _delay(100000); because whn Xtal Freq is high(20Mz) the delay changes & you will not be able to see the LEDs blinking. ensure that watchdog timer is disabled in _CONFIG, hope it works..

No comments: