Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Timer0 Timeout.

Status
Not open for further replies.
okay... now timer2 ain't working. Config settings same as before, here's the code:

C:
void main() {
     TRISD=0;
     PORTD=0;
     T2CON=0b01111011;
     PEIE_bit=1;
     GIE_bit=1;
     TMR2=0;
     TMR2IE_bit=1;
     TMR2IP_bit=1;

     
     while (1){
       TMR2IF_bit=0;
       PR2=0b11111111;
       TMR2ON_bit=1;
       if (TMR2IF_bit==1){
          PORTD=~PORTD;
          TMR2ON_bit=0;
             
        }
      
     }
     
}

pretty strange. I thought I had this one in the bag already, but I was mistaken. PORTD didn't even turn on. :(

Appreciate the patience with me. :)
 
A couple of things that are not quite right,
You are polling and so don't need an interrupt.
You are reseting the TMR2IF every loop.

Try,
Code:
void main() {
    TRISD=0;
    PORTD=0;
    T2CON=0b01111011;
    //PEIE_bit=1;
    //GIE_bit=1;
    TMR2=0;
    //TMR2IE_bit=1;
    //TMR2IP_bit=1;
    TMR2ON_bit=1; 
    PR2=0b11111111;
 
    while (1){
       if (TMR2IF_bit==1){
           TMR2IF_bit=0;
           PORTD=~PORTD;
        }
    } 
}

Note I moved setup code to before the while loop and remed out the interrupt code.

Edit, this may still be too fast, it is timeing out 76 times per second assuming 20MHz crystal.

Mike.
 
Last edited:
Aaaaah yes it worked. I had this thought that since as the name "Timer Interrupt Flag" itself suggests, it looked like as if I needed to activate the interrupt settings too, but as a friend told me, "You're not doing any interrupt programming, it's only polling".

Note I moved setup code to before the while loop and remed out the interrupt code.

I wonder, why does it need to be so? :)
 
For doing timing things like this I normally setup an interrupt at 100Hz and use that to time things.

So, assuming I want to flash the port at 2Hz (250mS on, 250mS off), I would do,
Code:
void interrupt() {
    if (TMR2IF_bit==1){
        TMR2IF_bit=0;
        Count100th++;
    }
}    
 
void main() {
    PEIE_bit=1;
    GIE_bit=1;
    TRISD=0;
    T2CON=0x4d;         //Timer2 on, pre=4, postscaler 10
    PR2=249;            //PR2=249 gives a 10mS interval
    TMR2IF_bit=0;      
    TMR2IE_bit = 1;
    while (1){
        if(Count100th==25){
            Count100th=0;
            PORTD=~PORTD;
        }
    } 
}
Note the above assumes a 4MHz crystal.

Mike.
 
Last edited:
I have a question: Why is it a problem to reset TMR2IF first, then turn on the timer, since TMR2IF should certainly flag up when the TMR2 reaches the value of PR2?

In short, why this snippet:
C:
while (1){
       TMR2ON_bit=1;         //turn on the timer
       if (TMR2IF_bit==1){   //poll for the flag bit to go high
           PORTD=~PORTD;   
           TMR2IF_bit=0;       //reset the flag bit
           TMR2ON_bit=0;      //stop the timer
       }
}
doesn't work?
 
Why are you turning off the timer. If you do that then why are you surprised it didn't work?

Mike.
 
aaahh... now I see. I thought the loop would go back to the part where the TMR2ON is set, but it's not part of the "if", but only part of the "while" loop. My bad. :eek:
 
Sorry, my bad, I didn't realise that you were turning it back on again in the while loop. The code will go back to where the timer is turned back on and so I have no idea why that wouldn't work.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top