Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

delay using pic timer1

Status
Not open for further replies.

luvuslim

Junior Member level 1
Joined
Sep 21, 2010
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,393
I am using pic16f877a timer1 to create 2ms delay. Crystal frequency is 20mhz.
I have loaded the value 55536 in tmr1 register to count 10000 but somehow its not working.
Code:
void main()
{
TRISB=0;
  PORTB=0;
  GIE_BIT=1;
  PEIE_BIT=1;
  T1CON=0X00;
  TMR1IE_BIT=0;
     TMR1L=0XF0;
TMR1H=0XD8;
      TMR1ON_BIT=1;
  while(1)
  {

if(TMR1IF_BIT==1)
{
TMR1ON_BIT=0;
TMR1H=0XD8;
TMR1L=0XF0;
 RB7_BIT=~RB7_BIT;

      TMR1IF_BIT=0;
       TMR1ON_BIT=1;
} } }
 

you must write your program like that:

void interrupt() {
*/ interruption traitement
}

void main() {
}
_______________________________________________

try this for your program.

*/ -----------------------------
*/ interruption ISR
*/ -----------------------------

void interrupt() {
TMR1ON_BIT=0;
TMR1H=0XD8;
TMR1L=0XF0;
RB7_BIT=~RB7_BIT;
TMR1IF_BIT=0;
TMR1ON_BIT=1;
}

*/ -----------------------------
*/ Main
*/ -----------------------------

void main() {
TRISB=0;
PORTB=0;
GIE_BIT=1;
PEIE_BIT=1;
T1CON=0X00;
TMR1IE_BIT=0;
TMR1L=0XF0;
TMR1H=0XD8;
while(1){
}}
 

My application is to drive a servo motor so i have created 20ms delay using timer0,which is working fine and want to create 1ms-2ms delay using timer1 so here is the code for 2ms delay ,but its not working...so plz help

Code:
unsigned int count,countt;
unsigned char output;

void interrupt()
{
  if(TMR0IF_BIT==1)
  {

    count+=1;
    TMR0IF_BIT=0;
    
  }
   if(TMR1IF_BIT==1)
  { TMR1ON_BIT=0;
  TMR1IF_BIT=0;
   
  }
  
  }
void Delay()
{
TMR1H=216;//values to generate 2ms delay for 20mhz crystal
TMR1L=240;
TMR1ON_BIT=1;
}
void main()

{


OPTION_REG=0x87;
 T1CON=0X80;
  TMR1IE_BIT=1;
  GIE_BIT=1;
  PEIE_BIT=1;
TMR0IE_BIT=1;
  TMR0=60;

  while(1)
  {


    switch(count )
    {
    case 1:
     
      RC1_BIT=1;
    
     Delay();

 RC1_BIT=0;
     
      TMR0=60;
      count=0;
      break;
}
}
}
 

I am new with PIC. And i'm trying to generate a 1sec delay using timer1 in pic16f877a

I wrote the following program, But the delay is not accurate enough. Could someone please help.


#include<htc.h>
__CONFIG(HS & PWRTEN & LVPDIS & WDTDIS & BORDIS);

void main()
{
int count =0;
TMR1ON = 1;
TMR1CS =0;
T1CKPS0=0;
T1CKPS1=0;
TMR1IF=0;
TMR1H = 0X0B;
TMR1L=0XDC;
TRISB=0X00;
RB1=0;
while(1)
{
while(!TMR1IF)
{}
count++;
if (count==16)
{
RB1=!RB1;
count=0;
}
TMR1H = 0X0B;
TMR1L=0XDC;
TMR1IF=0;
}
}
 

plz comment ur code so that I can understand how u r trying to generate delay..
 

In writing a value to a 16-bit timer, the order of the bytes is important.
Isn't this so for pic16f877a ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top