# Help needed for programming PIC16F877A...

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Hi,

what is wrong in this code showing error in Map lab..??

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);

#define _XTAL_FREQ 4000000

void main (void)

{
TRISB=0b00000000;
PORTB=0;
int=i;

for(i=1,i++,i<5)
{
PORTB = 0x1;
__delay_ms(1000);
PORTB = 0x2;
__delay_ms(1000);

}
}

#### tshuck

Joined Oct 18, 2012
3,534
what is wrong in this code showing error in Map lab..??
What the heck is Map lab? You mean MPLAB?

Look at the errors you are getting, it will point you in the right direction, most of the time. Like now, for instance, your for loop requires semi-colons... I see no semi-colons... This is also not the way a for loop works...

Look at this.

you should also add a super-loop(a while(1) ), to put your blink section in....

#### Ian Rogers

Joined Dec 12, 2012
1,024

#### spinnaker

Joined Oct 29, 2009
7,835
Ritesh,

Please study a good C tutorial. Your error is a very basic mistake. You have asked these kinds of questions before. Any first week C programmer should have been able to find it.

It also helps if you mention the error and where it is occurring. It is very obvious because you only have a few lines but had there been very difficult to find with no information and lots of lines of code.

First learn to help yourself. When you can no longer help yourself then learn to help others to help you.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Now its working......

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);

#define _XTAL_FREQ 4000000

void main (void)

{
TRISB=0b00000000;
PORTB=0;
int i;

for(i=1;i<5;i++)
{
PORTB = 0x1;
asm("nop");
PORTB = 0x2;
asm("nop");

}
}

#### spinnaker

Joined Oct 29, 2009
7,835
Amazing what can happen when you have the right syntax.

#### t06afre

Joined May 11, 2009
5,934
Ritesh at this point you should know a thing or two about micro controller programming.
First your main should always contain an endless loop something like this. (And I hope you agree that it should not be needed to mention this again)
Rich (BB code):
main()
{
while (1)
{
// you code here
}
}
So to simply blink LEDs your first program was not a bad try. But I doubt your second will show any visible blinking. Here is how I would have made your LED blink program
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);

#define _XTAL_FREQ 4000000

void main (void)

{
TRISB=0b00000000;
PORTB=0;
while(1)
{

PORTB = 0x1;
__delay_ms(1000);
PORTB = 0x2;
__delay_ms(1000);
}
}

#### nerdegutta

Joined Dec 15, 2009
2,684
I guess this program is a nice way to test out the:

Debugger -> Select Tool -> MPLAB SIM

and

View -> Watch -> Add SFR -> PortB

and

Debugger -> Animate

and see what is happening...

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
View -> Watch -> Add SFR -> PortB

and

Debugger -> Animate
nothing happen while adding this it was running only as before..

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
This code is working do u think it will work in hardware?

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);

#define _XTAL_FREQ 4000000

void main (void)

{
TRISB=0b10000001;
PORTB=0b10000001;
if(RB0=1)

{
PORTB = 0x1;
asm("nop");
}

else{
PORTB = 0x2;
asm("nop");

}
}

#### nerdegutta

Joined Dec 15, 2009
2,684
What are you trying to do? What do you want to happen?

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
I was saying if i add switch in hardware connected to portb0 will it work as per programming?

#### nerdegutta

Joined Dec 15, 2009
2,684
No.

Where is the
Rich (BB code):
while (1)
{
// some if- statements or case - switch
}
?

See earlier post in this thread.

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
No.

Where is the
Rich (BB code):
while (1)
{
// some if- statements or case - switch
}
?

See earlier post in this thread.
OK, If i will not use while then program will run for once,right??

and in this output of i is not commong in portA why?? portB is working..

Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);

#define _XTAL_FREQ 4000000
#define    i        RA0
#define    j        RB1

void main (void)

{
TRISB=0b00000000;
TRISA=0b00000000;

int i,j;{

for(i=0;i<5;i++)
{PORTA = i;

{for
(j=0;j<4;j++)
{

PORTB = j;

}
}

}
}
}

#### nerdegutta

Joined Dec 15, 2009
2,684
Please explain in words what you want to do, preferably followed with a schematic.

Now you are defining i as RA0 and j as RB1

Later you declare them i and j as int.

What's going on?

Without a while, the program will run only once, but what is the point with that?

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Now you are defining i as RA0 and j as RB1

Later you declare them i and j as int.

What's going on?
Please forgot the first fo a while..

Is there any use of it in 877A as i can't find this register in data sheet...
CMCON = 0x07 ; // Disable analog comparators

Last edited:

#### t06afre

Joined May 11, 2009
5,934
I got the bug, but how to off analog in portA i am writhing this........

ADCON0=0b00000000;
but not working
Rithesh are you very rude or just darn stupid We are using our time to try to help you. But NOTHING seems to sink in. Can you explain why you avoid all our advices??
NO your program will NOT run only once if you do not put in some endless loop. The controller will execute the assembler NOP instruction until it reach the end of program memory. Then the program counter will roll over and start to execute the instructions at address 0x0000.
Second, I guess you want to see some LEDs blink in your program. The human eye is quite slow. So in order to see any changes you must slow you program down. Use delays from 100msec and up between each change. inserting a NOP will not help in anyway. Your LEDs will appear to either be full on or full off.

ADCON0=0b00000000;
NO that will not work. 0x00 is wrong. READ the datasheet. Since you ignore everything we say. I do not bother to tell you what the correct value should be.

#### spinnaker

Joined Oct 29, 2009
7,835
No. Where is your while loop?

#### Ian Rogers

Joined Dec 12, 2012
1,024
Please forgot the first fo a while..

Is there any use of it in 877A as i can't find this register in data sheet...
CMCON = 0x07 ; // Disable analog comparators
No.... Like I said previously.... The code from my tutorials was written on the pic16f628a.... We have been through this quite a few times..

The pic16f877a hasn't got a compatator but HAS adc's

You need to sort your superloop as spinnaker said... AND read the data sheet to see what peripherals to configure...

Thread Starter

#### RRITESH KAKKAR

Joined Jun 29, 2010
2,829
Rithesh are you very rude or just darn stupid We are using our time to try to help you. But NOTHING seems to sink in. Can you explain why you avoid all our advices??
NO your program will NOT run only once if you do not put in some endless loop. The controller will execute the assembler NOP instruction until it reach the end of program memory. Then the program counter will roll over and start to execute the instructions at address 0x0000.
Second, I guess you want to see some LEDs blink in your program. The human eye is quite slow. So in order to see any changes you must slow you program down. Use delays from 100msec and up between each change. inserting a NOP will not help in anyway. Your LEDs will appear to either be full on or full off.
No. Where is your while loop?
I remember I am just learning but testing in MPlab only after this i will add while() delay when i will come to hardware..

Similar threads