1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

XC8 and general C questions

Discussion in 'Microcontrollers' started by wannaBinventor, Jun 26, 2013.

  1. wannaBinventor

    wannaBinventor Member

    Joined:
    Jun 24, 2010
    Messages:
    214
    Likes:
    2
    Hey all,

    I've started learning C and have executed a simple program with my PicKit2 dev board(PIC16F887 version) with XC8 in MPLAB 8.63 (I haven't upgraded to X)

    I had some uC programming knowledge (in assembly) before I quit for a while, but I've more or less lost it.

    What is the difference in a .h and a .inc file?

    Just what all needs to be included for a run of the mill program? For instance, on my PC I'm suppose to always include "stdio.h" Is that required for programming a PIC? Where could I read about what standard libraries are required? IE: What would I include if I wanted to access a library to write to a character LCD?

    How do I set the configuration bits at the top of the XC8 code? I tried the ASM method, and even a method shown in an XC8 tutotial, but it failed. I'm having to set configuration (particularly, turn off the WDT) with the PicKit2 configuration bits feature.

    Thanks!
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    .h is mainly used for C compilers, whereas .inc are used for asm..

    Headers that need to be included are the ones for the functions you are using..

    In XC8 you can use "__CONFIG()"... or "#pragma config xxx xxx" both should work fine.


    All these are in the manual..... The XLCD library needs you to modify the XLCD.h before use... (Its in the header directory )
     
  3. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    .inc files are used with assembler and .h files are used with C.

    Include as little as possible. I have a template file that includes:
    (these are for avr, but I commented them so you can find equivalent libraries for XC8)

    Code (C):

    #include <stdlib.h> // C standard library
    #include <stdint.h> // (C99) Standard Integer Types
    #include <avr/io.h> // Register address definitions
    #include <avr/interrupt.h>  // I use interrupts in almost every project, so include the interrupt vector definitions etc.
    #include <util/delay.h> // Delay functions.. I always use those. At least when debugging.
    #include "atmel_avr.h"  // This is my own file that contains useful general purpose macros like bit-manipulation etc.
     
    I don't know if that was useful for you because it is for avr, but I hope it gave you a "general idea". I would not include anything that I don't need. I rarely need "stdio" because I write my own string manipulation functions etc. The library functions (like sprintf) are very inefficient.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    Edit: sorry misunderstood the whole thing... ignore what I was about to say about the above.
     
    Last edited: Jun 26, 2013
  6. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    In XC8 use this at the beginning
    Code (text):
    #include <xc.h>
     
  7. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    For setting up the 16F887 in most of my projects when coding in XC8, this is how I typically set it up -

    Code (text):

    #include <xc.h>
    #include <stdio.h>  //include this header file if you plan to use functions such as printf
    //place remaining header files here

    __CONFIG(LVP_OFF & FCMEN_OFF & IESO_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);

    /*
    DEBUG disabled (default - controlled by PICkit 2 via MPLAB)
    Low Voltage Programming Off (LVP)
    Fail Safe Clock Monitor Off (FCMEN)
    Internal/External Oscillator Switchover Off (IESO)
    Brown Out Reset Off (BOREN)
    Program Memory External Read Protect Off (default)
    Data EEPROM External Read Protect Off (default)
    RE3 has MCLR function (default)
    Power Up Timer On (PWRTE)
    Watchdog Timer Off (WDTE)
    High Speed Oscillator Mode (FOSC_HS)

    Oscillator Speed = 16MHz (external crystal on RA6 & RA7)
    */

    //place defines here

    void main(void)
    {
           //place code here
    }
    FOSC_HS is for using external crystals greater than 4MHz up to 20MHz (I use 16MHz). If using the internal oscillator, you can use FOSC_INTRC_NOCLKOUT instead, which frees up RA6 and RA7 for I/O purposes.

    Also note that the underscore which precedes "CONFIG" is a double underscore. Everything else is single underscore.
     
    Last edited: Jun 28, 2013
  8. wannaBinventor

    wannaBinventor Member

    Joined:
    Jun 24, 2010
    Messages:
    214
    Likes:
    2
    Thanks a lot guys. One last question: where can I find a list of the wording that you just showed. I think the reason it wasn't working for me at first was bc I was just using "WDT". Is that in the compiler manual, or where.
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,151
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Usually you'll find that the config wording mirrors the datasheet... From that its easy to work it out.

    I just use a number...
     
  10. Jon Wilder

    Jon Wilder Active Member

    Joined:
    Oct 22, 2010
    Messages:
    859
    Likes:
    82
    Location:
    Fresno, CA
    If you're running Windows 7 go to your C drive and follow this path -

    C:\\Program Files (x86)\Microchip\xc8\v1.12\docs

    There you will find a document named pic_chipinfo.html. Open it in your web browser. Once on this page, select 16F887 from the list.

    This page tells you how to set the config word using pragmas, but each config bit macro name is defined the same way here as it is for the __CONFIG() macro. Just replace the = sign with a _ and use the options available under each config macro name.

    For the record, Microchip considers using the __CONFIG() macro as "legacy". It still works so that it's backwards compatible with legacy code, but they recommend using pragmas for all newly written code. Here is the same example code above, but using pragmas -

    Code (text):

    #include <xc.h>
    #include <stdio.h>  //include this header file if you plan to use functions such as printf
    //place remaining header files here

    #pragma config LVP = OFF        //disable low voltage programming
    #pragma config FCMEN = OFF      //disable fail safe clock monitor
    #pragma config IESO = OFF       //disable internal/external oscillator switchover
    #pragma config BOREN = OFF      //disable brown out reset
    #pragma config PWRTE = ON       //enable power up timer
    #pragma config WDTE = OFF       //disable watchdog timer
    #pragma config FOSC = HS        //high speed oscillator mode - external crystal > 4MHz

    /*
    DEBUG disabled (default - controlled by PICkit 2 via MPLAB)
    Low Voltage Programming Off (LVP)
    Fail Safe Clock Monitor Off (FCMEN)
    Internal/External Oscillator Switchover Off (IESO)
    Brown Out Reset Off (BOREN)
    Program Memory External Read Protect Off (default)
    Data EEPROM External Read Protect Off (default)
    RE3 has MCLR function (default)
    Power Up Timer On (PWRTE)
    Watchdog Timer Off (WDTE)
    High Speed Oscillator Mode (FOSC_HS)

    Oscillator Speed = 16MHz (external crystal on RA6 & RA7)
    */

    //place defines here

    void main(void)
    {
           //place code here
    }
    Again if you want to use the internal oscillator, replace #pragma config FOSC = HS with #pragma config FOSC = INTRC_NOCLKOUT.
     

Share This Page