Dealy using timer in MC9S12XHZ512

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Dealy using timer in MC9S12XHZ512

774 次查看
kdn
Contributor III

I want to create time delay in MC9S12XHZ512 micro-controller using Timer, I have used 16MHz crystal on my micro-controller board with no prescale, Can someone explain me how can I calculate delay of 1ms if I am using 16-bit Timer.

标签 (1)
0 项奖励
3 回复数

533 次查看
ZhangJennie
NXP TechSupport
NXP TechSupport


hi KDN,

1ms delay can be easy implemented if use processor expert.  there is method: void Cpu_Delay100US(word us100) that can implement at least 100us delay. so Cpu_Delay100US(10) can delay 1ms. below is the function.

/*
** ===================================================================
**     Method      :  Cpu_Delay100US (component MC9S12XHZ512_144)
**
**     Description :
**         This method realizes software delay. The length of delay
**         is at least 100 microsecond multiply input parameter
**         [us100]. As the delay implementation is not based on real
**         clock, the delay time may be increased by interrupt
**         service routines processed during the delay. The method
**         is independent on selected speed mode.
**     Parameters  :
**         NAME            - DESCRIPTION
**         us100           - Number of 100 us delay repetitions.
**                         - The value of zero results in maximal
**                           delay of approx. 6.5 seconds.
**     Returns     : Nothing
** ===================================================================
*/
#pragma NO_ENTRY                       /* Suppress generation of entry code in a function */
#pragma NO_EXIT                        /* Suppress generation of exit from a function */
#pragma MESSAGE DISABLE C5703          /*Disable C5703: Parameter ‘<Parameter>’ declared in function ‘<Function>’ but not referenced */
void Cpu_Delay100US(word us100)
{
  /* irremovable overhead (ignored): 13 cycles */
  /* ldd:  2 cycles overhead (load parameter into register) */
  /* jsr:  4 cycles overhead (call this function) */
  /* rts:  7 cycles overhead (return from this function) */

  /* irremovable overhead for each 100us cycle (counted): 13 cycles */
  /* dbne:  3 cycles overhead (return from this function) */

  /*lint -save  -e950 -e522 Disable MISRA rule (1.1,14.2) checking. */
  asm {
    loop:
    /* 100 us delay block begin */
    /*
     * Delay
     *   - requested                  : 100 us @ 8MHz,
     *   - possible                   : 800 c, 100000 ns
     *   - without removable overhead : 797 c, 99625 ns
     */
    pshd                               /* (2 c: 250 ns) backup D */
    ldd #$0107                         /* (2 c: 250 ns) number of iterations */
    label0:
    dbne d, label0                     /* (3 c: 375 ns) repeat 263x */
    puld                               /* (3 c: 375 ns) restore D */
    nop                                /* (1 c: 125 ns) wait for 1 c */
    /* 100 us delay block end */
    dbne d, loop                       /* us100 parameter is passed via D register */
    rts                                /* return from subroutine */
  };
  /*lint -restore Enable MISRA rule (1.1,14.2) checking. */
}

another method is to use PTI,  it can generate 1ms insterrupt. see attached code, TimerInt is used.

====================================

this answer is for you. if it helps, pleaes click on "Correct Answer " button. thanks

Best Regards,

Zhang Jun

0 项奖励

533 次查看
kdn
Contributor III

Thanks for replay , Can you tell me how can I can calculate time required by timer to increment by 1, if I am using 16MHz crystal with noprescale in MC9S12XHZ512 .

0 项奖励

533 次查看
kdn
Contributor III

How can I calculate machine cycle of MC9S12XHZ512 micro-controller.

0 项奖励