TIMER0 Interrupt

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
emanuellima
Posts: 1
Joined: 12 Feb 2015 11:28

TIMER0 Interrupt

#1 Post by emanuellima » 12 Feb 2015 11:42

Hello all,

I am developing a project with a PIC18F45K22, and I want to use TIMER interrupts. A sensor will enable an interrupt at pin RB0, and then this interrupt will start the TIMER, when the timer is 1 min a LED should be turned ON.

I am using a internal osc at 16MHz.

Can you help me? how could I do it?

so far I was trying to use the code bellow

Code: Select all

void interrupt(void){

     if(INT0F_bit == 1){
        PORTD.B0  = 1;
        text2 = ("   INTERRUPT   ");

        T0CON.TMR0ON = 1;      //Now start the timer!
        INT0F_bit = 0;
        Delay_ms(500);
        PORTD.B0  = 0;
        //text2 = ("  Temperature:   ");
     }
     else if(T0CON.TMR0IE && T0CON.TMR0IF){
      //TMR0 Overflow ISR
      counter++;  //Increment Over Flow Counter

      if(counter == 61)
      {
         //Toggle RB1 (LED)

         if(PORTD.B5  = 0)
            PORTD.B5 = 1;
         else
            PORTD.B5 = 0;
            
         counter = 0;  //Reset Counter

      }

      //Clear Flag
      T0CON.TMR0IF = 0;
      }

}
and the main menu

Code: Select all

  INTCON.GIE  = 1;                   // Enable GLOBAL interrupts
  INTEDG0_bit = 1;                   // Set interrupt on rising edge
  INT0IE_bit  = 1;                   // Enable INT0 interrupts
  INTCON.PEIE = 1;                   //peripharel interrupt enable
  INTCON.TMR0IF = 0x0; //Clear timer0 overflow interrupt flag
  INTCON.TMR0IE = 1;   //Enable TIMER0 Interrupt
  T0CON.T08BIT = 0;    //8 BIT MODE
  T0CON.T0CS = 0;      //Prescaler gets clock from FCPU (5MHz)
  T0CON.PSA = 0b111;       //Timer Clock Source is from Prescaler

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: TIMER0 Interrupt

#2 Post by darko.minic » 13 Feb 2015 10:03

Hello,

We provide examples for timer0 interrupt, you can find it in compiler examples folder:
\Mikroelektronika\mikroC PRO for PIC\Examples\Internal MCU modules\P18F45K22\Timer0 Interrupt

Also if you want to use external interrupt you should look at following example for UNI Hall Click:
http://www.libstock.com/projects/downlo ... oc_pic.zip

You should look at this examples and try to implement them in your code.

Regards,
Darko

jayanthd
Posts: 630
Joined: 08 May 2013 18:31
Location: Bangalore

Re: TIMER0 Interrupt

#3 Post by jayanthd » 13 Feb 2015 23:41

Try attached code. Proteus 8.1 file attached.

Use mikroE Timer Calculator tool.

Code: Select all

unsigned int counter = 0;

//Timer1
sbit LED at LATD0_bit;
sbit LED_Direction at TRISD0_bit;

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

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

void Interrupt() {

    if(INT0IF_bit) {
         counter = 0;
         InitTimer1();
         INT0IF_bit = 0;
         INT0IE_bit = 0;
    }

    if(TMR1IF_bit) {
    
         if(++counter == 600) {     //counter value = 600 = 60 sec (100 ms timer Interrupt)
              LED = 1;
              counter = 0;
              TMR1ON_bit = 0;
              INT0IE_bit = 1;
         }
    
        TMR1IF_bit = 0;
        TMR1H         = 0x3C;
        TMR1L         = 0xB0;
        //Enter your code here
    }
}

void main() {

     ANSELA = 0x00;
     ANSELB = 0x00;
     ANSELC = 0x00;
     ANSELD = 0x00;
     ANSELE = 0x00;
     
     CM1CON0 = 0x00;
     CM2CON0 = 0x00;
     
     TRISA = 0xFF;
     TRISB = 0x01;
     TRISC = 0x00;
     TRISD = 0x00;
     TRISE = 0x08;
     
     PORTA = 0x00;
     PORTB = 0x00;
     PORTC = 0x00;
     PORTD = 0x00;
     PORTE = 0x00;
     
     LATA = 0x00;
     LATB = 0x00;
     LATC = 0x00;
     LATD = 0x00;
     LATE = 0x00;
     
     LED_Direction = 0;
     LED = 0;
     
     IRCF0_bit = 1;
     IRCF1_bit = 1;
     IRCF2_bit = 1;
     
     INTEDG0_bit = 1;
     INT0IF_bit = 0;
     INT0IE_bit = 1;
     PEIE_bit = 1;
     GIE_bit = 1;
     
     while(1) {
     
     }
}
Attachments
ext int timer.png
ext int timer.png (61.04 KiB) Viewed 8698 times
PIC18F45K22 Ext Int + Timer.rar
(106.62 KiB) Downloaded 314 times
[HW:] EasyPIC v7, mikroProg PIC, AVR, STM32, PSoC, Tiva, 8051
[SW:] mikroC PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroBasic PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
mikroPascal PRO PIC, dsPIC, PIC32, AVR, ARM, 8051
Visual GLCD, Visual TFT

User avatar
darko.minic
Posts: 747
Joined: 01 Dec 2014 11:10

Re: TIMER0 Interrupt

#4 Post by darko.minic » 18 Feb 2015 11:02

Hello Jayanth,

Thank you for sharing your knowledge and experience with our users.

Best regards,

aparna k
Posts: 55
Joined: 17 Jul 2019 07:17

Re: TIMER0 Interrupt

#5 Post by aparna k » 11 Oct 2019 05:28

Hi,
I am trying to implement timer0/timer1 interrupt using PIC18F67K40. But it is not working. Can anyone send me the code?

User avatar
jovana.medakovic
mikroElektronika team
Posts: 986
Joined: 18 Dec 2018 10:36

Re: TIMER0 Interrupt

#6 Post by jovana.medakovic » 15 Oct 2019 14:46

Hello,

In the attachment, you can find an example for timer0 and timer1 for PIC18F67K40.

Kind regards,
Jovana
Attachments
Timer1 Interrupt.zip
(58.03 KiB) Downloaded 247 times
Timer0 Interrupt.zip
(56.8 KiB) Downloaded 324 times

Post Reply

Return to “User Projects”