Hello,
I´m beginner programmer with MC9RS08KA in C, and would like to help to create a delay of 700ms. e.g. After press a button, one led turn on after 700ms. I knew the function delay_ms() used in PICs, but I don't know how make with codewarrior. Anyone know?
tks.
Hello, and welcome to the forum.
The 'RS08KA8 device has two timer modules, a MTIM and a TPM. Either could be used to generate a delay of 700ms. I will explain a possible approach using the MTIM module.
Since the 'RS devices do not have a proper interrupt capability, you will need to poll for the occurrence of MTIM overflow. Depending on your requirements, you could generate the delay in one 700ms increment, or alternatively in a number of smaller increments of perhaps 10ms each, and to count the number of overflows for the required delay period. If you have a number of different intervals to generate, and maybe run concurrently, the second method would be the preferred one.
You would need to select the fixed frequency clock source to obtain a sufficiently long timing period, i.e. a frequency between the limits 15.625kHz and 19.531kHz, depending on the internal reference clock trim. This is the internal reference frequency divided by two. It is then a matter of setting the prescale value and the MTIMMOD value to obtain the required delay.
For example, a trim setting for a fixed frequency clock of 15.625kHz, a prescale setting of 64, and MTIMMOD = 170 would produce a delay in the vicinity of 700ms. The delay formula is PS*(MTIMMOD + 1) / FFCLK.
Regards,
Mac
Hello Pedro,
I think that it would be a good idea for your delay processing to be generally applicable, and not specifically for the timing of a LED. To do this, I think that you would need two functions - one function to setup the delay in the first instance, and a second function that could be periodically polled to test for a delay timeout condition. Both these functions can then be called from your main loop to meet the requirements of your application.
// Global variable:unsigned int q;void set_delay( unsigned int x){ q = x; MTIMSC = 0x20; // Counter reset MTIMSC_TOF = 0; // Clear flag}unsigned int test_timeout( void){ if (MTIMSC_TOF) { MTIMSC_TOF = 0; // Clear flag if (q) q--; } return q; }
The following code snippet might be used to control the LED.
LAMP = 0;set_delay( 50); // 1 second delaywhile (test_timeout()); // Wait for timeoutLAMP = 1;
If the COP (watchdog) is enabled, you will also need to clear the COP timer while you wait for timeout to occur.
Regards,
Mac
The professional way is to use one of the on-chip timers or the RTC (real time clock). You'll have to read about those in the manual.
The hobbyist way is to burn away all CPU capacity in a counting loop with a volatile counter variable. You will have to disassemble the code and count the instructions and their cycles, or alternatively write the function in assembler.
The same for contact debouncing: you will have to debounce the button with a timer, no matter if you are using Freescale or PIC.