Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A timer Interrupt to cause delay

  1. Nov 27, 2008 #1
    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;

    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
  2. jcsd
  3. Dec 1, 2008 #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.
  4. Dec 1, 2008 #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?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook