Ruth Hendrix

Pause a programm

Discussion created by Ruth Hendrix Employee on Jan 28, 2006
Code:
void delay (byte value) {    for (value += TCNTH; value <> TCNTH; ); }

 
This message contains an entire topic ported from a separate forum. The original message and all replies are in this single message. We have seeded this new forum with selected information that we expect will be of value to you as you search for answers to your questions.
 
Posted: Fri Dec 09, 2005 12:20 pm    
 
Hi all. How can I halt a time x (msec) a program??
 
Posted: Fri Dec 09, 2005 2:45 pm    
 
Depends what you mean by halt but you should always use a known timing source rather than a code loop.
You could configure a timer to give you an interrupt after a timeout of the appropriate length and execute a WAI instruction onthe CPU. The timer interrupt would wake the CPU.
 
Posted: Sat Dec 10, 2005 5:15 am    
 
A very simple way of generating a delay of a few milliseconds, that does not require interrupts, etc. It requires that the free running counter associated with a timer module be operating.

To setup the delay:
1. Read the high byte of the timer counter register TCNTH. (You will then also need to read TCNTL to release the read latch.)

2. Add an amount corresponding to the desired delay to represent the timeout value.

3. Continue to read the value of TCNTH, and loop until its value is equal to the timeout value.

In assembly code, it might look like the following:
Code:
DELAY:   LDA     #DELAY_VALUE           ADD    TCNTH           TST    TCNTL          ; RELEASE READ LATCH DL1:      CMP    TCNTH           TST    TCNTL          ; RELEASE READ LATCH           BCC    DL1            ; WAIT FOR TIMEOUT           RTS 

 
I hope this is of assistance.

Regards,
 
Posted: Mon Dec 12, 2005 5:06 am    
 
The snippet of assembly code of my previous post will not always work properly. Also, the code was for HC08 rather than HCS12, and where the timer does not have a "read latch" to worry about.
Quote:

In assembly code, it might look like the following:
Code:
DELAY:    LDA    #DELAY_VALUE           ADD    TCNTH           TST    TCNTL          ; RELEASE READ LATCH DL1:      CMP    TCNTH           TST    TCNTL          ; RELEASE READ LATCH           BCC    DL1            ; WAIT FOR TIMEOUT           RTS 

 
The following modified code might work:
Code:
DELAY:    LDAA   #DELAY_VALUE           ADDA   TCNTH  DL1:      CMPA   TCNTH           BNE    DL1            ; WAIT FOR TIMEOUT           RTS 

 
I apologise for any inconvenience.

Posted: Mon Dec 12, 2005 7:49 am    
 
Hi, thanks for your help. I dont know assembly ,,, What should I write in C?
 
Posted: Tue Dec 13, 2005 5:38 pm    
 
See if the following C code will work:
Code:
void delay (byte value) {    for (value += TCNTH; value <> TCNTH; ); }

 
I have assumed that the TCNTH byte register is already defined for the device, and that the timer counter has been set up and enabled elsewhere. The modulo value will need to be the default value.
You will need to ascertain the relationship between the parameter 'value' and the delay produced. This will depend on the bus frequency and the counter prescale value. For each increment of TCNTH, there will be (256*Prescale) bus cycles.

Regards,
Posted: Thu Dec 15, 2005 3:01 pm    
 
Thanks again!

Outcomes