• 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.

Using TMR1 interrupt to generate 1 sec delay.

Status
Not open for further replies.

lloydi12345

Member
Hi, first of all it's my first time to use TMR1. I used TMR0 but as an external input interrupt so I really have no idea how to use TMR registers for timers or counters. I would like to make a 1 sec delay and portb will show it. Can you teach me about the calculations? I've read the datasheet and a lot of information from other websites but I can't understand them all. So far I managed to blink LEDs but I wanted to know how to exactly calculate time based from my internal clock. As far as I know, I need to place counters for me to increase the time but don't know how to start since I can't understand fully the calculations needed. Here's what I've started. Please help me to continue this.

Code:
void interrupt(void){
     if (PIR1.TMR1IF){               //Let me use this even if no other interrupts are enabled
     PORTB = ~PORTB;             //To show the output
     TMR1L = 100;                   //TMR1 start time
     TMR1H = 90;
     PIR1.TMR1IF = 0;              //reset timer1 flag
     }
}

void main() {

        portb = 0x00;
        trisb = 0x00;
        
        T1CON.TMR1CS = 0;                    // Fosc / 4
        T1CON.T1CKPS1 = 1;                   // Setting prescale value to 1:8
        T1CON.T1CKPS0 = 1;                   //              "

        TMR1L = 100;                             //TMR1 start time
      [COLOR="Red"]  TMR1H = 90;    <<--------- If I change this one to 88 why is it not blinking anymore
[/COLOR]
        PIE1.TMR1IE = 1;
        INTCON.PEIE = 1;
        INTCON.GIE = 1;
        
        T1CON.TMR1ON = 1;                    //turn on timer1

        while(1){
        }
}
Thank you for your time reading this thread.
 
Last edited:

Mr RB

Well-Known Member
Here is some code from this page; Zero-error 1 second timing algorithm

Code:
        // C code for a 1 second period with a 1MHz timer (4MHz xtal);
	// uses 1 variable; unsigned long bres
	// gets here every TMR0 int (every 256 ticks)

	bres += 256;	// add 256 ticks to bresenham total

	if(bres >= 1000000)	// if reached 1 second!
	{
		bres -= 1000000;	// subtract 1 second, retain error
		do_1sec_event();	// update clock, etc
	}
You need to change your interrupt to be the TMR0 interrupt, which is good as it leaves the more useful TMR1 available for other tasks. Then the code above goes in the TMR0 interrupt. :)
 

lloydi12345

Member
Here is some code from this page; Zero-error 1 second timing algorithm

Code:
        // C code for a 1 second period with a 1MHz timer (4MHz xtal);
	// uses 1 variable; unsigned long bres
	// gets here every TMR0 int (every 256 ticks)

	bres += 256;	// add 256 ticks to bresenham total

	if(bres >= 1000000)	// if reached 1 second!
	{
		bres -= 1000000;	// subtract 1 second, retain error
		do_1sec_event();	// update clock, etc
	}
You need to change your interrupt to be the TMR0 interrupt, which is good as it leaves the more useful TMR1 available for other tasks. Then the code above goes in the TMR0 interrupt. :)
Here's another link that shows how to generate a 1 sec delay using TIMER0.
Lab 7: PIC Timers and Counters (Part 1) :Embedded Lab
Your links are so useful. Thank you Mr RB and rajbex :)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top