timer 1 usage in pic16f877a

Status
Not open for further replies.

raf23

Newbie level 5
Joined
Jun 24, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
india
Visit site
Activity points
1,370
Hello

I have got a situation where I'm forced to use timer 1 to generate interrupt at lower time limits like 256 us. Being a 16 bit timer ,Is there anyway that i could use it as 8 bit timer or more specifically to generate interrupt after 256 Instruction cycles ?

Any help would be deeply appreciated.

Thanks in advance
 

What is the Fosc you are using and what timer delay do you want?

Here is an example of Fosc = 8 MHz, PIC16, timer for 260 us.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Timer0
//Prescaler 1:4; TMR0 Preload = 124; Actual Interrupt Time : 259 us
 
//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG     = 0x81;
  TMR0       = 124;
  INTCON     = 0xA0;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit   = 0;
    TMR0         = 124;
    //Enter your code here
  }
}



Another one for 50 us timer

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Timer0
//Prescaler 1:1; TMR0 Preload = 155; Actual Interrupt Time : 50 us
 
//Place/Copy this part in declaration section
void InitTimer0(){
  OPTION_REG     = 0x88;
  TMR0       = 155;
  INTCON     = 0xA0;
}
 
void Interrupt(){
  if (TMR0IF_bit){ 
    TMR0IF_bit   = 0;
    TMR0         = 155;
    //Enter your code here
  }
}



Edit: I didn't see that you need Timer 1 code. Here it is.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Timer1
//Prescaler 1:1; TMR1 Preload = 65015; Actual Interrupt Time : 260 us
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON  = 0x01;
  TMR1IF_bit     = 0;
  TMR1H  = 0xFD;
  TMR1L  = 0xF7;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H    = 0xFD;
    TMR1L    = 0xF7;
    //Enter your code here
  }
}

 
Last edited:

Timer1 is just a bigger version of Timer 0. Functionally it's almost identical so assuming you are counting up to generate the interrupt as it rolls over to zero, just make the high 8-bits all ones (TMR1H = 0xFF). It will then use the lower bits as an 8-bit counter like TMR0.

Brian.
 

You can generate short timeouts with TMR1 by using a preset value for so that the TMR1 registers will not start counting from 0.

Theres a formula for that, similar to calculating the preset for the TMR0. It goes something like this

TMR1 Preset value = 65536 - ((TMR1 interrupt period)x(Fosc)/(4)x(Prescaler))

Then use the preset value inside your ISR.

Code:
void interrupt isr(void)
{
  if(TMR1IE && TMR1IF)
  {
     //write the preset value to TMR1H && TMR1L register 

    //Add other statements
  }
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…