Like Tree3Likes
  • 1 Post By jimblumberg
  • 1 Post By jimblumberg
  • 1 Post By christop

Delay for a fraction of a second in Borland C++ 4.52 IDE

This is a discussion on Delay for a fraction of a second in Borland C++ 4.52 IDE within the C Programming forums, part of the General Programming Boards category; It has been many years since I did much C programming but I Have Been Given the job of adding ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    21

    Unhappy Delay for a fraction of a second in Borland C++ 4.52 IDE

    It has been many years since I did much C programming but I Have Been Given the job of adding some capabilities to an existing program. I am using the “XP Mode” in Windows 7. The program was written in Borland C++ version 4.52 using the 16 bit 80186 instruction set. The source code is 404 pages long when pasted into word.

    The existing program uses the void sleep(unsigned seconds); in many different places but I want to delay the program for a fraction of a second. I have found recommendations to use
    Code:
     
    #include <windows.h>
    Sleep(500);
    After adding this the program contains:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <dos.h>
    #include <alloc.h>
    #include <mem.h>
    #include <time.h>
    #include <tcp.h>
    #include <conio.h>
    
     
    Lots of code
    
     
    Sleep(500);
    When I do this I get:

    Warning COMMS2.C 423: Call to function 'Sleep' with no prototype in function DownloadAFile
    Linking cdacs.exe:
    Linker Error: Undefined symbol _Sleep in module COMMS2.C

    Another recommendation I found was to use
    Code:
    delay(500);
    To get 0.5 second delay. When I did this I didn't get any warnings or errors but my program just stopped. (I didn't wait 500 seconds).

    I will be grateful to anyone who can help me get a fractional second delay.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,774
    You might try either of these headers "mapiwin.h" or "winbase.h".
    These both have Sleep() function in Borland C++ 5.5.

    Note: The "winbase.h" is supposed to be included by windows.h; so, it is not likely to fix the problem.

    The "mapiwin.h" says its a WIN16 vs. WIN32 compatibility header, it is more likely to be correct.

    Tim S.
    Last edited by stahta01; 10-08-2012 at 07:56 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Are you still using the Borland compiler? Note the Borland delay() function delays in mili-seconds so delay(500) would be 500 miliseconds (1/2 second) not 500 seconds.


    Jim
    Matticus likes this.

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Thank you for the reply Tim.

    When I use #include <mapiwin.h> I get the following error:

    Compiling COMMS2.C:
    Fatal ..\..\BC45\INCLUDE\PSHPACK8.H 38: Error directive: 8-byte packing is not supported!!

    When I use #include <winbase.h> I get a bunch of errors including:

    Compiling COMMS2.C:
    Error ..\..\BC45\INCLUDE\WINBASE.H 168: Declaration missing ;
    Error ..\..\BC45\INCLUDE\WINBASE.H 169: Multiple declaration for 'DWORD'

    Also I see that Winbase.h contains the message:

    winbase.h -- This module defines the 32-Bit Windows Base APIs

    But, I am compiling a for 16-bit application. So this won't work.

    Jerry

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Thank you for the reply Jim.

    Yes I am still using the Borland C++ 4.52 IDE. I understand that delay should be in milliseconds. I added the comment about 500 seconds just in case it was in seconds. Since then I tried
    Code:
    delay(1);
    and my program still stopped when it reached that line.

    Jerry

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    What do you mean by stopped? It should stop/delay for 1 milisecond. I suggest post the smallest possible program that illustrates your problem. If the program doesn't compile provide the complete error messages exactly as they appear in your development environment.


    Jim

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Quote Originally Posted by jimblumberg View Post
    What do you mean by stopped? It should stop/delay for 1 milisecond. I suggest post the smallest possible program that illustrates your problem. If the program doesn't compile provide the complete error messages exactly as they appear in your development environment.


    Jim
    My compiled program is running on a picoflash single board computer from JK microsystems imbedded in a critical process controller designed by the company I am supporting. The source code is 404 pages long when pasted into Word. Most of that code is needed to get the system up and running so the smallest program I can use is way beyond the scope of this forum. But, I have added print statements that print to the processor terminal so I can see step by step what is happening.
    The section of code of interest is inside a loop. The code of interest is:
    Code:
    printf("#124 time = %02d:%02d:%02d\n", tm.ti_hour, tm.ti_min, tm.ti_sec);
     
    SendResp(RspPtr);
     
    sleep(1);
    This works fine. I can watch the terminal and see each printout that includes the time as the program goes through the loop over and over again.

    When I change the code to

    Code:
    printf("#124 time = %02d:%02d:%02d\n", tm.ti_hour, tm.ti_min, tm.ti_sec);
     
    SendResp(RspPtr);
     
    delay(500);
    There are no errors and no warnings.

    After downloading the .exe to the board, everything runs fine until I get to the loop. Then I see the first printout and then nothing more appears on the terminal. I have to shut off the power to get the processor to run again.

    I have tried

    Code:
    printf("#124 time = %02d:%02d:%02d\n", tm.ti_hour, tm.ti_min, tm.ti_sec);
     
    SendResp(RspPtr);
     
    delay(1);
    and that doesn’t work either.

    Jerry

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    Can you write a program, and run it on your board, just to test delay()? Something simple like:

    Code:
    int main() 
    {
    
       while(1)
       {
          delay(5000);
          printf("Do you see the delay?\n");
       }
    
       return(0);
    }
    It is possible that your board doesn't have the proper support for the delay() function and you may have to resort to something like:
    Code:
    unsigned int i;
    for(i = 0; i < 60000; ++i)
    { /* Blank Body */ }
    Edit: Also unless this board is running Windows you probably shouldn't be including the <windows.h> header file.


    Jim
    Last edited by jimblumberg; 10-09-2012 at 03:00 PM.
    stahta01 likes this.

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    OK Jim

    I created a small modification to your program. I had to add #includes. I added print before delay

    Code:
    //#include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <dos.h>
    #include <alloc.h>
    #include <mem.h>
    #include <time.h>
    #include <tcp.h>
    #include <conio.h>
     
    int main()
    {
                    while(1)
                    {
                                    printf("Before delay\n");
                                    delay(5000);
                                    printf("Do you see the delay?\n");
                    }
                    return(0);
    }
    The “Build” gave me

    Compiling TEST.CPP:
    Warning TEST.CPP 23: Unreachable code in function main()
    Linking test.exe:

    After downloading I got

    C:\>test
    Before delay

    then nothing. So, from what you said the picoflash probably doesn’t support delay().

    Following up on your suggestion I wound up with the following program
    Code:
     
    //#include <windows.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <setjmp.h>
    #include <dos.h>
    #include <alloc.h>
    #include <mem.h>
    #include <time.h>
    #include <tcp.h>
    #include <conio.h>
     
    int main()
    {
                    unsigned int i;
                    unsigned int j;
                    printf("Before delay\n");
                    for (j = 0; j< 1000; ++j)
                    {
                                    for(i = 0; i < 30000; ++i)
                                                    { /* Blank Body */ }
     
                    }
                    printf("Do you see the delay?\n");
                    return(0);
    }
    That allowed me to determine that the inner loop takes about 12 msec for this configuration.

    That should allow me to get my job done. But some have warned that this could cause overheating of the CPU.

    Jerry

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Maybe:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
       clock_t start, stop;
       start = clock();
       stop = clock();
       while(stop < start + 1500) {  //1.5 seconds
          stop = clock();
       }
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    21
    Quote Originally Posted by Adak View Post
    Maybe:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
       clock_t start, stop;
       start = clock();
       stop = clock();
       while(stop < start + 1500) {  //1.5 seconds
          stop = clock();
       }
       return 0;
    }
    Thank you Adak

    I tried this and it works.

    Any idea what the difference in heating might be between this approach and the tight “for” loop?


    Jerry

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Jerry900 View Post
    Any idea what the difference in heating might be between this approach and the tight “for” loop?
    None, unless clock() somehow halts or slows down the CPU while it executes (which would be highly unusual in my experience). You can probably call a system-specific routine to put the CPU to sleep for a short time inside the busy loop.

    I'm guessing that whatever time source delay depends on is not running (maybe an interrupt is disabled) , so it will effectively halt the program forever.
    Salem likes this.

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,774
    Does the NOP instruction (if it exists) use less power?

    NOP := No Operation Instruction on many embedded and early CPUs.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Jerry900 View Post
    Thank you Adak

    I tried this and it works.

    Any idea what the difference in heating might be between this approach and the tight “for” loop?


    Jerry
    No, my cpu's all have high end aftermarket coolers since they "Fold" for medical research, so much. Can you test it with yours, by running a free utility like CoreTemp?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fraction Program...
    By hottiefee in forum C++ Programming
    Replies: 15
    Last Post: 01-18-2011, 09:04 PM
  2. Replies: 18
    Last Post: 03-26-2008, 10:01 AM
  3. Help with fraction problem
    By jrahhali in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2004, 11:37 PM
  4. fraction help
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2002, 05:44 PM
  5. Fraction
    By Nicknameguy in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2002, 11:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21