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