This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer interrupt problem

Hello,

I have any Problems with my Timer Interrupt I use GPT1 but it doesn't work correctly.
I'm trying to implement a 1ms Timer, but it doesn't work.

Has anyone a sample for a 1ms or a 10ms second Timer on GPT1 Timer T3

Thanks for any help

Best regards
RAKO

Parents
  • try this:

    #include <reg167.h>
    #include <intrins.h>
    static unsigned long volatile timer_tick = 0UL;
    static void timer3_irq (void) interrupt T3INT = 35 {
    timer_tick++;
    }
    void timer3_setup (unsigned int ticks_per_sec) {
    unsigned int reload;
    unsigned long frequency = 2500000;
    unsigned int prescale;
    for (prescale = 0; prescale < 8; prescale++) {
    if ((frequency / ticks_per_sec) <= 65535) break;
    frequency /= 2;
    }
    reload = frequency / ticks_per_sec;
    T3CON = 0x0080; /* 2.5MHz, Timer Mode, Count Down */
    T3CON |= prescale;
    T2CON = 0x0027; /* Setup T2 for reload operation on any T3OTL */
    T2 = reload; /* Reload for T3 */
    T3 = reload; /* Start T3 with proper reload */
    T3IC = 0x0044; /* Timer 3 interrupt enabled, Level 1 */
    T3R = 1; /* Start Timer */
    IEN = 1; /* Enable Interrupts /
    }
    void timer3_delay (unsigned long ticks) {
    unsigned long start_tick;
    unsigned long timer_tick_copy;
    _atomic_ (0); /* start un-interruptable code */
    start_tick = timer_tick_copy;
    _endatomic_ (); /* end un-interruptable code */
    while (1) {
    _atomic_ (0); /* start un-interruptable code */
    timer_tick_copy = timer_tick;
    _endatomic_ (); /* end un-interruptable code */
    if ((timer_tick_copy - start_tick) > ticks) break;
    }
    }
    void main (void) {
    DP7 = 0x01; /* Setup P7.0 for output */
    ODP7 = 0x01; /* Setup P7.0 for open-drain */
    timer3_setup (1000); /* Setup timer for 1000Hz operation */
    while (1) {
    timer3_delay (100); /* Wait 0.10 seconds */
    P7 |= 0x01; /* Turn on P7.0 */
    timer3_delay (100); /* Wait 0.10 seconds */
    P7 &= ~0x01; /* Turn off P7.0 */
    }
    }
    

Reply
  • try this:

    #include <reg167.h>
    #include <intrins.h>
    static unsigned long volatile timer_tick = 0UL;
    static void timer3_irq (void) interrupt T3INT = 35 {
    timer_tick++;
    }
    void timer3_setup (unsigned int ticks_per_sec) {
    unsigned int reload;
    unsigned long frequency = 2500000;
    unsigned int prescale;
    for (prescale = 0; prescale < 8; prescale++) {
    if ((frequency / ticks_per_sec) <= 65535) break;
    frequency /= 2;
    }
    reload = frequency / ticks_per_sec;
    T3CON = 0x0080; /* 2.5MHz, Timer Mode, Count Down */
    T3CON |= prescale;
    T2CON = 0x0027; /* Setup T2 for reload operation on any T3OTL */
    T2 = reload; /* Reload for T3 */
    T3 = reload; /* Start T3 with proper reload */
    T3IC = 0x0044; /* Timer 3 interrupt enabled, Level 1 */
    T3R = 1; /* Start Timer */
    IEN = 1; /* Enable Interrupts /
    }
    void timer3_delay (unsigned long ticks) {
    unsigned long start_tick;
    unsigned long timer_tick_copy;
    _atomic_ (0); /* start un-interruptable code */
    start_tick = timer_tick_copy;
    _endatomic_ (); /* end un-interruptable code */
    while (1) {
    _atomic_ (0); /* start un-interruptable code */
    timer_tick_copy = timer_tick;
    _endatomic_ (); /* end un-interruptable code */
    if ((timer_tick_copy - start_tick) > ticks) break;
    }
    }
    void main (void) {
    DP7 = 0x01; /* Setup P7.0 for output */
    ODP7 = 0x01; /* Setup P7.0 for open-drain */
    timer3_setup (1000); /* Setup timer for 1000Hz operation */
    while (1) {
    timer3_delay (100); /* Wait 0.10 seconds */
    P7 |= 0x01; /* Turn on P7.0 */
    timer3_delay (100); /* Wait 0.10 seconds */
    P7 &= ~0x01; /* Turn off P7.0 */
    }
    }
    

Children
No data