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.
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
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 .
How can I calculate machine cycle of MC9S12XHZ512 micro-controller.