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){
}
}
// 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.
// 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.