This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

timer problem to count external clock

Other Parts Discussed in Thread: MSP430F4270

i am useing msp430f4270 controller. i want to calculate external clock so i give this clock as input to 74hc14 smit trigger ic and give its output to p1.5 pin as input. but my timer is not counting pulse correctly................ in input of 74hc14 pulses comes good but in out put pulse is not as good as input to this ic so is there is any problem with pin setting?????? i atteched pin setting snapsot and schemetic diagram here ...i cant attech cro snapshot........

if i give 70,000 pulse its only count 24062..................

here i corrected error regarding ground  appear in schematic and now i can debug so problem is of timer........

so sorry its hardware issue my ground is not common now its resolved.......

now timer counts good but now problem is of accuracy..........

if i give it 1000 pulsew with freq=250  its count 1003 and some times 1005

here is code

-----------------------------code--------------------------------

#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer(void)
{

cal_flag=1;
if(count==0)
pulse_flag=1;


}
void calculation (void)
{
if(cal_flag==1)
{
pval=cval;
cval=TAR;
cval=TAR;
if(cval != pval)
{
if(cval<pval)
{
pval=0xFFFF-pval;
dif=cval+pval;
}
else
dif=cval-pval;
total2=total2+dif;
n1=1;
}
}

}

void conversion(void)
{
if(n1==1)
{

total=total2%1000000;
n2=1;
n1=0;
}

}

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

while(1)
{


calculation();
conversion();
output();

}
}
void output(void)
{
if(n2==1)
{
n2=0;
cal_flag=0;
}
}

any suggestion ????????????????

  • The schematics looks good so far (in is in and out is out). Output of the 74HC14 should be a clear rectangle. Now it is an inverter and your input pullup means the initial logic level on the timer pin is low. Depending on how the pulse generator counts, this might make up for +-1 pulse.

    karan patel1 said:
    pval=cval;
    cval=TAR;
    cval=TAR;

    Reading TAR twice doesn't do any good. If you want to avoid the problem of reading TAR whiel it might be clocked by an unsynchronized clock, you'll have to do this:

    while (cval !=TAR) cval = TAR;

    This ensures a proper reading if MCLK is >= 10*TAclk.
    If the input clock speed is near MCLK, then use a capture unit to 'snapshot' the timer count in the CCR register by a forced capture. Since the capture snapshot happens synchronous to the timer clock, this works, but is of course more effort than the proposed while loop.

    karan patel1 said:

    if(cval != pval)
    {
      if(cval<pval)
      {
         pval=0xFFFF-pval;
        dif=cval+pval;
      }
      else
        dif=cval-pval;
      total2=total2+dif;
      n1=1;
    }
    }

    I'm not sure this calculation is correct. I think you'll have a +1 error on overflow. However, if pval and cval are simple unsigned ints, you can skip the whole effort.

    "diff = cval-pval;" always gives the correct difference, even if there was an oveflow, as long as the difference is <65536. "total", of course, needs to be a long int (unsigned is good, as you don't have negative counts).

  • My MSP430F5xx based (zero-parts) frequency meter is published (firmware and part of the source code) here

    http://forum.43oh.com/topic/3317-msp430f550x-based-frequency-meter

  • thank you very much for reply....

    i gone through your blog but i find its give integer frequency.....

    but my requirement is accuracy of 3 digit after decimal point and my max freq is 40Hz  so how can i achieve this accuracy????

    here i have 2 timer

    a-basic timer(whit out facility of external clock)in basic timer there is to part(btcnt1+btcnt2).btcnt1 got aclk of 32768hz and t2 got aclk/256.

    b-timer A3 having capablity of external clock + compare facility

    so here i give unknown clock to timer A3 and generate compare intrupt at every 10pulse and in this isr i read basic timer value and from this value i try to find frequency... but its not works......

  • If you want to capture 1 Hz, measuring cycle must be 1 second. In the last post on link, you can see that my frequency meter can measure in 0.5 Hz resolution with measuring cycle of 2 seconds. If you want to measure 0.001 Hz, this is possible for sure (by using timer divider in my code), but your measuring cycle will take 1000 seconds (almost 17 minutes).

  • yes you are right. but its not practical that frequency update at every 17 min......... is there another way.?

  • karan patel1 said:
    but my requirement is accuracy of 3 digit after decimal point and my max freq is 40Hz  so how can i achieve this accuracy????

    Frequency counting is done by comparing two frequencies, one of them known.

    You count the pulses of the higher frequency during one pulse of the lower frequency.

    This means, you either count external pulses during a known time interval, or you count pulses of a known internal frequency during one external pulse width.

    In your case:

    set the timer up for counting with a known frequency. YOu need to know your minimum  input frequency (or things start to be complecated).
    If minimum frequency is 1Hz, the timer must not count faster than 65535Hz. Because 1s *65535Hz means 65535 counts, which is the maximum size of the timer count register.
    Let's say fT = 10000Hz  for simple math.
    Then you use a capture unit to capture the current timer count on each rising edge of the input frequency. Now on first incoming pulse, you capture a value of, say 100. On the next you capture 10100. The difference x is 10000, which gives f = fT/x = 1.0000 Hz (you see, four decimals)
    If the next capture then is 10600, the difference is 500 which gives f = fT/x = 10000Hz/500 = 2Hz. On 40Hz, you'd only get a count of 25, which only gives a resolution of 1/25 Hz.

    Well, it's up to you to define the border parameters and get the ideal values for timer frequency etc.

    Note, that of course the update frequency is equal to the incoming pulse frequency.

  • k igot it my frequency max limit is 40hz....

    i want to ask one thing i am using basic timer as 16 bit counter to produce 1 sec delay so if at any instant i want to read its value i should read it as (BTCNTT1+(BTCNT2*256)) na??????????????????????????????

  • karan patel1 said:
    k igot it my frequency max limit is 40hz....

    So if you're using a 40kHz clock for the timer, you can distinguish between 40,000 and 39,999Hz when counting ht enumber of tiemr pulses between two signal clock edges. However, the minimum frequency you can properly measure is 0.6Hz (assuming you somehow really get 40.0kHz timer clock, which isn't that simple)
    But those 0.6Hz could be measured with a resolution of 0.000015Hz :)

    karan patel1 said:
    i want to ask one thing i am using basic timer as 16 bit counter to produce 1 sec delay

    why that? If you count the incoming pulses during 1 second, you'll have for a frequency of n Hz an error of 1/n Hz. Because you don' tknow by how much you missed ht enext pulse. If you count 25, you don't know whether it is 25.000 or 25.999Hz. You only know that it is below 30.000

    However, to answer your quesiton: It depends :)

    BTCNT1 and BTCNT2 are two independent counters. BTCNT1 always counts with ACLK. BTCNT2 coutns wiht either ACLK, SMCLK or ACLK/256.
    If oyu pick ACLK/256 for BTCNT2, it still doesn't mean that it is the 'high byte' of a 16 bit count. This is only true if you hold both, set both to 0 and then start them. Else the count of BTCNT2 won't necessarily happen when BTCNT1 overflows.
    See the schematics on the users guide, Fig.13-1.

**Attention** This is a public forum