Skip to main content
added 1 character in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
void call12delay12(void)
{
    nop
}

For 20-23 counts, you would call it once plus adding 8 to 11 nops after the call (or a dummy jump to the next instruction which would eat up 6 cycles plus 2 to 5 nops -- so delaying 20 cycles would cost just four instructions plus the subroutine which is assumed to be used more than once.). For 24-31 counts, you would call itdelay12 twice, and add 0 to 70to 5 nops and/or a jump instruction as needed.

So to delay 20 cycles:

    acall delayl12
    jump next
next:
    nop
    nop
void call12(void)
{
    nop
}

For 20-23 counts, you would call it once plus adding 8 to 11 nops after the call. For 24-31 counts, you would call it twice, and add 0 to 7 nops as needed.

void delay12(void)
{
    nop
}

For 20-23 counts, you would call it once plus adding 8 to 11 nops after the call (or a dummy jump to the next instruction which would eat up 6 cycles plus 2 to 5 nops -- so delaying 20 cycles would cost just four instructions plus the subroutine which is assumed to be used more than once.). For 24-31 counts, you would call delay12 twice, and add 0to 5 nops and/or a jump instruction as needed.

So to delay 20 cycles:

    acall delayl12
    jump next
next:
    nop
    nop
added 1 character in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163

As others have mentioned, this is best done in assembly. Here is my original attempt at coding this, when I thought the jump instructions took either 2 or 4 cycles (see *EditEdit below for the revised version).

As others have mentioned, this is best done in assembly. Here is my original attempt at coding this, when I thought the jump instructions took either 2 or 4 cycles (see *Edit below for the revised version).

As others have mentioned, this is best done in assembly. Here is my original attempt at coding this, when I thought the jump instructions took either 2 or 4 cycles (see Edit below for the revised version).

added 351 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163

Since I wrote the original code, the OP has informed me that the number of clock cycles for a jump in his 8051 is 5 or 6, not the 2 or 4 stated in the datasheet I read. So I have re-written the routine to take this into account. Unfortunately, this bumps the minimum cycle count that can be timed to 32 instead of 20. So if counts between 20 and 3531 are absolutely needed to be handled, some special purpose code will need to be written specific to that case (see below).

For counts of 20-31, you could create a subroutine with just one nop, that takes 12 cycles including the call and return:

void call12(void)
{
    nop
}

For 20-23 counts, you would call it once plus adding 8 to 11 nops after the call. For 24-31 counts, you would call it twice, and add 0 to 7 nops as needed.

Since I wrote the original code, the OP has informed me that the number of clock cycles for a jump in his 8051 is 5 or 6, not the 2 or 4 stated in the datasheet I read. So I have re-written the routine to take this into account. Unfortunately, this bumps the minimum cycle count that can be timed to 32 instead of 20. So if counts between 20 and 35 are absolutely needed to be handled, some special purpose code will need to be written specific to that case.

Since I wrote the original code, the OP has informed me that the number of clock cycles for a jump in his 8051 is 5 or 6, not the 2 or 4 stated in the datasheet I read. So I have re-written the routine to take this into account. Unfortunately, this bumps the minimum cycle count that can be timed to 32 instead of 20. So if counts between 20 and 31 are absolutely needed to be handled, some special purpose code will need to be written specific to that case (see below).

For counts of 20-31, you could create a subroutine with just one nop, that takes 12 cycles including the call and return:

void call12(void)
{
    nop
}

For 20-23 counts, you would call it once plus adding 8 to 11 nops after the call. For 24-31 counts, you would call it twice, and add 0 to 7 nops as needed.

added 2490 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 6 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 190 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 55 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 446 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 258 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
added 70 characters in body
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading
Source Link
tcrosley
  • 48.4k
  • 5
  • 99
  • 163
Loading