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.

how to produce milli seconds delay in pic timer0?

Status
Not open for further replies.

suvaraj

Member level 2
Member level 2
Joined
Dec 24, 2010
Messages
50
Helped
4
Reputation
8
Reaction score
4
Trophy points
1,298
Location
erode
Activity points
1,580
hi to all
i need to create milli second delay in pic timer0.i hav confuse with calculation.any one can help me to solve my problem.pls explain me the timer calculation clearly.
thank u
 

Hi, and first question - what is the frequency of XTAL?
 
Clock frequency divided by 4 gives instruction cycle time.
Multyply by timer prescale value gives timer increment time.

Example with 4 Meg clock and 1:8 prescale:

Instruction cycle time = 4/4 = 1M;

1/1000000 = 0.000001 seconds

Timer increment time = 0.000001 * 8 prescale = 0.000008 seconds;

Counts for 1mS = 0.001 / 0.000008 = 125;

Result 125 counts of timer take 1mS.

If you subtract the result from 256 and preload the timer with that value in an interrupt routine.
You will have a 1mS interrupt you can use as a clock;

Code:
/*--- Global ---*/

volatile unsigned int sysclock;

/*--- Interrupt routine ---*/

void interrupt int_vector(void)
  { 

  if(T0IF)       /* Timer_0 overflow interrupt ~1mS system clock */
    {
    sysclock++;
  
    TMR0 = 131;  /* Preload Timer_0 for 1mS interrupt */
    T0IF = 0U;
    }
   }

/*--- Delay routine ---*/

void delay_mS(unsigned int ms)
  {
  unsigned int timer = sysclock;

  while((sysclock - timer) < ms){
    ;
    }
  }

You don't have to worry about overflow when subtracting one unsigned from another.

You could also use a macro and let the compiler work it out for itself.

Code:
/*--- Macro to calculate 1mS Timer period (1:8 Prescale value) ---*/

#define FREQ_OSC  4.0
#define PRESCALE  8.0

#define TMR_1mS ((1000.0)/(((1.0)/((FREQ_OSC)/(4.0)))*(PRESCALE)))
 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top