Delay function timing not matching.

Discussion in 'Embedded Systems and Microcontrollers' started by elecbeg, Sep 8, 2016.

  1. elecbeg

    Thread Starter Active Member

    Sep 7, 2009
    30
    2
    Hi,

    I am using MPLABX and XC8, with PIC18F14K22.

    I am trying to use the __delay_ms () function to set a delay time. It does delay, but not for the expected time.
    Below is the code im using, I would expect that whatever number I insert into the delay function would delay for that many milliseconds. Instead, if I enter 125, it actually delays for about 500 milliseconds. If I enter 185, it delays for about 750 milliseconds.

    Could you explain what I doing wrong please.
    Thanks.

    Code (Text):
    1. // CONFIG1H
    2. #pragma config FOSC = IRC       // Oscillator Selection bits (External RC oscillator)
    3. #pragma config PLLEN = OFF      // 4 X PLL Enable bit (PLL is under software control)
    4. #pragma config PCLKEN = ON      // Primary Clock Enable bit (Primary clock enabled)
    5. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled)
    6. #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
    7.  
    8. // CONFIG2L
    9. #pragma config PWRTEN = OFF     // Power-up Timer Enable bit (PWRT disabled)
    10. #pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
    11. #pragma config BORV = 19        // Brown Out Reset Voltage bits (VBOR set to 1.9 V nominal)
    12.  
    13. // CONFIG2H
    14. #pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT is always enabled. SWDTEN bit has no effect.)
    15. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
    16.  
    17. // CONFIG3H
    18. #pragma config HFOFST = ON      // HFINTOSC Fast Start-up bit (HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.)
    19. #pragma config MCLRE = OFF      // MCLR Pin Enable bit (RA3 input pin enabled; MCLR disabled)
    20.  
    21. // CONFIG4L
    22. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
    23. #pragma config LVP = OFF         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
    24. #pragma config BBSIZ = OFF      // Boot Block Size Select bit (1kW boot block size)
    25. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
    26.  
    27. // CONFIG5L
    28. #pragma config CP0 = OFF        // Code Protection bit (Block 0 not code-protected)
    29. #pragma config CP1 = OFF        // Code Protection bit (Block 1 not code-protected)
    30.  
    31. // CONFIG5H
    32. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block not code-protected)
    33. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
    34.  
    35. // CONFIG6L
    36. #pragma config WRT0 = OFF       // Write Protection bit (Block 0 not write-protected)
    37. #pragma config WRT1 = OFF       // Write Protection bit (Block 1 not write-protected)
    38.  
    39. // CONFIG6H
    40. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers not write-protected)
    41. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block not write-protected)
    42. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
    43.  
    44. // CONFIG7L
    45. #pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
    46. #pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
    47.  
    48. // CONFIG7H
    49. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
    50.  
    51. // #pragma config statements should precede project file includes.
    52. // Use project enums instead of #define for ON and OFF.
    53.  
    54. #define _XTAL_FREQ 4000000  //Sets oscillator to 4Mhz
    55. #define LED1ON LATC = 0x01 //Lights LED1
    56. #define LED1OFF LATC = 0x00 //Turn LED 1 off
    57.  
    58.  
    59. #include <xc.h>
    60.  
    61.  
    62.  
    63.  
    64. void main(void)
    65. {
    66.    
    67.     TRISC = 0x00;
    68.    
    69.     while (1)
    70.     {
    71.        
    72.         LED1ON;
    73.         __delay_ms (180);
    74.         LED1OFF;
    75.         __delay_ms (180);
    76.     }
    77. }
    78.  
     
  2. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    The rc oscillator wont be very precise , change line 54 see what happens , or use internal FRC
     
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    That relationship is 4 to 1. It should be easy to solve for you
     
  4. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    The default speed of the internal oscillator is 1 MHz, which you don't seem to change anywhere, so your _XTAL_FREQ should be 1000000. It is 4000000 right now.
     
  5. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    I read this as being an external rc ?
     
  6. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    I'm thinking code (IRC) been changed and not the comments :rolleyes:
     
  7. elecbeg

    Thread Starter Active Member

    Sep 7, 2009
    30
    2
    Yes, I forgot to change the comment to say it is the internal oscillator being used, sorry about that.

    I did forget to say I changed the XTAL_FREQ to 1000000 before posting, but the delay was still no where near 1ms per number in the delay code. I did not time how far it was off and dont have the hardware to hand for next couple of days, but changing the XTAL to 1000000 did not sort out the problem. I posted the XTAL 4000000 as it did show a more clear time scale and I am hoping someone can tell me what else the problem could be?

    Thanks in advance.
     
  8. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    You need to go through OSCCON settings and look at __delay_ms( ); requirements in XC8
     
  9. elecbeg

    Thread Starter Active Member

    Sep 7, 2009
    30
    2
    I will try looking through it all again, have gone through it a few times, I know I am missing something, just cant seem to find what it is.
    Thanks for help.
     
  10. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Post exactly what you use along with the exact time measurements. Otherwise, it's completely pointless.
     
    JohnInTX likes this.
  11. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    your code, when done right:

    18f14k22_delay.PNG
     
  12. elecbeg

    Thread Starter Active Member

    Sep 7, 2009
    30
    2
    Thank you all for your help, the timing was wrong when I changed XTAL_FREQ to 1000000, because I actually changed it to 100000 (I missed out a "0").

    Sorry for bothering you all with such a silly mistake, its working fine now.
     
    NorthGuy and JohnInTX like this.
  13. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Well done!
    Thanks for letting us know.
     
  14. TQFP44

    New Member

    Sep 3, 2016
    25
    4
    Well done ,
     
Loading...