Types of delay in C language and changes in MPLAB X XC8

Discussion in 'Programmer's Corner' started by umibuta, May 5, 2014.

  1. umibuta

    Thread Starter New Member

    Dec 4, 2010
    8
    0
    Hi,

    I am a beginner learning C language for PIC. I am using MPLAB X with XC8 on OSX. My question is I was reading up about C language from books and online tutorials and I came across a few discrepancies which I cannot find answers to.

    1) Are there different instructions for delay? For example I came across Delay10KTCYx(), delay ms() and __delay_ms()

    What's the difference? And are they due to the different compiler's language i.e XC8 , C18 etc? Or are they one and the same and I can use in XC 8?

    2) Some tutorials tell you to specify your PIC in the header with

    #include <xc.h> or #include <p18f452.h> or #include <htc.h>

    Again what's the difference?

    What should a beginner take note of when programming? It is complicated enough knowing there is an assembler language and High level language.
    I opted to just jump straight into C language and I realised there are differences in the compiler that you used.
    I would appreciate if someone give me some advise on learning C language for picf16 and picf18.
    I bought Tim Wilmshurst "Designing Embedded Systems with PIC Microcontrollers"
    to learn PIC. Is it outdated already?

    Thanks in advance.
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    All C compilers are by far not equal. They all differ somewhat. You are using the XC8 compiler. And this document should get you started http://ww1.microchip.com/downloads/en/DeviceDoc/50002173A.pdf Remeber ALWAYS create a project for your code DO NOT skip this part.
    As for the delay function the correct for XC8 is to use __delay_ms(), or __delay_us(). Then in doubt I recommend use the manual(PDF file). It should be inn the docs
    section of the compiler install folder.
    The book you are using is written for C18 compiler and this differ some from the XC8 compiler. Although the big lines are much the same. Anyway the best way to learn C is to use it.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    The difference you see are not because of the individual C compilers themselves. It is due the support libraries created by different people at different times under no standard whatever.

    So they do similar things in very different ways.

    The moral of the story is to stick to documents written for your specific compiler for low lever tasks such as delays and hardware manipulation.

    More generally written documents are useful for more general tasks, such as copying memory from A to B.

    But even a mem copy can be compiler specific... such as in PIC18 where you may be copying from ROM to RAM. So start with documents for your compiler.
     
  4. umibuta

    Thread Starter New Member

    Dec 4, 2010
    8
    0
    Thank for the replies. At least I can confirm there are different instruction sets for the different compilers. While searching for an answer I have always seen advice to refer to the PIC specification sheet or XC 8 manual. Without sounding rude I am not sure, what I should refer to. (For this instance, I did learn that <xc.h> includes files for all PIC and that I need to set the configurations)

    Is there an instruction set (like they have for assembler language) specifically for XC 8? I agree that the best way to learn is to use it. Just wanting to increase my XC 8 vocabulary so I can learn by trial and error.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
Loading...