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 calculate time needed for instruction ?

Status
Not open for further replies.

apple

Newbie level 5
Joined
Feb 21, 2005
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,370
how to calculate time

hi, i am doing a countdown timer on PIC16F877..... may i know how can i calculate how much time an instruction need.....such as "movwf note" i am using 20Mhz PIC....
thanks for your help..
 

how to calculate time

Hi,

You can use one of the timers to interupt your software every 1ms. Then use simple counters to find out the second, minute, hour and so on. Thus your program can run frely without you needing to worry how long somehting happans.
Good luck.
 


how to calculate time

Spec of instruction will describe the period.
 

Re: how to calculate time

Hi,
From the datasheets of ur pic, they will actually indicate how long is one instruction cycle. Usually for PIC 16 series will be 200ns..
So if let say u are using timer1 to have a interrupt when overflow.
Timer1 has 16 bits so will have a count of up to 65535
Then below will be the time taken for each interrupt.
"200ns X number of count to cost interrupt x prescalar.."
usually for a start, can test with a prescalar of 1:1
then.. that will be ard 13.1m s
to have 1s interrupt, u need 1s , u need to have 76 of such interrupt cause by interrupts .
All the best.
btw, this is for the 20Mhz crystal
 

Re: how to calculate time

hi... thanks for all the suggestion given....but can i try it with loops to countdown instead of using interrupt?? because some of my friends told me thatinterrupt may cause some problem. Is there any caution that need to be careful when using interrupt??
 

Re: how to calculate time

Yes, u need to make sure that ur interrupt service routine has to be as short as possible.. Hmmm.. is fine with doing the countdown loop instead of interrupt I feel.
I am actually working on that now too.. I guess first u still got to make the timer as a counter n then let it overflow. When overflow, the overflow flag will be logic1. so u can make use of this to have a loop count down.. etc, whenever the overflow flag is a one, then decrement ur count loop..
Hpe it helps .
 

Re: how to calculate time

Hi,

Idont link to use loops, because the MCU stops doing other things. You can compare it to a NON multitask to a multitask MCU. If you are jsut building a clock, then ok this routin will work. But if you will ever build somehitng that is more then that, then you will have a problem.
Here is the code that i used for most of my applicaitons. you can see that the interupt is very short, all the rest im doing in the program itself.
Lets start:
1. You have to set the global veribales. I use globael because i run between few functions and i dont want my clock to get all worng...
2. You will find the interupt rutting. you can see that it is very short and simple. You will get in overflow flag from the timer, clear the flag and reset the timer.
3. You have to implament the settings for the timer overflow. As scdoro explaind, you write 0xFFFF - the time that you need.
For example. 10ms at 4Mhz:
4MHz / 4 = 1MHz = 0.000001 Sec
10ms = 0.01Sec.
10ms/1MHz = 0.01/0.000001 = 10,000 = 0x2710
FOR TMR1 = 0xFFFF - 0X2710 = 0xD8EF
4. Using a while loop, you enter each function at the correct time when the flag changes.
Code:
bit Flag_10mS=0, Flag_100mS=0, Flag_1Sec=0;	        // Timer Flags
unsigned char Count100mS=0, Count1Sec=0,Count1Min=0 ;// Time counters


/***************************************************
 *                    INTERRUPT SERVICE ROUTINE                      *
 ***************************************************/

void interrupt isr( void )
{

    if ( TMR1IF && TMR1IE )	// Timer1 overflow event occurred, set to 1mSec?
    {
        Flag_10mS = 1;
        TMR1L = 0xEF;		// Initialize Timer1 low byte
        TMR1H = 0xD8;		// initialize Timer1 high byte 1mSec
        TMR1IF=0;		// Clear TMR1 Interrupt Flag
        Count100mS++;	// counter for 100mS
         if ( Count100mS >= 10 )  // counter for 100mS
         {
              Flag_100mS = 1;
              Count100mS = 0 ;	// Reset counter
              Count1Sec++;
          }
          if ( Count1Sec >= 10 )	// Counter for 1 sec
         {
              Flag_1Sec = 1;
              Count1Sec = 0 ;	// Reset counter
         }
    } 
}
/******************************
 * Timer1 initialization code *
 ******************************/

void Init_Timer1( void )
{
    T1CON = 0x00;	// T1CON register setup
    TMR1L = 0xEF;	// Initialize Timer1 low byte
    TMR1H = 0xD8;	// initialize Timer1 high byte 10mSec
    TMR1IF = 0;	// Reset Timer1 Overflow H/W flag
    TMR1IE = 1;	// Enable Timer1 Overflow interrupt
    TMR1ON = 1;	// Start Timer1 counter
}

/***********************************************
 *                        INITIALIZE SYSTEM                          *
 ***********************************************/

void main(void)
{
Init_Timer1();	// Initialize Timer1
PEIE = 1;		// Enable Peripheral Interrupts
GIE = 1;		// Enable Global Interrupts

while(1)
{
// ************** 10mSec Function *****************
   if ( Flag_10mS )	// Functions to run at 10mSec
   {
       Flag_10mS = 0 ;
    ....
    }//End 10mSec

// ************** 100mSec Function *****************		
   if ( Flag_100mS )	// Functions to run at 10mSec
{
      Flag_100mS = 0;
      ....
}

// ************** 1Sec Function *****************
if ( Flag_1Sec )	// Functions to run at 1Sec
{
     Count1Min++;
     Flag_1Sec = 0 ;
     ...
}// End 1Sec

// ************** 1Min Function *****************
if ( Count1Min >= 60 )	// 1 Minute passed )
{
    Count1Min = 0 ;
   ...
}//End 1 Min
}//End while
}//End main

You will have to modifay the code a bit, but this is a code that works on PIC16F88

Good luck.
 

Re: how to calculate time

Hi,

I have this program (i don´t know where i get this, and i never tried it) but it calculates the delay, download and try.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top