How to estimate a delay cycle for the HCS12

freescaleCreating a delay and estimating how much time the delay takes is a straightforward process. I’ll show you how to create a delay routine in assembly, in addition to estimating how much time the routine actually takes.  In order to estimate the delay cycle, we will need to determine the amount of time spent in clock cycles for each instruction inside the delay routine.

By obtaining a datasheet for the HCS12, we can find out the amount of clock cycles for each HCS12 instruction.  Look for the “access detail” column, it will display characters that may look like rPf, P, rPo, etc.  Regardless of character type, what we are interested in is the amount of characters in the column for that instruction. The amount of characters are equal to the amount of clock cycles.

Calculating CPU Instruction Time

fetchdecodeexecute copyThe HCS12 has an 8 MHz crystal. However, only half of the crystal’s oscillator frequency is used for CPU instruction time. Normally, time is calculated as T = 1/F. In the case of the HCS12, we will need to halve the frequency provide by the crystal before calculating the actual time:
8 MHz / 2 = 4 MHz;  T = 1 / 4 MHz = 0.25 us

Now we can estimate the time delay with this formula
(COUNTER_VALUE x NUM_OF_INSTRUCTION_CYCLES) x CPU_CYCLE_TIME

But first, let’s take a look at the example code below.

COUNTER_LOC EQU #$1200
--
--
DELAY LDAA #300
      STAA COUNTER_LOC
LOOP  NOP
      NOP
      DEC COUNTER_LOC
      BNE LOOP
      RTS

Breaking Down the Instruction Cycles

The actual loop inside the DELAY subroutine has a total of four instructions. The NOP instruction requires only one instruction cycle. The DEC instruction takes four instruction cycles. BNE requires three instruction cycles if looping back and only one instruction cycle if falling through the loop.

To determine the amount of delay time for the function above, we simply plug the values into our formula:
(COUNTER_VALUE x NUM_OF_INSTRUCTION_CYCLES) x CPU_CYCLE_TIME
(300 x 9) x 0.25E-6 = 0.000675 us seconds

 

 

Leave a comment