A timer Interrupt to cause delay

In summary, the conversation was about using timer interrupts in a program to cause a delay in displaying binary numbers. The code provided was for a specific microcontroller, and there was confusion about the purpose and implementation of interrupts in the context of operating systems. There was also a question about the functionality of the provided interrupt handler.
  • #1
Mathslova
7
0
I am new to interrupts, so i have made a simple program using a timer interrupt to cause a delay of about 1 sec in displaying the binary numbers. However I have failed to achieve this.

Anyone knows the problem with my program?

any assitance will be highly appreciated.

#include <p24FJ128GA010.h>

_CONFIG2( FNOSC_FRCPLL & OSCIOFNC_OFF ); // Internal oscillator - 4xPLL giving 16 MIPS
// pin RC15 has the oscillator

int main()
{
//Configure the device to use the internal oscillator
OSCCONbits.COSC = 1u; // Internal oscillator with PLL
CLKDIVbits.RCDIV = 0u; //Internal oscillator not divided (8MHz)
CLKDIVbits.DOZEN =0; //No post scaling
CLKDIVbits.DOZE =0; //



//Set the LSB of port A to be outputs
TRISA = 0xFF00;

//TIMER1 Configuration Register
T1CON = 0;
T1CONbits.TCKPS = 0b11; //Prescale = 256
T1CONbits.TON = 1; //Timer on
TMR1 = 0; //Timer = 0

//Configure TIMER1 interrupt
_T1IP = 4; //Interrupt priority = 4
PR1 = 62500-1; //Period Register

_T1IF = 0; //Clear timer interrupt flag
_T1IE = 1; //Switch on timer 1 interrupt enable

TMR1 = 0;

while(1)
{
Idle();
asm("nop");
PORTA = 0b11111111;
PORTA = 0b00000000;

}//end while loop


}//end main loop





//TIMER1 Interrupt
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt()
{

_T1IF = 0; //Clear interrrupt flag before returning
}
 
Technology news on Phys.org
  • #2
I am aware of timer interrupts. I've used the sleep(int seconds) procedure to set a thread to sleep for a set number of seconds. You can have a process wait for a resource with the wait() function or its equivalent and have the process currently using the process signal the waiting process when its done.

But, if you're only familiar with interrupts in the programming sense, then you might not be aware of the purpose of interrupts. In Operating Systems, an interrupt stops the currently running process so that the process which was formerly blocked-waiting for an event, can now run.

For instance, your keyboard is able to accurrately able to show all characters you're typing in the rate at which you're going, eventhough other processes (such as the ones involving your monitor screen) are operating, with the use of interrupts.

However, I wonder how does one in a UNIX/Unix-like system implement interrupts without the usage of mutexes, semaphors, and the like.
 
  • #3
It looks as if you are running this code on some kind of microcontroller? Which one?

Although I'm not familiar with the specific microcontroller you're using, it doesn't appear to me that this code should be expected to do anything. It looks to me like 1 second after the line _T1IE = 1; we should expect _T1Interrupt to trigger. But it doesn't look like T1Interrupt actually does anything? It looks like it just marks the interrupt serviced and returns. As michinobu points out interrupts are, well, "interrupts", the code will not be waiting around after the _T1IE = 1; line for the interrupt to occur.

What is it you think this interrupt handler is doing?
 

1. How does a timer interrupt cause a delay?

A timer interrupt is a signal sent by a timer to the processor, indicating that a specific amount of time has passed. When the timer interrupt is received, the processor pauses its current task and executes a function to handle the interrupt. This function can include instructions to cause a delay, such as waiting for a certain number of timer interrupts to occur before resuming the original task.

2. What is the purpose of using a timer interrupt for delay?

The main purpose of using a timer interrupt for delay is to free up the processor to perform other tasks while waiting for the delay to finish. This allows for more efficient use of the processor's resources and can prevent the system from becoming unresponsive while waiting for the delay to finish.

3. Can a timer interrupt cause a delay in real-time applications?

Yes, a timer interrupt can be used to cause a delay in real-time applications. However, it is important to carefully design and implement the interrupt handling function to ensure that the delay does not impact critical tasks or cause unexpected delays in the system.

4. Is a timer interrupt the only way to cause a delay?

No, a timer interrupt is not the only way to cause a delay. Other methods include using software loops or built-in delay functions. However, a timer interrupt is often preferred as it allows for more precise timing and allows the processor to perform other tasks while waiting for the delay to finish.

5. How can the delay time be adjusted using a timer interrupt?

The delay time can be adjusted by changing the timer's settings, such as the frequency or interval at which the interrupt is triggered. This can be done through programming or by using a timer with adjustable settings. Alternatively, the delay time can also be controlled by the instructions in the interrupt handling function.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Electrical Engineering
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Electrical Engineering
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
7K
Back
Top