Begineer to PIC 16F877A

Thread Starter

wilsonz91

Joined Jun 9, 2011
3
Hi, i'm new to PIC, hope you can help me here. I'm using a 16F877A microchip

1) I'm using mikroC, and my program starts as follow:
#include <pic.h>

#define pb1 RA0

void main()
{
......
}

Problem i'm having is when i build, the error comes out saying "cannot open include file pic.h. I downloaded a header file 16F877A.h and pasted it in Mikroelectrica>include, then I replace the program header with 16F877A.h but it comes out a very long error.
What am I suppose to do?

2) I don't understand the following code I saw from a sample:
PORTA = 0b000000;
Is it the same as PORTA = 0;

3) How does using a 8Mhz or a 20Mhz oscillator affects the PIC?

4) I read from a training book that switches and LED should be connected to a resistor or else it would short, is that true? cause i was told that 16F877A has internal resistors.

Thank you for helping.
 

RiJoRI

Joined Aug 15, 2007
536
#2 -- Yes, they are the same. The reason to use the 0b00000000 form is to make it easier to see which bits are being set/cleared. E.g., with 0b10111111 it is easier to see bit 6 is being cleared. With 0xBF you would need to convert to binary anyway. (Some ancient computers only had decimal representation. What does "OUT 2,191" do???)

#3 -- Assuming your PIC can handle the 20MHz*, the code will run faster, as will all the timers. We just went through this at work using a Nat Semi HPC chip. We had to double the clock speed to get the code to run faster. The code base is 20+ years old, and was loaded with "magic numbers"! ( "LD T1,#0FED") Arrrgh!

--Rich

* I'm of the 3.456 MHz era. 8 MHz still feels very fast to me!
 

ErnieM

Joined Apr 24, 2011
8,415
#1) What to do? Drop back and punt: see if they have any examples and get them to build so you can see what they need. It looks like you have a path problem. That's a rather obscure compiler (one I could never figure out a business model that would require it), but Mikroelectrica does have a forum where you might get some help.

#2) Yeah what RJoRI said. 0b stars a binary number, 0x a hex number.

3#) That chip can handle 20MHz quite nicely, as all Microchip products are conservatively spec'd and run to their maximums (and a little beyond too). You will see some cores have a built in 4 or 8 MHz oscillator so you can save a few pins and a crystal.
 

Thread Starter

wilsonz91

Joined Jun 9, 2011
3
Thank you for all your replies.

#1) What to do? Drop back and punt: see if they have any examples and get them to build so you can see what they need. It looks like you have a path problem. That's a rather obscure compiler (one I could never figure out a business model that would require it), but Mikroelectrica does have a forum where you might get some help.
what other compilers would you suggest? I tried using codes provided by mikroC, the examples doesn't use any #include at all. Is it necessary for me to include it if i'm just doing basic functions such as push a button to turn on a LED?
 

ErnieM

Joined Apr 24, 2011
8,415
I tried using codes provided by mikroC, the examples doesn't use any #include at all.
Seriously?

Why not go back and read your first post. It goes like this

1) I'm using mikroC, and my program starts as follow:
#include <pic.h>
...
what other compilers would you suggest?
Sourceboost makes some with free evaluation versions.
Microchip has great free evaluation versions for 18 and up, and I believe the HiTech one works with lower devices (but I use BoostC for those).
 

AlexR

Joined Jan 16, 2008
732
Include files are very much compiler specific and generally a file written for one compiler will throw up a heap of errors if run on a different compiler. Files such as pic.h usually contain information about where to find information about PIC register and register bit names and locations.
I do not use MicroC but from what I can gather it has the information that other compiler keep in header files as part of the MicroC IDE setup. All you should need to do is tell the IDE which chip you are using and it will take care of adding any headers information that it needs.
 

Thread Starter

wilsonz91

Joined Jun 9, 2011
3
Thank you all for your reply, I manage to get it to work. However I face another problem now. As seen on my earlier I used #define pb1 RA0. This seems to be valid, but the problem is when it is used in the main program. Following is the code:

#define LED1 RA0;

void main()
{
PORTA = 0b000000;
TRISA = 0b000000;

while(1){
LED1 = 1;
delay_ms(1000);
LED1 = 0;
}
}

Error pops up(I'm using mikroC). The problem is with LED1 = 1. The code sample i received from my friend is done this way. What is the right way to do it? Thank you.
 

stahta01

Joined Jun 9, 2011
133
From glancing at the manual; I think this is wrong.
http://www.mikroe.com/eng/downloads/get/30/mikroc_pic_pro_manual_v101.pdf
Rich (BB code):
#define LED1 RA0;
Try this; you really need to read the directions of YOUR compiler.

Rich (BB code):
sbit LED1 at RA0_bit;
Edit: The main difference is the use of "RA0_bit" instead of "RA0"
Note: This is just an educated guess.
FYI: Using an ";" at the end of an "#define" is wrong 99.9999% of the time.

Tim S.
 

t06afre

Joined May 11, 2009
5,934
In HI-Tech C you are allowed to do bit manipulation of the SFR registers. Like done in your code
Rich (BB code):
RA0=1;
That is not posible in the C compiler you are working with. You can try using these macros instead. Most PIC C compilers recognize these macros and produce compact code.
Rich (BB code):
#define bitset(var, bitno) ((var) |= 1UL << (bitno))
#define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
#define toggle_bit(var,bitno) (var ^= (1UL << bitno))
#define test_bit(var,bitno) (var & (1UL << bitno))
 
Last edited:
Top