Begineer to PIC 16F877A

Discussion in 'Embedded Systems and Microcontrollers' started by wilsonz91, Jun 9, 2011.

  1. wilsonz91

    Thread Starter New Member

    Jun 9, 2011
    3
    0
    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.
     
  2. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
  3. RiJoRI

    Well-Known Member

    Aug 15, 2007
    536
    26
    #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!
     
    wilsonz91 likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    #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.
     
    wilsonz91 likes this.
  5. Tang

    New Member

    Jun 11, 2011
    2
    1
    you can buy one EVM board, and there are many testing programe codes you can try it. it's easy to beginner.
     
    wilsonz91 likes this.
  6. wilsonz91

    Thread Starter New Member

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

    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?
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Seriously?

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

    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).
     
    wilsonz91 likes this.
  8. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
    wilsonz91 likes this.
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    wilsonz91 likes this.
  10. wilsonz91

    Thread Starter New Member

    Jun 9, 2011
    3
    0
    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.
     
  11. stahta01

    Member

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

    Code ( (Unknown Language)):
    1.  
    2. sbit LED1 at RA0_bit;
    3.  
    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.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In HI-Tech C you are allowed to do bit manipulation of the SFR registers. Like done in your code
    Code ( (Unknown Language)):
    1. 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.
    Code ( (Unknown Language)):
    1.  
    2. #define bitset(var, bitno) ((var) |= 1UL << (bitno))
    3. #define bitclr(var, bitno) ((var) &= ~(1UL << (bitno)))
    4. #define toggle_bit(var,bitno) (var ^= (1UL << bitno))
    5. #define test_bit(var,bitno) (var & (1UL << bitno))
    6.  
     
    Last edited: Jun 23, 2011
Loading...