Help understanding the Timer Calculator and Timer1 Interrupt

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 8-bit PIC MCUs.
Post Reply
Author
Message
Ditch
Posts: 71
Joined: 23 Jan 2017 23:14

Help understanding the Timer Calculator and Timer1 Interrupt

#1 Post by Ditch » 24 Jan 2019 12:46

Hi There,

I'm after a little help or pointers regarding the Timer 1 interrupt using a PIC16f1847, MikroC Pro and 8MHZ MCU clock.

I've added the code to my project which was created by the Timer Calculator.

Code: Select all


//Timer1
//Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON	 = 0x21;
  TMR1IF_bit	 = 0;
  TMR1H	 = 0x3C;
  TMR1L	 = 0xB0;
  TMR1IE_bit	 = 1;
  INTCON	 = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H	 = 0x3C;
    TMR1L	 = 0xB0;
    //Enter your code here
  }
} 
I'm using this to try to get an LED to flash at exactly 1HZ.

I'm obviously missing something because I was expecting this interrupt to occur every 100ms but I am seeing this happen at just less than 2 seconds and not the 100ms that I was expecting.

This is me. I'm trying to learn and understand this so any help would be appreciated.

Ian


The actual current code is;

Code: Select all



sbit Relay at RB3_bit;
sbit Led at RB2_bit;

unsigned short cnt = 0;

//Timer1
//Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 100 ms

//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON	         = 0x21;
  TMR1IF_bit   	 = 0;
  TMR1H	         = 0x3C;
  TMR1L	         = 0xB0;
  TMR1IE_bit	 = 1;
  INTCON	 = 0xC0;
}

void Interrupt(){
  if (TMR1IF_bit){
    TMR1IF_bit = 0;
    TMR1H	 = 0x3C;
    TMR1L	 = 0xB0;

    cnt++;
  }
}



void main() {

  //CMCON = 0x07;        // turn off comparators
  PORTA = 0;             // All PORTA pins are cleared
  TRISA = 0b00011111;    //
  PORTB = 0;             // All PORTB pins are cleared
  TRISB = 0b11110011;    //
  
  OSCCON = 0x72;

  InitTimer1();          // Initiate Timer 1
  
  LED = 1;               // Switch the LED On
  RELAY = 0;             // Switch the RELAY Off


    do {
     
    if( cnt == 10 ){ LED = 0; }                 // cnt is incremented by the Intrrupt
    if( cnt == 20 ){ LED = 1; cnt = 0;}

    } while (1);
}

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: Help understanding the Timer Calculator and Timer1 Inter

#2 Post by filip.grujcic » 25 Jan 2019 12:50

Hello,

Have you made sure you disabled the PLLEN bit in config bits?
Could you zip and attach your project here?

Kind regards,
Filip Grujcic

Ditch
Posts: 71
Joined: 23 Jan 2017 23:14

Re: Help understanding the Timer Calculator and Timer1 Inter

#3 Post by Ditch » 26 Jan 2019 12:24

Hi Filip,

Thanks for your response. I must have had something wrong but i've since fixed this and timing is now perfect.

For anybody else struggling with this, the above code does work. You need a 32.768 crystal on pins T1OSI & T1OCO and the main chip oscilator set as the LP type.

Thanks for your help.

Ian

Post Reply

Return to “PIC PRO Compilers”