Tuesday, August 2, 2011

PIC Programs 01: LED blinking


When we start using a particular micro controller,most probably the first program which we may write will be the one to blink an LED. So here is a program for blinking an led every second using PIC16F877A. The program is inended for HI-TECH compiler.  

This program program will blink an LED connected to any of the pins in PORT B. It is recomented that you initially setup an essential PIC circuit before connecting an LED to the PIC. It is also recomented that you place a 1K resister in series with the LED to limit the current drawn from the PIC.



#include <htc.h>

#define _XTAL_FREQ 12000000 //setting the crystal frequency to 12MHz

void main()
{

PORTB = 0xff; //Initialising the port.
TRISB = 0x00; //Sets port B as an output port

while(1)
{

PORTB=0xff; //Clears all PORTB pins (LOW)
__delay_ms(100); //Hold for 100ms
PORTB=0x00; //Sets all PORTB pins (HIGH)
__delay_ms(900); //Hold for 900ms
}
}




1 comments:

Anil said...

For proper functioning set the configuration bits to 3F7A.

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes