Delay routine problem

I want to create delay functions in a separate c file. The code should be as perfect as possible without using internal timer. I read somewhere that 'Calling the routine takes about 22us' Though 22uS may be different for my cpu clock, if possible this factor should also be taken into account.

I am using 89S52 with 24Mhz crystal. I tried following code.

void usdelay(unsigned int us){
        while (us--){
                _nop_();        //0.5uS single-cycle instruction delay
                _nop_();        //0.5uS single-cycle instruction delay
        }
}

void msdelay(unsigned int ms){
        unsigned long tm = 1000*ms;
        while (tm--){
                _nop_();        //0.5uS single-cycle instruction delay
                _nop_();        //0.5uS single-cycle instruction delay
        }
}


void secdelay(unsigned int sec){
        unsigned long tm = 1000*sec;
        while (tm--){
                msdelay(1);
        }
}

The problem is that the uS & ms delays are proper (may be - I have not measured them), but it takes very long to finish secdelay with 1 sec timing. Please HELP.

Parents
  • void usdelay(unsigned int us){
            while (us--){
                    _nop_();        //0.5uS single-cycle instruction delay
                    _nop_();        //0.5uS single-cycle instruction delay
            }
    }
    


    what about the time the while() takes?
    look at the disassembly
    illusions about the tine a C construct takes are always false

    if you want precise timing
    1) write it in assembler
    2) remember the time the call and parameter transfer takes

    Erik

Reply
  • void usdelay(unsigned int us){
            while (us--){
                    _nop_();        //0.5uS single-cycle instruction delay
                    _nop_();        //0.5uS single-cycle instruction delay
            }
    }
    


    what about the time the while() takes?
    look at the disassembly
    illusions about the tine a C construct takes are always false

    if you want precise timing
    1) write it in assembler
    2) remember the time the call and parameter transfer takes

    Erik

Children
No data
More questions in this forum