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.

[SOLVED] generate time delay using timer

Status
Not open for further replies.

shreyas_patel21

Full Member level 3
Joined
Jan 4, 2011
Messages
181
Helped
15
Reputation
30
Reaction score
14
Trophy points
1,298
Activity points
2,325
how to generate 1 us time delay using timer module of pic
i have the code for it
here it is
void delay(unsigned int us)
{
unsigned int x;
T1CON=0x0000;
for(x=0;x<us;x++)
{
TMR1=0x0000;
T1CON=0x8010; //timer 1 enable with prescale of 1:8
while(TMR1<0x02);
T1CON = 0x0000;
}
}
can anyone explain me why 0x02 is chosen for 1 us in TMR1.?
 

can you please post the full code here. missing information on the oscillator configurations
 

thank you john,


#include <stdio.h>
#include <p24fj16ga002.h>
#include "math.h"
#include "i2c.h"
#include "string.h"




_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & COE_OFF);
_CONFIG2(IESO_OFF & FNOSC_FRCPLL & FCKSM_CSECME & OSCIOFNC_OFF & POSCMOD_NONE & IOL1WAY_OFF & I2C1SEL_PRI);

i think oscillator is configured for 16MHz
i have attached the header file for your information
 

Attachments

  • p24FJ16GA002.h.txt
    132.7 KB · Views: 64

ok. your prescaler is 1:8 and your oscillator is 16MHz. since its prescaled, now the oscillator for the timer is 2MHz. To find the time, take t = 1/T, 1/2MHz = 500ns. Thus letting TMR 1 to count to 0x02, you will get 1us :)
 

    V

    Points: 2
    Helpful Answer Positive Rating
Hey Shreyas,

What John has explained about timing is very correct, each machine cycle takes
500ns, so for 1uS u need to have 2 machine cycles or two timer count..

but your code will give more than 1 us delay,
This is you code

void delay(unsigned int us)
{
unsigned int x;
T1CON=0x0000;
for(x=0;x<us;x++)
{
TMR1=0x0000;
T1CON=0x8010; //timer 1 enable with prescale of 1:8
while(TMR1<0x02);
T1CON = 0x0000;
}
}

you dont need to configure time 1 everytime you enter the for loop this will take one machine cycle extra...
Also while(TMR1<0x02); will tale more than 2 machine cycle..
if you change above code in assemebly, this will check the condition everytime, which will ad extra machine cycle..
Your for loop will also take extra machine cycle..
since you want delay in uS, its is very critical case..
this routine will give you more than double delay, than you expect in the code..
try to go in steps for 10uS delay
In msec delay for loop delay is negligible, though it is present...

I hope you are getting what I want to say...

__
Amit
** If you find my answer useful, please click the "Helpful? Please click" icon. **
 
thank you amit,
i got it,


void delay(unsigned int us)
{
unsigned int x;

T1CON=0x8010; //timer 1 enable with prescale of 1:8
for(x=0;x<us;x++)
{
while(TMR1<0x02);
}
T1CON=0x0000;
}
can this code work?
 

void delay(unsigned int us)
{
unsigned int x;

T1CON=0x8010; //timer 1 enable with prescale of 1:8

while(TMR1<us);

T1CON=0x0000;
}

Try using this piece of code..
i think this will give more accurate delay than you previous code..
U can heck it on oscilloscope
or make large delay of second using us delay routine & check it with LED blinking u will fill the differenece..

This is actually ZUGADU TARIKA...
Not the right practice...

Also dont assign SFR in the way u r assigning T1CON

If u set any bit T1CON |= 0x8010;
& for resettig T1CON &= 0x7FEF;
using this practice u will not affect the other bits of SFR if you are using it anywhere else.
Try this code & pls tell me is it working fine enough for u...:wink:
__
Amit
** If you find my answer useful, please click the "Helpful? Please click" icon. **
 

thank u,

i tried the code using leds and working good..
thanks for the sfr assigning style..

you told that its the zugadu tarika, and not in right practice then what is the right practice code?
 

Zugadu means... scope of this code is limited to this hardware configuration only..:wink:
There is nothing wrong in it..
I just wanted to say, you can make generic code for delay...
just play with hardware or software to get that code & it anywhere u want...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top