problem with timer1 in pic16f877a

Status
Not open for further replies.

preethin

Newbie level 5
Joined
May 21, 2013
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,344
how to load the values in timer1 and also how to reload it for every 500ms
 

You cannot have a timer of 500 ms. Have a timer of say 10 ms and increment a counter on every 10 ms timer interrupt. So, when counter = 50 i.e., 10 ms * 50 = 500 ms you can set a flag. Use this flag in the while(1) loop to execute so other code. After every 10 ms timer interrupt you have to reload the values of timer. So, if your timer values are say 0X1F and 0xDB then in the interrupt routine add the below lines.

//for timer1
TMR1H = 0x1F;
TMR1L = 0xDB;

Remember. You have to reload it every 10 ms interrupt. If you choose 15 ms interrupt then you will be reloading values at every 15 ms.
 

void timer_init()
{
T1CON=0x31;
TMR1H=0x0b;
TMR1L=0xdc;
while(TMR1IF==0)
{
var=0;
TMR1IF=0;
TMR1H=0x0b;
TMR1L=0xdc;
var++;
if(var==5)
set_flag=1;
var=0;
}

}
what is the problm with this timer func
why it is not executing has a delay for 500ms
 

You can't have a delay of 500 ms. Where is your timer1 interrupt code? When there is a timer1 interrupt then TMR1IF will be set. So, after reloading the timer you have to clear it so that another timer1 interrupt can happen. In you code after the first interrupt TMR1IF will be 1 and so the while(TMR1IF == 0){...} bloak will execute before Timer1 is started and doesn't execute after the first interrupt.

An example code is like this.


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:2; TMR1 Preload = 25535; Actual Interrupt Time : 10 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON  = 0x11;
  TMR1IF_bit     = 0;
  TMR1H  = 0x63;
  TMR1L  = 0xBF;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H    = 0x63;
    TMR1L    = 0xBF;
    //Enter your code here
  }
}

 

u can genrate 500ms if u use 4 Mhz & prescaler =8 so ur time resolution = 8 us so for 500 ms required count = 62500 so u load timer1 => 65536-62500 => 0x0BDC in Timer1 reg.
 
Last edited:

How u say T=0.25 us

- - - Updated - - -

#include <PIC.h>
__CONFIG (0x0184); // According to ur controller Crystal consider = 4Mhz
//=========================================================
void delay(unsigned int byte);
//=========================================================
void interrupt TMR_INTR (void)
{

if(T1IF)
{
T1IF = 0;
TMR1H =0x0B;
TMR1L =0xDC;
RB7 = !RB7;
}
}
//=========================================================
void main(void)
{
delay(10000);
TRISB =0;
delay(10000);
TMR1IF = 0;
TMR1ON = 0;
T1CON = 0x30; // Prescale 8
TMR1H = 0x0B;
TMR1L = 0xDC;
TMR1IE = 1;
TMR1ON = 1;
while(1)
{
// ur application code
}
}
//=========================================================
void delay(unsigned int byte)
{
while(byte--);
}
//=========================================================
 

I already mentioned pre scaler=8 of timer1 so divide by 8 for timer1 not other process timing. so ur timer1 resolution is Fosc/4/8 = > 4e6/4/8 = >125000 Hz & t= 8 us. U can check code also which is already post. Still any doubt refer datasheet
 

how to load the values in timer1 and also how to reload it for every 500ms

Hi,

Through the link and description tab in it , so that you can how to set any value to raise a timer interrupt. You have a sample program describing it.

**broken link removed**
 

Yes. I think you are right Golden Electronics. But I am getting 0x0BDB and not 0x0BDC for timer values.

4 MHz/4 = 1 MHz

1 MHz/8 = 125000

Ttimer = 8 us

500 ms / 8 us = 0.5e6/8 = 62500

65535 - 62500 = 3035 (as it counts from 0 to 65535)

0x0BDB
 

Yes. I think you are right Golden Electronics. But I am getting 0x0BDB and not 0x0BDC for timer values.

just check how you calculated to get 0x0BDC probably some where rounding off might cause the difference.

enable the interrupt and write in the interrupt function what you want to do !

_T1Interrupt(void){
// Clear Timer1 interrupt flag
// Enter here what you want to do !
}
 

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…