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.

how to count external events using cc2530/cc2431 Timer1?

Other Parts Discussed in Thread: CC2431, SIMPLICITI, CC2530

Hi,

 

I want to count external events/clock frequency using Timer1 ,

 

can somebody guide me through as I am stuck in this mode which simply shows count of free running counter  on receiving external interrupts where could i possibly go wrong?

my code is:--

#include<cc2431.h>

#include<string.h>

#include<math.h>

#include<stdio.h>

char OM[12];

 int count=255;

int count1=0;

void delay()

{

 int i;

 for(i=0;i<30000;i++)

 {}

}

void sd()

{

    int i;

     for(i=0;i<10;i++)

    {}

}

void ssd()

{

    int i;

     for(i=0;i<10000;i++)

    {}

}

void event()

{

    int a,i;

     T1CTL=0x01;    

  T1CCTL2=0x79;

    T3CTL=0x1C; 

  //delay();

   ssd();

 

   //T1IF=0;

   if(TIMIF==0x41)

    {

 

     count--;

 

    }

if((T1IF==1))

{

     count1++;  

      //U0DBUF='@';

        a=sprintf(OM,"%d",count1);

    while(!(T1CTL=0x80));

 

    while(!(P1IF=1));

 

       T1IF=0;

       T1IE=0;

          if(count==0)

     {

     count=255;

     T3IF=0;

 

/*  for(i=0;i<=a;i++)

    {

    U0DBUF=OM[i];

     delay();

     }*/

    U0DBUF=T1CC2L;

    delay();

 

    U0DBUF=T1CC2H;

    delay();

 

    U0DBUF=0x0D;

    delay();

 

    U0DBUF=0x0A;

    delay();

 

     T1CC2L=0;

     T1CC2H=0;

     T1IF=1;

     T1IE=1;

     T3IF=1;

     }

     }

 

 

        sd();

    }

 

  //U0UCR=0x82;

 

void main()

{

    //delay();

    U0GCR=0x08;

    U0BAUD=59;

    U0CSR=0xC0;

 

    P0SEL |=(1<<3)|(1<<2);

    P0DIR =(P0DIR&0x06)|0x04;

    PERCFG=0x40;

    P1SEL |=0x01;

    //P1DIR=0xFE;

    P1IEN=0x01;

    PICTL=0x00;

 

    UTX0IF=0;

    IEN2|=0x0D;

    SLEEP &=~0x04;              //SLEEP & CLCKCON is oscillator select

    CLKCON =0x00;

    IEN1=0x2A;

    //IEN2=0x20;

    TIMIF=0x40;

    while(1)

    {

      event();

      count1=0;

    }

 

}

  • Hi,

    honestly I didn't really get, what your code means and what you are up to, just my two cents:

       1.: did you assure that the compiler didn't optimize your empty  loops away, I read something on this in the SimpliciTI code.

       2.: compared to your code an Interrupt Service Routine (ISR) reads like a nobrainer - believe me 8-)

    Good luck!

  • hi buddy

    I am trying to do something which cc2431/cc2530 is not meant for i.e. counting events from an external source by getting time  difference from successive counts at each rising edge

  • well,

    1) did you check that the compiler did NOT remove empty loops like for(...) {}

    2) try to implement busy waiting - if really necessary - with asm("NOP");

    3) why don't you use a timer ISR to handle the counter - thats exactly what ISRs are good at - if you want a really defined delay

    4) if you need a detection of a rising edge, why introduce unclear timing by dumb busy waiting via for(..) {}, the result will be compiler configuration dependent at least

  •  

     

    Hi,

    1)Compiler is not removing empty for(---) loops

    2)This is a deliberate waiting 

    3)Those dumb waitings are mainly for UART as serial data is usually not comprehensible without delay ,Timer3 is used for 1 sec delay by making it overflow 255 times

    4)I am getting counts at every rising edge of external events as in Timer 1 input capture ,but  they are free running in nature
    5)My question was regarding getting time or counts at every external input capture event