PIC16F877A LED Blinking Program

Thread Starter

ActivePower

Joined Mar 15, 2012
155
I am a complete noob in programming a PIC microcontroller and I have been trying to get the most basic of the tasks (flashing an LED) done.

I am using MPLAB v8.73 with HiTech C Compiler and a PIC16F877A

I found an circuit online (http://justanotherlanguage.org/sites/default/files/blink_a_led_circuit.jpg) and built the circuit with a few modifications in the component values.

The code is as follows:

Rich (BB code):
#include<pic.h>
#define _XTAL_FREQ 20000000

void main()
{
     TRISB=0;
     for (;;)
     {
        RB0=1;
        __delay_ms(250);
        RB0=0;
        __delay_ms(250);
     }
}
The code builds successfully but does not blink the LED :( Maybe there is a problem with the circuit but I wanted to be sure about the code first.

Thanks
 

takao21203

Joined Apr 28, 2012
3,702
Good work!

You need to modify the ANSEL register setting for this PIC model, if I guess right. Also configuration needs to be set correctly, including to select HS oscillator.

In the schematic the LED is connected to PORTA.
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
Hey, thanks! I had changed the LED to the RB0 position on the PIC. Does that make any difference? And I am using a 20 MHz crystal oscillator, so I included 'XT' in the config bits from the datasheet. And what is the syntax for changing the 'ANSEL', I am afraid I am unaware of that.

Thanks!
 

t06afre

Joined May 11, 2009
5,934
You have not set the config bits as far as I can see. The config bits are related to section 14.0 SPECIAL FEATURES OF THE CPU in the datasheet.
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
I set the config bits from the MPLAB IDE option 'Configuration Bits' and unchecked the 'Use configuration bits set in code' and selected the XT oscillator from the drop-down menu. Does that not count? I am sorry I do not know the details.
 

t06afre

Joined May 11, 2009
5,934
I set the config bits from the MPLAB IDE option 'Configuration Bits' and unchecked the 'Use configuration bits set in code' and selected the XT oscillator from the drop-down menu. Does that not count? I am sorry I do not know the details.
It is OK for a beginner to it that way. But it is poor for documentation. Anyway If you are using a 20MHz crystal. The setting should be HS and not XT
 

JohnInTX

Joined Jun 26, 2012
4,787
RP1/0 in STATUS have to select RAM bank 1 or 3 for TRISB and then change to 0 or 2 for PORTB. Step through your code in MPLAB with a watch window open to be sure that STATUS is correct when addressing the I/O. 'Watch' PORTB as well to see if the LSB is being set/cleared.

You should not have to in this case but instead of RB0 = 1, try writing (unsigned char) 1 and 0 to the port (LSB of char is RB0). Sometimes flipping individual bits on midrange PICs can be problematic.

As others have indicated, HS is the correct oscillator to use for 20MHz. HS drives the XTAL harder than XT.

You can indeed set the config in MPLAB but eventually it should be in your source code.

Finally, you can step in the Disassembly Output window in MPLAB to see what's going on on the code level.
 

t06afre

Joined May 11, 2009
5,934
RP1/0 in STATUS have to select RAM bank 1 or 3 for TRISB and then change to 0 or 2 for PORTB. Step through your code in MPLAB with a watch window open to be sure that STATUS is correct when addressing the I/O
The C compiler will take care of this
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
Here is the code after modifications:

Rich (BB code):
#include<pic.h>
#define _XTAL_FREQ 20000000

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF & DEBUG_ON & CPD_OFF & CP_OFF)

//Should I leave DEBUG ON? I have just a programmer and I do not know what it does apart from programming the PIC

void main()
{
    TRISB=0;
    for (;;)
    {
       RB0=1;
       __delay_ms(250);
       RB=0;
      __delay_ms(250);
    }
}
This builds successfully but would it run on the PIC?
Also, would it make any difference if I use RA pins or RB pins? The circuit is configured for RA but I wrote the code from the HiTech C Quickstart guide and it suggests RB pins.
On a slightly unrelated note, for my PIC circuit-related queries should I make a post here or in the Projects Forum?

Thanks!
 

t06afre

Joined May 11, 2009
5,934
If you do not have any debugger leave it off. I think if debugger specified the PIC will wait for debugger to connect before starting running the code
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
I do not have a separate debugger for the PIC. All I have is a JDM programmer and the WinPicProg software.

EDIT: It worked!! The LED blinks and it looks and feels awesome.. The only change I made was to replace the XT with HS. Funny how something as small as that could alter the final result. Although, I am going to change the delay time and check again just to know that it wasn't a fluke...will let you know what happens.. Thanks everyone for your help!
 

t06afre

Joined May 11, 2009
5,934
I do not have a separate debugger for the PIC. All I have is a JDM programmer and the WinPicProg software.

EDIT: It worked!! The LED blinks and it looks and feels awesome.. The only change I made was to replace the XT with HS. Funny how something as small as that could alter the final result. Although, I am going to change the delay time and check again just to know that it wasn't a fluke...will let you know what happens.. Thanks everyone for your help!
One small thing. Your program worked fine this time. But in the future replace
Rich (BB code):
#include<pic.h>
with
Rich (BB code):
#include <htc.h>
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
I am really very sorry to post in this thread as I know it is archived under 'Programmer's Corner' but I didn't feel like creating a new thread for a question which is more relevant to this post.

I got my LEDs blinking away last time around. However, when I tried to go for something more (http://forum.allaboutcircuits.com/showthread.php?t=72133), I had a few problems (http://forum.allaboutcircuits.com/showthread.php?goto=newpost&t=72145).

Then I rebuilt my LED blink circuit and it didn't work and now I am hopelessly stuck again. There is no problem with the code, I believe, for:

1. I took it from the HiTech C compiler Quickstart Guide.
2. It was running perfectly yesterday.

Here is the code anyway:

Rich (BB code):
//main.c

#include<htc.h>
#define _XTAL_FREQ 20000000

volatile unsigned char counter;			//value can be changed automatically during execution without modification through a function 

__CONFIG(FOSC_HS & BOREN_OFF & DEBUG_ON & WDTE_OFF & CP_OFF & CPD_OFF & LVP_ON & PWRTE_OFF);

void init()
{
	PIE1=0b00000001;						//Peripheral Interrupt Enable
	INTCON=0b01000000;							//Interrupt Control Register
	OPTION_REG=0b10000000;					//Pullup resistor enabled; active low bit ~RBPU
	T1CON=0b00110101;						//Timer 1 Control Register: Enable TMR1 using Internal Oscillator at FOSC/4
	TRISB=0x0;							//Configure as Output
}

void main(void)
{
	counter=0;
	init();			//initialize
	ei();			//enable interrupts

/* Always initialize interrupts first and enable them later */

	while(1)
	{
		PORTB=counter;
	}
}
and the ISR

Rich (BB code):
//IntFunc.c

#include<htc.h>

extern volatile char counter;		//var defined somewhere else but used here

void interrupt my_isr()				//associate the function with the Interrupt Vector using keyword
{
	if((TMR1IE)&(TMR1IF))			//if TIMER1 interrupt is enabled & TIMER1 is overflowing
	{
		counter++;
		TMR1IF=0;					//reset TMR1 interrupt flag
	}
}
Can anyone please help me troubleshoot the circuit/code?

Thanks!

EDIT:
1. The IC on the right is the L293D and I had stuck it in with great difficulty last night, so I did not actually want to remove it. Sorry for my laziness!
2. Also my protoboard isn't labelled so the uppermost row is the power rail (I haven't connected the battery pack yet) and the last but one is the GND.
 

Attachments

Last edited:

t06afre

Joined May 11, 2009
5,934
Have you done some debugging like placing some breakpoints or using the watch option. And/or singlestepping of the code? You can do this using the MPLAB simulator. Also have you gone back and tested the last working code
 

t06afre

Joined May 11, 2009
5,934
Rich (BB code):
__CONFIG(FOSC_HS & BOREN_OFF & DEBUG_ON & WDTE_OFF & CP_OFF & CPD_OFF & LVP_ON & PWRTE_OFF);
Here you have set LVP_ON I do not think the JDM programmer support this. Erase your chip compleatly and set LVP_OFF. Just a hunch though but the cost of doing it is quite low
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
Have you done some debugging like placing some breakpoints or using the watch option. And/or singlestepping of the code? You can do this using the MPLAB simulator. Also have you gone back and tested the last working code
As far as I know the code is fine. It was working perfectly yesterday. That is why I think the circuit may be the cause of the trouble.
 

Thread Starter

ActivePower

Joined Mar 15, 2012
155
Thanks for pointing that out! I corrected it in my program, although it still did not help. I have tested all my previous working codes. None of them work. I strongly suspect something is up with the circuitry. I used my 6 V battery pack to test a dc motor last night for a few minutes in which it became quite hot. Could that have drained it enough not to supply the activating voltage? (I am sorry if that seems impossible I am thinking about any and all possibilities here; anything to get the darn thing working)

EDIT: I spent some time with the circuit and my multimeter last night checking all loopy wires and stuff and after a while fiddling with the power connections, it worked! Apparently, the battery pack was super-drained out and could not maintain a constant 5V supply. I replaced the pack with a 9V cell and a 7805 and the LED is flashing away on my board as we speak.

Thanks everyone for your help!
 
Last edited:
Top