PIC16F877A LED blink problem

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
aashishpic
Posts: 3
Joined: Wed May 07, 2014 12:48 am

PIC16F877A LED blink problem

Post by aashishpic » Sat Jun 14, 2014 11:09 am

I am a pure beginner to PIC microcontrollers and want to Blink a LED with it (as my first test).

But at the end the LED did not blink, I performed the following steps,

1. Generate Hex file from MPLAB X IDE v2.05
2. Flash the PIC16F877A with this hex file using a PIC programmer.
3. Connect a LED to PIC16F877A on breadboard to test my work

Please have a look to the detailed steps of the work..........
https://drive.google.com/file/d/0B5NYQC ... sp=sharing

My code is as (not that given in above link),

Code: Select all

#include <xc.h>

void delay( unsigned int );

void main()
{
	unsigned int DELAY = 100;

	TRISC=0x00;

	while(1)
	{
		PORTC = 0x00;		 //led off
	    delay(DELAY);

		PORTC = 0x01;		 //led on
		delay(DELAY);
	}
}

//DELAY FUNCTION ( mS )
void delay( unsigned int time )
{
    unsigned int i;
	  unsigned int j;
		for( i=0; i<time; i++ )
		for( j=0; j<1275; j++ ) {;}
}
and I used the circuit diagram as,

Image

But at the end I could not get the LED blinking.
So where I was wrong? Does the PIC16F877A not have an on chip clock? Whether I have to connect an external crystal?? Or my program is incorrect/incomplete?? Or anything I am missing….??

Please help me I am in a great need of it. Please help.

Thanks in advance.

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: PIC16F877A LED blink problem

Post by waltr » Mon Jun 16, 2014 8:17 am

Re-read the data sheet for the 16F877. It does not have an on-board oscillator therefore you must connect an XTAL or oscillator for the PIC to run.

Where are the CONFIG bots defined in your code?

Where are the 100nF de-coupling caps on the Vdd to Vss pins?? These are required.
Why is MCLR pin floating??? This must be at a logic high for the PIC to run. Connect to Vdd with a 10k Ohm resistor.

Go back to Microchip's web site and look up any of the App Notes that use these series PIC and study the schematis.
Also, this is a very popular PIC for hobbyist so there is lots and lots of info and example circuits for using this PIC. Search and study.

aashishpic
Posts: 3
Joined: Wed May 07, 2014 12:48 am

Re: PIC16F877A LED blink problem

Post by aashishpic » Tue Jun 17, 2014 1:09 am

Lots of thanks for your reply :) , I understand all your stuff except configuration bits :?: .

I am a beginner to pic microcontrollers and couldn't understand the configuration bits.

Please suggest me some in depth tutorial/stuff on configuration bits and with examples how to use them.
If the tutorial explains it from groundup then it will be very beneficial.

Can you please send me a example configuration word that i can use to get my basic LED blink program, I have a 12 MHz crystal, I got __CONFIG(1,0x0F24); __CONFIG(2,0X0000); in a tutorial : http://extremeelectronics.co.in/microch ... r-part-ii/

I got one more tutorial : http://saeedsolutions.blogspot.in/2012/ ... oteus.html
they set #define _XTAL_FREQ 200000000 do I need it ?

Will the configuration bits used in these tutorials be okay for my simple LED blink test??

Please reply sooooooooooon.........

Thanks in advance.

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: PIC16F877A LED blink problem

Post by waltr » Tue Jun 17, 2014 8:56 am

The easiest way is for you to learn about the CONFIG bits to go to the Microchip forum and look at posted code using the XC compiler.
I never use the raw hex values to define the CONFIG bits. Instead use the defines in the XC header files that are human readable.

Here is a good tutorial on learning how to use and program PICs:
http://www.gooligum.com.au/tutorials.html

aashishpic
Posts: 3
Joined: Wed May 07, 2014 12:48 am

Re: PIC16F877A LED blink problem

Post by aashishpic » Tue Jun 17, 2014 10:52 pm

Lots of thanks sir, I modified my circuit and code according to you as,

Code: Select all

#include <xc.h>

// Configuration word for PIC16F877
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF);

void delay( unsigned int );

void main()
{
	unsigned int DELAY = 100;

        TRISD=0x00;
        PORTD = 0x00;
        
	while(1)
	{
		PORTD = 0x00;		 //led off
	        delay(DELAY);

		PORTD = 0x01;		 //led on
		delay(DELAY);
	}
}

//DELAY FUNCTION ( mS )
void delay( unsigned int time )
{
    unsigned int i;
	  unsigned int j;
		for( i=0; i<time; i++ )
		for( j=0; j<1275; j++ ) {;}
}
and my modified circuit is,
new ckt.png
new modified circuit
But...Huh........bad luck, again I did not get the LED even turned ON.

Please help me.

Waiting for your reply...........
Thanks in advance.

waltr
Support Volunteer
Posts: 2823
Joined: Tue Sep 08, 2009 12:07 pm
Location: Philadelphia, USA

Re: PIC16F877A LED blink problem

Post by waltr » Wed Jun 18, 2014 7:40 am

Schematic looks better. Is the 100nF cap as close as possible to the Vdd/Vss pins? there should be a cap on each Vdd/Vss pair.

Does the XTAL oscillate?

For the code:
CONFIG bits look ok.

Now test and debug the code without the hardware by running MPLAB's SIMulator.
Step through the code and ensure every PIC register is set properly.
Use the Stopwatch feature and time the Delay function.

Post Reply