Delay function help

Discussion in 'Programmer's Corner' started by nevermine, Jun 21, 2010.

  1. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    hi,im doing the PIC_BASE_C_4 example which is driving 7segment,but have problems with the DelayS().
    i dont have the stdmacros-HTC.h in my hi-tech C include folder but i found it in this forum. then i add it into my header file,it still cant work...
    besides,i tried Delay_MS(100); or delay_ms(100); both of the cant work,is there any other file that i need to include?
    currently i just #include <htc.h>

    is there anyway to write a delay function myself? probably delayms/delays...
    thanks in advance
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  3. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    errr that's what im doing @@,which PIC_BASE_C_4 example,
    but im facing the problem with delay
    so i wonder how can i write a delay function beside using the 1 from the example...

    thanks for replay anyway^^
     
  4. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I think this example is somewhat old. I can not find any delay.h in my HI-Tech version(9.71). You could try to comment out the #include
    "delay.h" line in the stdmacros-HTC.h All time delays are now defined in the htc.h r paste in the and replace
    Code ( (Unknown Language)):
    1.  
    2. #define DelayS(T) {unsigned char i; for (i=0; i<T*10; i++) DelayMs(100);}
    3.  
    with
    Code ( (Unknown Language)):
    1.  
    2. #define DelayS(T) {unsigned char i; for (i=0; i<T*10; i++) __delay_ms(100);}
    3.  
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    in Hi-Tech PICC Lite the syntax for the delay is:

    __delay_ms(100)

    or try this depending on what version you are using....

    _delay_ms(100)


    B. Morse
     
  6. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    yea thats what im doing in my code, but it doesnt work,with the error "undefined object "__delay_ms()". im did include the htx.h file and defined the _XTAL_FREQ 4000000...
    Code ( (Unknown Language)):
    1.     for (;;)
    2.     {
    3.         for (digit=0; digit<10;digit++)
    4.         {
    5.             PORTC= pat7seg[digit] >> 1;
    6.             RB2= pat7seg[digit] & 0b0000001;
    7.        
    8.             __delay_ms(100);
    9.         }
    10.     }
     
  7. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This is something that is fixed in the 9.71a version of the compiler. You could download it from Microchip, for eval and Lite mode usage. You application do not need high speed. May I suggest you run your CPU on a lower frequency. Insert this early in the code. If your OSC is 4MHz it will be the same as your is running on 500KHz osc. So you must use #define _XTAL_FREQ 500000 in your code
    Code ( (Unknown Language)):
    1.  
    2. OSCCON=0b00110000;//500KHz osc
    3.  
    You can also download the 9.71a version here for eval/Lite mode. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849
     
    Last edited: Jun 23, 2010
  8. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    I use version 9.65 and 9.70 (different PC's) and I can use the functions just fine..... I will have to post some code here when I get home tonight...... I have another delay routine I use that is based on Timer0 and its interrupt, which I find very useful to be able to do other tasks while the delay is running.....

    B. Morse
     
  9. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Then _delay(x) exceeds maximum limit of 197120 cycles. You will get this error. The __delay_ms() is defined as shown
    Code ( (Unknown Language)):
    1.  
    2. #define __delay_us(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000000.0)))
    3. #define __delay_ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
    4.  
    If you use 4MHz as clock and want a 100ms delay. The input to the _delay function will be 100000 cycles. So it should be OK. But due to a some sort of bug it will not work. But _delay(100000) will work. This do not depend on which mode you use for the compiler (PRO/Lite).
     
    Last edited: Jun 22, 2010
  10. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    Hi all,thanks for reply.
    first of all, let say if im using 4Mhz crystal (internal) and i want to delay 100ms, so by applying the formula above, (100X4M/4000)=100K?then how can i use it?to write a delay function myself
    btw, i found a delay function from google which look like below
    Code ( (Unknown Language)):
    1. //**************************************
    2. // Name: Delay functions for HI-TECH C on the PIC
    3. // Description:Delay functions for HI-TECH C on the PIC
    4. // By: SouthOfHeaven
    5. //
    6. //
    7. // Inputs:None
    8. //
    9. // Returns:None
    10. //
    11. //Assumes:None
    12. //
    13. //Side Effects:None
    14. //This code is copyrighted and has limited warranties.
    15. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.7421/lngWId.3/qx/vb/scripts/ShowCode.htm
    16. //for details.
    17. //**************************************
    18.  
    19. /*
    20. *   Delay functions for HI-TECH C on the PIC
    21. *
    22. *   Functions available:
    23. *       DelayUs(x)  Delay specified number of microseconds
    24. *       DelayMs(x)  Delay specified number of milliseconds
    25. *
    26. *   Note that there are range limits: x must not exceed 255 - for xtal
    27. *   frequencies > 12MHz the range for DelayUs is even smaller.
    28. *   To use DelayUs it is only necessary to include this file; to use
    29. *   DelayMs you must include delay.c in your project.
    30. *
    31. */
    32. /*  Set the crystal frequency in the CPP predefined symbols list in
    33.     HPDPIC, or on the PICC commmand line, e.g.
    34.     picc -DXTAL_FREQ=4MHZ
    35.    
    36.     or
    37.     picc -DXTAL_FREQ=100KHZ
    38.    
    39.     Note that this is the crystal frequency, the CPU clock is
    40. divided by 4.*/
    41. #ifndef XTAL_FREQ
    42. #define XTAL_FREQ   4MHZ        /* Crystal frequency in MHz */
    43. #endif
    44. #define MHZ *1000           /* number of kHz in a MHz */
    45. #define KHZ *1          /* number of kHz in a kHz */
    46. #if XTAL_FREQ >= 12MHZ
    47. #define DelayUs(x) 
    48.  
    49.  
    50.                 {
    51.                      unsigned char _dcnt; \
    52.                  _dcnt = (x)*((XTAL_FREQ)/12MHZ); \
    53.                  while(--_dcnt != 0) \
    54.                      continue;
    55.                      }
    56.     #else
    57.     #define DelayUs(x)  { unsigned char _dcnt; \
    58.                  _dcnt = (x)/(12MHZ/(XTAL_FREQ))|1; \
    59.                  while(--_dcnt != 0) \
    60.                      continue; }
    61.     #endif
    62.     extern void DelayMs(unsigned char);
    but it mention that "to use DelayMs you must include delay.c in your project."
    but i couldnt found a delay.c i n my compiler...where can i get it?
    the code is working for me,but if i want to delay like 100ms,den i need to repeat the delayUs for thousands of time...or use a for loop?
    is there any way to derive the delayMs?

    got my code,i just #include <htc.h> is it sufficient for my code?
    and i look into the code there is no __delay_ms()...

    sorry for my stupidness...
    thanks in advance
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    My fault it is in the pic.h include file
    Until you can download the 9.71a version from the htsoft site. you can put this in your code
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 4000000 //teling the compiler that clock speed is 4MHz
    4. #define _100_ms (100)*(_XTAL_FREQ/4000.0)
    5.  
    6. void main(void)
    7. {
    8. _delay(_100_ms);
    9. }
    10.  
     
    nevermine likes this.
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    nevermine likes this.
  13. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    By the way. The maximum number for the _delay() is now 50660096 cycles
     
  15. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    hey , now the delay is working!!thanks so much for helping
    btw, what if i wan to make a delay of 1 second? beside doing __dealy_ms(1000)...
    in this code>> "#define _100_ms (100)*(_XTAL_FREQ/4000.0)" , "_100_ms" why 100? so that mean i can do a second my changing this value?
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    That was a work-around to your problem If you are using version 9.71a you can do like this. It will work. Both in Lite and PRO mode. You will have 45 days to use PRO mode after that it is only Lite mode. But for hobbyist Lite mode will still do the job. The Link I gave is public and legal link so share as you want
    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 4000000
    4. //teling the compiler that clock speed is 4MHz
    5. __CONFIG(/*Set your config bit correct or...:eek:*/);
    6. void main(void)
    7. {
    8. __delay_ms(10000);//10 seconds wait no probem
    9. }
    10.  
    11.  
     
  17. nevermine

    Thread Starter New Member

    Nov 30, 2009
    7
    0
    hi t06afre, i'd change my PIC16F505 to PIC16F887, then the delay function cannot work, with the error
    "Error [499] ; 0. undefined symbol: _delayms(16F887.obj) "
    but previously i tried on 16F505 it is not neccesary to define the _delay.
    does it concern with the __CONFIG()?
    thanks
     
  18. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    use _delay_ms(), not _delayms(), seems like a simple syntax error to me.
     
Loading...