Maker Pro
Maker Pro

C programming on PIC16F877A

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
vick, I was doing some digging today and found out that you can simulate C code in proteus isis. It actually works out pretty well.

Have a look at this tutorial here. http://smainj.free.fr/tutorial/
Tutorial1.rar contains the flash files, video you can watch in your browser. 7mb's

I think being able to simulate it on the PC makes it a lot easier to make mistakes without wasting time programming the chip and rewiring the hardware. Thought you might like it, maybe you like your way better.

Hey great help man.

thank you :)
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
Code:
#include <pic.h>

__CONFIG ( 0x3F32 );

#define LED_1 RA0
#define LED_2 RA1
#define LED_3 RD2
#define LED_4 RA3
#define LED_5 RC0
#define LED_6 RA5
#define LED_7 RC2
#define LED_8 RC3

void init(void);
void delay(unsigned long data);

void main(void)
{

init();
for(int j=0;j<3;j++)
{
LED_1=0;
LED_2=0;
LED_3=0;
LED_4=0;
LED_5=0;
LED_6=0;
LED_7=0;
LED_8=0;
delay(8000);
LED_1=1;
delay(8000);
LED_1=0;
LED_2=1;
delay(8000);
LED_2=0;
LED_3=1;
delay(8000);
LED_3=0;
LED_4=1;
delay(8000);
LED_4=0;
LED_5=1;
delay(8000);
LED_5=0;
LED_6=1;
delay(8000);
LED_6=0;
LED_7=1;
delay(8000);
LED_7=0;
LED_8=1;
delay(8000);

LED_1=0;
LED_2=0;
LED_3=0;
LED_4=0;
LED_5=0;
LED_6=0;
LED_7=0;
LED_8=1;
delay(8000);
LED_8=0;
LED_7=1;
delay(8000);
LED_7=0;
LED_6=1;
delay(8000);
LED_6=0;
LED_5=1;
delay(8000);
LED_5=0;
LED_4=1;
delay(8000);
LED_4=0;
LED_3=1;
delay(8000);
LED_3=0;
LED_2=1;
delay(8000);
LED_2=0;
LED_1=1;
delay(8000);
LED_1=0;
delay(8000);


}

}

void init()
{
TRISA=0b00000000;
TRISB=0b00000000;
TRISC=0b00000000;
TRISD=0b00000000;
TRISE=0b00000000;
}

void delay(unsigned long data) //delay function, the delay time
{
for( ;data>0;data-=1); //depend on the given value
}
This is my code and it build successfully in MPLAB IDE..but when I wan to load it into my PIC16F877A, it always show that verification of configuration failed...

any mistake that I have done ?


Thank you for the help :)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
How did you arrive at the configuration word

__CONFIG ( 0x3F32 );

It is possible you've got a wrong value in there.
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
How did you arrive at the configuration word

__CONFIG ( 0x3F32 );

It is possible you've got a wrong value in there.

0x3F32 means what and what changes should I make ? but before that it is ok but I CHANGED the version of Hi-tech compiler version then it cant work already.

So what should I do?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You need to check the documentation for the PIC to determine what this configuration word means.

They mean different things on different PICs, so what works for one type of PIC may not work for another.

It does things like select the clock source, whether code is protected, whether the watchdog timer is enabled, etc., etc. See here for some basics.
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
You need to check the documentation for the PIC to determine what this configuration word means.

They mean different things on different PICs, so what works for one type of PIC may not work for another.

It does things like select the clock source, whether code is protected, whether the watchdog timer is enabled, etc., etc. See here for some basics.

I am using PIC16F877A, so can you tell me what CONFIG words that should I change to ? I have a small presentation tomorrow :(
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You should change this config word:

__CONFIG ( 0x3F32 );

And you should change it based on what you read in the documentation for the PIC16F877A so that it behaves the way you want it to.

I cannot know your clock source or desired clock speed, or any of the other things that can be set in the config word.
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
You should change this config word:

__CONFIG ( 0x3F32 );

And you should change it based on what you read in the documentation for the PIC16F877A so that it behaves the way you want it to.

I cannot know your clock source or desired clock speed, or any of the other things that can be set in the config word.

I used a 20MHz crystal oscillator :)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
OK, so go find the datasheet for the 16G877A and determine what bits need setting, convert that to hex and that is the configuration word you require.

Didn't you even write a simple hello world program on your PIC? Did you use a simulator for everything? You may be learning a lesson in the limitations of simulation.

It sounds like a school project, so I'm not going to do all your work for you.
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
OK, so go find the datasheet for the 16G877A and determine what bits need setting, convert that to hex and that is the configuration word you require.

Didn't you even write a simple hello world program on your PIC? Did you use a simulator for everything? You may be learning a lesson in the limitations of simulation.

It sounds like a school project, so I'm not going to do all your work for you.

This is actually my own projects..My Uni maybe havent teach us about this..I discover it myself :)
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
OK, so go find the datasheet for the 16G877A and determine what bits need setting, convert that to hex and that is the configuration word you require.

Didn't you even write a simple hello world program on your PIC? Did you use a simulator for everything? You may be learning a lesson in the limitations of simulation.

It sounds like a school project, so I'm not going to do all your work for you.

where can I get a better simulator ? I do not have one :(
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You clearly need the real world now.

You have to hand in a real programmed PIC, not a simulation, right?
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
You clearly need the real world now.

You have to hand in a real programmed PIC, not a simulation, right?

Yes I have the real board and programmer now..But simulation software do help me in understanding right ?

So in datasheet what and where should I look for the things I need ?

I really need a guide in reading the datasheet :(
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
0x41ED this is the code the make it fine..When I converted this number to decimal it become 16877, So it matches the PIC I used..

Is this correct ? Just to make sure I didnt get the wrong things :)
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
I hope you went through the bits and decided what value you wanted then later discovered that it happened (by fluke) to be 16877 in decimal, because the config word and the name of the device have no relationship like that.

And please learn to Google.

Look here
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
I hope you went through the bits and decided what value you wanted then later discovered that it happened (by fluke) to be 16877 in decimal, because the config word and the name of the device have no relationship like that.

And please learn to Google.

Look here

The number in what format ? binary ?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
It's just a number. You've been using hex, but most of the descriptions of what the bits mean are obviously in binary.

You can convert from one to the other, right?
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
You can express the number in any format that both you and the compiler agree on.

You have been using hex, so why not continue?

Preferably you would use symbolic constants, but I think that's too big an ask.
 

vick5821

Jan 22, 2012
700
Joined
Jan 22, 2012
Messages
700
You can express the number in any format that both you and the compiler agree on.

You have been using hex, so why not continue?

Preferably you would use symbolic constants, but I think that's too big an ask.

But I do not whether what to choose for each bit ? either power timer on or off ? the brown set timer and so on..where can I read about the function of each bits?
 
Top