0
\$\begingroup\$

I am trying to use timer1 interrupt on PIC18F26K83. I use Micro C as compiler. So I set my timer for 30 ms and after that 30 ms, it is supposed to enter an interrupt and light the LED. But it never lights up the LED (never enters the interrupt/interrupt never occurs). The code is:

#define led LATA.RA2
void Clk_8Mhz(){
    //8 MHz clock
    //  OSCCON1 REGISTER
    NOSC2_BIT=1;
    NOSC1_BIT=1;
    NOSC0_BIT=0;//HF INTERNAL OSC
    //OSCFRQ REGISTER HFINTOSC FREQ. SELECTION
    OSCCON1 =0b01100000; //HFINTOSC, Divider =1;
    FRQ3_BIT=0;
    FRQ2_BIT=0;
    FRQ1_BIT=1;
    FRQ0_BIT=1;    // 0011 :8MHz     //0100 = 16 Mhz
}

//Timer1
//Prescaler 1:1; TMR1 Preload = 5536; Actual Interrupt Time : 30 ms

void InitTimer1(){
    T1CON      = 0x01;
    TMR1IF_bit = 0;
    TMR1H      = 0x15;
    TMR1L      = 0xA0;
    TMR1IE_bit = 1;
    INTCON     = 0xC0;
}

void main (){
    TRISA.B2=0;
    led=0;
    clk_8Mhz();
    InitTimer1();
    while(1){
        delay_ms(10);
    }
}

void Interrupt(){
    if (TMR1IF_bit){
        TMR1IF_bit = 0;
        TMR1H      = 0x15;
        TMR1L      = 0xA0;
        led        = 1;
    }
}

What could be the cause of this interrupt never occurring?

\$\endgroup\$
  • \$\begingroup\$ The datasheet says T1CON highest bit (7) is the "ON" bit. Could you could try "T1CON = 0x81"? Also, I think mikroC is case-insensitive, but you could also make the ISR "void interrupt()" as the docs say. \$\endgroup\$ – RaphaelP May 7 at 13:28
  • \$\begingroup\$ But according to datasheet TxCOn bit 7 is unused bit (Page 298) \$\endgroup\$ – Günkut Ağabeyoğlu May 7 at 13:45
  • 1
    \$\begingroup\$ You're right. I was reading the wrong timer. Have you set the timer source (page 300)? The default value is external clock. You'd probably want it to Fosc."T1CLK = 0x02;" \$\endgroup\$ – RaphaelP May 7 at 14:12
  • \$\begingroup\$ It's interesting how the TxCON registers for Timer 1/3/5 (page 299) are different in layout from the TxCON registers for Timer 2/4/6 (page 324)! Very confusing, it took me a while to figure it out... \$\endgroup\$ – MartinF May 8 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.