__delay_ms() problem

Discussion in 'Embedded Systems and Microcontrollers' started by msr, Jun 20, 2010.

  1. msr

    Thread Starter Active Member

    Jul 8, 2008
    62
    1
    Hello,

    Im using a PIC16887 (Pickit2 debug express board) with HITECH PICC Compiler (lite mode) and Im trying to use __delay_ms(unsigned float) function.

    Im just trying a "blink led" test, and I want it ON for 1sec and OFF for another second. However, when using 1000 as argument, I get this error:
    I also tried 250 in the argument but it fails. It seems the maximum value I can use is 197. With that value it succeeds and with 198 it fails. Weird?

    Any suggestions?

    Thanks!
     
  2. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Im not exactly sure, and it sounds odd, but If it was an 8bit number I would expect it to go to 255.

    Post the code.
     
  3. bertus

    Administrator

    Apr 5, 2008
    15,638
    2,343
  4. msr

    Thread Starter Active Member

    Jul 8, 2008
    62
    1
    Ok, here's the code I have:

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>    
    3.  
    4. #define _XTAL_FREQ 4000000
    5.  
    6. __CONFIG(INTCLK & WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
    7. __CONFIG(BORV40);
    8.  
    9.  
    10.  
    11. void setup(void){
    12.  
    13.     // everything is OUTPUT
    14.     TRISA = 0x00;
    15.     TRISB = 0x00;
    16.     TRISC = 0x00;
    17.     TRISD = 0x00; // LEDs are connected to these pins
    18.     TRISE = 0x00;
    19. }
    20.  
    21.  
    22.  
    23. int main(void){
    24.  
    25.     setup();
    26.  
    27.     while(1){
    28.  
    29.       PORTD = 0x01;
    30.       __delay_ms(100);
    31.       __delay_ms(100);
    32.       __delay_ms(100);
    33.       __delay_ms(100);
    34.       __delay_ms(100);
    35.  
    36.       PORTD = 0x00;
    37.       __delay_ms(100);
    38.       __delay_ms(100);
    39.       __delay_ms(100);
    40.       __delay_ms(100);
    41.       __delay_ms(100);
    42.  
    43.     }
    44.  
    45. }
    This WORKS. It fails on compiling when I use delays of 198 or greater. Until 197 it works.



    Edit: I didn't include "delay.h". When I try to do that I get: "can't open include file "delay.h": No such file or directory". Where is that include file? However the function signature Im using isn't equals to that one on edaboard's thread. Its __delay_ms() instead of DelayMs(). I suppose DelayMs() was a function of a previous version of PICC Compiler, maybe.
     
    Last edited: Jun 20, 2010
  5. bertus

    Administrator

    Apr 5, 2008
    15,638
    2,343
    Hello,

    It could be that it is for an older version.

    Here is a page wich uses the delay sequence you give:
    http://pastebin.com/f215dbde6

    It is defined at the start of the program.

    Bertus
     
  6. sceadwian

    New Member

    Jun 1, 2009
    499
    37
    It's obviously a math problem with the compile specific routine and the ASM code it invokes. Call a 100ms delay 10 times... Don't be daft.
    You may be trying to include a file from the wrong diretory path, find out where it is and include THAT subdirectory.
     
  7. Macabra

    Active Member

    May 31, 2008
    49
    0
    Sounds to me like you need to find where this delay.h header file is in your project and add it to your project folder that you are currently saving your workplace. Also, don't forget to actually include it in your project from your workplace (ie where you include other header files..)
     
  8. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    The fact that it compiles and works with short delays indicates that there is nothing wrong with paths, declarations or headers file locations.

    The problem appears to be with the internal workings of the compiler.

    I struck the same sort of problem with HiTechC some time ago. Even though the _delay() function is supposed to accept a long int (32 bits) it kept throwing up an error with values well short of 16 bits (it was a long time ago so I don't remember the exact details). At the time I thought it might be a limitation of the unpaid-for version but there was no way I was going to spend good money to find out.
     
  9. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    I had a similar issue when I was trying to use that 'function'

    I decided in the end that the __delay_ms 'function' must really just be a macro which called __delay_us() in a loop, which the compiler was trying to optimise to be an inline, making the number too large. I found that changing the Crystal frequency defined would change the value at which things fail, and turning off all optimisations stopped there being a problem.
    However, what is the point in having optimisations turned off. All-in-all, not impressed with the Hitech compiler, due to the number of problems that I am having.
     
  10. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Both the _delay_ms() and _delay_us() are macros that use the crystal frequency setting (whatever the command may be) to translate the delay time into machine cycles and then pass that value to the __delay() function that does the actual work.

    In the end I ditched HiTechC and bought a non-commercial licence for SourceBoostC which I am very happy with.
     
  11. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Last edited: Jun 23, 2010
  12. msr

    Thread Starter Active Member

    Jul 8, 2008
    62
    1
  13. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
  14. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    I have a recent install of mplab 8.50, and that is still version 9.70 of Hitech C

    Maybe it is a development only version, given the comment that you have linked to (as in it doesn't load old workspaces, bad move)
     
  15. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Ah,
    looks like this 'fixed' version is only available to those who wish to pay for a license and register the product.
    Given the number of bugs I am having with the compiler, I am not going to pay them for a license I think.

     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Last edited: Jun 23, 2010
  17. coldpenguin

    Active Member

    Apr 18, 2010
    165
    9
    Thanks, am downloading now.
     
  18. msr

    Thread Starter Active Member

    Jul 8, 2008
    62
    1
    Really strange HITECH doesn't offer 9.71 version on their website...
    But it works! With the that version "__delays()" are correctly working!

    Thank you very much t06afre!
     
  19. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    It was on the web site but was pulled back due to bugs I guess. If you get strange problems. Try to switch from PRO mode to Lite mode. I had a problem that the optimizer took to much of code away. The optimizer ignored setting ANSEL(H) to zero:mad::confused:. Big problems as I could not read some single BITs in port A. In PRO mode I had to use #asm, #endasm to force setting of bits in the ANSEL registers. Htsoft say the bugfixed version will be (re)released soon
     
  20. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
Loading...