Question about MPLAB XC8 and 16 series families

Discussion in 'General Electronics Chat' started by hunterage2000, Feb 2, 2013.

  1. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Hi, does anybody here use MPLAB XC8? I am looking for the C16 include libraries so I can use delay.h, adc.h etc. The delay I have is for the C18 but when I use __delay_ms(); it shows as unrecognisable.
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    Headers are in Program Files (x86)\Microchip\xc8\v1.11\include. Your version number may vary.
     
  3. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Yeah I have my compiler set as XC8/v1.12 and have the delay.h file in header files in my projects tab. I have looked at the XC8 Guide and it shows __delay_ms().

    Code ( (Unknown Language)):
    1.  
    2. #include <xc.h>
    3. #include <delays.h>
    4. #define _XTAL_FREQ 20000000
    5.  
    6. int main(void) {
    7.     TRISB = 0; // Configure PORTB as output
    8.     PORTB = 1;
    9.     __delay_ms(10);
    10.     PORTB = 0;
    11.     return 0;
    12. }
    13.  
    14.  
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,884
    1,005
    What is the exact error you are getting?
     
  5. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    See the attached. There a red error line under __delay_ms(1000); but when I clean and build there is no errors found. It doesnt work on the breadboard.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    delays.h seems to only be for the PIC18 family, as given by the line in delays.h that reads:
    I've run into a few problems with the built-in libraries and the 16F family... I usually just roll my own...
     
  7. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    So how do I make my own delay then? Every source Ive seen shows __delay_ms();.
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Well, you can either go the route of calculating the delay of each statement and loop x amount of times(don't forget, branches take up two clock cycles), or use the timer. This would be hardware specific, as the timer delay would be dependent on the implementation and fosc. The timer route is probably the easiest, but you'll need to look a the datasheet for that one...
     
  9. hunterage2000

    Thread Starter Active Member

    May 2, 2010
    400
    0
    Can you recommend any good sources for xc8, PIC C programming etc that will help me progress? So far I know the basics of I/O port setting and pullup/down switches. I want to progress through ADC, interfacing LCD's, serial/parallel comms, wireless comms but I am finding it hard to find sources.
     
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    The XC compiler is relatively new, but it is based off of Hi-Tech's C compiler, so most of the references with Hi-Tech's compiler should work for XC...

    These look promising:
    http://www.gooligum.com.au/tut_baseline_C.html

    http://extremeelectronics.co.in/category/microchip-pic-tutorials/

    http://www.pyroelectro.com/category/tutorials/
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Read the datasheets.

    The 16F5x about 50 times.
    Then the 16f1824 50 times.

    The other PICs are just variations of these.

    For delays use the timer interrupt.

    TIMER0: 256 * prescaler
    TIMER1: 65536 * prescaler (different)

    You program the time, and then poll the timer interrupt flag.
    You do not have to use real interrupt. The Flag gets set anyway.
    Then you reset it, and reprogram the timer.

    Often you only need to write the high word of timer 1.

    Read the datasheet again.
    Not all the electrical ratings of course, but the memory layout and the I/O ports. And the timer0 and timer1.

    If you still have trouble, read it again and again. As many times as needed.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    It probably do not work because you have not set any of the configuration bits
     
  13. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    I don't like delay for some cycles as you need to change the cycles when you change the processor speed, and that can get lost causing days of delay.

    Most times you just want a quick and dirty delay, nothing extremely accurate (can be off a cycle or five), easy to set up and use, and doesn't need to keep any hardware hostage.

    Just a simple code delay, right?

    When you get the Microchip Applications Library (MAL) (and you should get it) inside the folder at ..\Microchip Solutions\Microchip\Common you will find the TimeDelay.c module. It has a very useful function: Delay10us();

    It has been tested for:
    Processor: PIC10/PIC12/PIC16/PIC18/PIC24/dsPIC30/dsPIC33/PIC32
    Compiler: PICC, C18, C30, C32

    I've used it with XC8 and it worked well there too.
     
    tshuck likes this.
Loading...