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.

pic timer 0, timer delay is not working

Status
Not open for further replies.

Teja.p

Member level 2
Joined
May 29, 2016
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,727
pic timer 0 , tiimer delay is not working

i am using pic16f73 and timer0 and clock frequency 8MHZ
of prescaler 1:16
by using formula
vaule= (required delay* fosc/4*prescaler


for 1ms delay
value =125
but it was not working real time.....its output delay coming around 25micro se

Code:
void delay(unsigned int a)
{
unsigned int i=0;
for(i=0;i<a;i++)
{
;
   OPTION_REG=0x46;
     
           TMR0IF_bit = 0;                                // Clear interrupt bit
       TMR0IE_bit= 1;                                // Enable Timer1 interrupt
        PEIE_bit= 1;                                 // Enable peripheral interrupts
        GIE_bit= 1; 
     TMR0=125;
;
         while(TMR0IF_bit==1)  //If Timer1 Interrupt
        {
                TMR0IF_bit= 0;    // clear the interrupt
        }                              
        
     }   
        }
please help
how generate 1ms sec delay
 
Last edited by a moderator:

Re: pic timer 0 , tiimer delay is not working

Try this code for 8 MHz Crystal.

Code:
//Timer0 
//Prescaler 1:8; TMR0 Preload = 6; Actual Interrupt Time : 1 ms   
//Place/Copy this part in declaration section 

void InitTimer0(){   
OPTION_REG	 = 0x82;   
TMR0	= 6;   
INTCON = 0xA0; 
}   

void Interrupt(){   
if (TMR0IF_bit){      
TMR0IF_bit	 = 0;     
TMR0 = 6;     
//Enter your code here   
} 
}
 
  • Like
Reactions: Teja.p

    Teja.p

    Points: 2
    Helpful Answer Positive Rating
Re: pic timer 0 , tiimer delay is not working

Timer 0 on that chip count UP from the TMR0 value until it overflows from 0xff to 0x00 when the IF bit is set (see Section 5 in the data sheet).
Because the TMR0 register goes back to 0, you need to reset it to something like the calculated value if you want the counter to continue triggering at the same intervsl.
(If it is not critical that your delay is exact, then just use the calculated value again. However if it is critical then I suggest you use the ISR approach that Okada has shown (but test for both the IF and IE bits being set) and trigger something off from within the ISR (using a volatile variable), then you need to consider the overheads in triggering an ISR. If you do need to go down this path, get back to us and we can guide you as to how to do this).
Susan
 

Re: pic timer 0 , tiimer delay is not working

thank u for u r reply sir
but i was not working
why u wrote TMR0 = 6;
how u calculate TMR0 = 6; for 1ms delay
please help me sir
how to generate 1ms delay by using pic16f73
 

Re: pic timer 0 , tiimer delay is not working

hello,


read again Susan's response on #3

prescaler set to 1/8
Fosc=8Mhz
timer0 will count from 6 to 256 ( because of 8 bits size 256 => 0 !)
so TMR0IF flag will be set after 256-6=250 pulses
FOSC=8 Mhz internal cycle duration 4/8= 0,5 µS
periode * 8 because divider = 1/8
250* 4/8 * 8 = 250 * 0.5 * 8 = 1000 µS => 1µS

if you are lazy, you can use mikroE Timer calculator
but you will never progress..
timer_calculator.JPG
 

Re: pic timer 0 , tiimer delay is not working

Dear sir
i tried that what u said but it was not working
please help me sir
....
 

Re: pic timer 0 , tiimer delay is not working

You are really not helping us to help you.
What exactly do you mean by "...not working"?
Can you compile your code?
Can you program your chip?
Is you chip running at all?
What does all of your code look like now?
What is it supposed to do and what is it actually doing?
How are you testing this?
Susan
 

Re: pic timer 0 , tiimer delay is not working

i want to turn an led for 1sec and turn off led for 1sec.
i tried u r code and compile u r code on micro and tested on proteus software ...
 

Re: pic timer 0 , tiimer delay is not working

I'm sorry to say but you really need to learn how to ask for help. I ask you a number of questions to guide you but you have not answered them.
I have not provided you with any code. The only code is a snippet of yours and a corresponding snippet from Okada. Therefore we can't see the code you are referring to about turning a LED on and off.
Please post all of your code, including the configuration settings.
If you are using an external clock or crystal, please tell us the frequency it is running at.
You say you want to turn a LED on and off every second - OK so what is it ACTUALLY doing?
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top