How to create time delay

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to create time delay

5,687 Views
santygo
Contributor I

I am a beginner..using MC9S08QGB 8 bit microcontroller....i need to program for a time delay....

i can use simple for loop for time delay. but how to measure the execution time for my delay loop....

i found on google tht there is execution counter in code warrior.where can i find tht....i am using code warrior 10.1(eclipse)

Labels (1)
0 Kudos
Reply
6 Replies

2,522 Views
bigmac
Specialist III

Hello,

 

For earlier CW versions (6.x), the cycle counter was only available with full chip simulation (FCS) mode, and whilst within debug mode.  You might check whether this is also true for CW 10.1.

 

Regards,

0 Kudos
Reply

2,522 Views
PeterHouse
Contributor I

santygo,

 

How long do you need to delay?  A very short delay, 10s to 100s of clock cycles, might make sense to code as some NOPs or a short loop.  Longer delays should peobably use a timer and probably interrupts in some manner.  You have to weigh the use of processor time and whether it should be tied up counting OR let a timer count so the processor can perform another task.

 

Peter

0 Kudos
Reply

2,522 Views
santygo
Contributor I

Hi,

 

Thank you for your reply... i need delay of 30s..i was using simple "for" loop for delay..but i dont know how much delay it creates...could u tell tht how to calculate the time taken by the single iteration of my for loop...there is no cycle counter in debug mode which i can find in previous CW versions.....i am using CW10.1...thank you...

0 Kudos
Reply

2,522 Views
bigmac
Specialist III

Hello,

 

For a 30 second delay, the software loop method would be inappropriate.  You might base your timing on either the TPM module or the MTIM module.  The idea is to generate an interrupt at regular intervals, and use a global variable to count the number of interrupts, as has already been suggested.

 

Here are some code snippets to demonstrate this.  For delays of tens of seconds I might suggest a periodic interrupt period of, say 10 milliseconds.  I will also assume the use of the MTIM module for this purpose.  The clock source for this module could be either the bus clock or the fixed frequency clock.  However, I will assume use of the fixed frequency clock.

 

The fixed frequency clock is one half the reference frequency.  Assuming that FEI mode is used, and assuming the factory trimmed reference frequency of 31.25 kHz, the fixed frequency clock will be 15.625 kHz, or 64 us clock period.  A total of 156 clock periods will give a delay of 9.98 milliseconds, which is assumed to be sufficiently accurate for this timing purpose.  The 156 clock periods would be achieved with MTIMMOD = 155; and prescale division by 1.

 

The required action upon timeout of the 30 second delay might be handled using one of two methods - processing within the ISR function itself, or using frequent polling of the timeout status within the main loop.  The polling method would be adopted if the timeout action were lengthy.  For this example, I have assumed the polling method.

 

In addition to the ISR function, other functions will be needed to initialise the MTIM module, to start each timeout period, and the polling function to test whether delay timeout has occurred.  There are many possible variations - this is just one example.

 

// Global variables:volatile word delay_count = 0; // Counter for MTIM overflowsvolatile byte tflag = 0;       // Timeout flag// MTIM initialisation:void MTIM_init( void){   MTIMSC  = 0x30;             // Clear & stop timer   MTIMMOD = 155;   MTIMCLK = 0x10;             // Fixed freq clock, prescale 1   MTIMSC_TOF = 0;             // Ensure overflow flag is clear}

  

// Set timeout delay - 1 second increment, 255 seconds maximumvoid set_delay( byte delay){   delay_count = (word)delay * 100;   MTIMSC = 0x60;                  // Start timer, enable interrupt   MTIMSC_TOF = 0;                 // Ensure overflow flag is clear   tflag = 0;                      // Clear timeout flag}// Polling function to be frequently called - test for delay timeoutvoid test_timeout( void){   if (tflag) {                    // Test for timeout       tflag = 0;                   // Clear timeout flag      // Code for delay timeout action here   }} 

  

// MTIM ISR function:interrupt void MTIM_ISR( void){   MTIMSC_TOF = 0;                  // Clear overflow flag   if (delay_count) {      delay_count--;                // Decrement counter if non-zero      if (delay_count == 0) {       // Test for timeout occurred         tflag = 1;                 // Set timeout flag         MTIMSC = 0x70;             // Clear and stop timer      }   }}

 

A disclaimer - this specific code has not been tested.  You will need to do the testing and debug as might be required.  

 

Regards,

Mac

 

2,522 Views
PeterHouse
Contributor I

Santygo,

 

30s is a really long time delay to use code looping.  You should look into the use of a timer channel.  A timer channel and interrupt wil let your code set it and forget it and an interrupt will be generated at the end of 30s.

 

I am not that familiar with Code Warrior.  You can look at the assembly code for the looping and cross reference the code manual and calculate the loop timing based on the execution time of each cpu instruction.  It is tedious and very accurate.

 

Good Luck,

 

Peter House

0 Kudos
Reply

2,522 Views
peg
Senior Contributor IV

Hello

 

Quite often delays in the order of seconds don't need to be very accurate. With this in mind I often simply use the timers overflow interrupt to decrement a counter. This will give you a timer that is accurate to one overflow period. This also does not tie up the actual timer registers and they can be used for concurrent, shorter more accurate uses.

0 Kudos
Reply