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 in capture mode

Other Parts Discussed in Thread: MSP430FR5729

i have msp430fr5729 controller and want to calculate frequency of input pulse. i have implemented following code and able to calculate frequency  8 hz and onward but when i decreace frequency cant calculate frequency i think its problem regarding overflow. but the way i handled it in my code i think its ok so can help me its urgent .

here is code.

-------------------------------------------------------------------------------------------------------------------------------------------

#include <msp430.h>
volatile unsigned char no_of_bytes;
double total1,total2,mastertotal1,mastertotal2;
float flow1,flow2;
unsigned long long int count1,count2,mcount1,mcount2;
unsigned int ppuvalue,previouscount3,previouscount4,currentcount3,currentcount4,k;
unsigned long int temp;
unsigned int crc,crc1;
volatile unsigned int previouscount1,previouscount2,currentcount1,currentcount2,temp1,temp2;
unsigned int reset,*dataptr;
volatile unsigned int displayindicator,updatedataflag,txflag,firsttime,firstetime;
volatile unsigned char rxbuf[88],id[80],*rxptr,transfer,txbuf[85],overhade,overflowflag;
unsigned char *dataptr1,finish;
void tx(void);
void calculate_crc(volatile unsigned char *array_ptr);
void kputchar(char TXchar);
void checkcrc(void)
{
rxptr--;
//p1--;
crc1=*rxptr;
crc1=crc1<<8;
rxptr--;
crc1=crc1+*rxptr;

calculate_crc(&rxbuf[0]);

}
void calculate_crc(volatile unsigned char *array_ptr)
{
unsigned int B3RX2;
unsigned char B0R0,B2R0;
crc = 0xffff;
for ( B2R0 = 0 ; B2R0 < no_of_bytes ; B2R0++ )
{
crc ^= ( *array_ptr );
array_ptr ++;
for ( B0R0 = 0 ; B0R0 < 8 ; B0R0++ )
{
B3RX2 = crc;
crc >>= 1;
crc &= 0x7fff;
B3RX2 &= 0x0001;
if ( B3RX2 )
crc ^= 0xa001;
}
}
//return(crc);
}
void errorfn(void)
{
unsigned char k;
txbuf[0]=0x02;
txbuf[1]=0x80;
txbuf[2]=0x01;
txbuf[3]=0x70;
for(k=0;k<=3;k++)
kputchar(txbuf[k]);
}
void kputchar(char TXchar)
{
// if(TXchar!=0x03)
//{
while(!(UCA1IFG & UCTXIFG));

/* wait for TX register to be empty*/
UCA1TXBUF = TXchar; /* send the character */
//}
/* else
{
if(skip==1||skip==6)
skip==3;
else if(skip==2)
skip=4;

}*/
}
void clockconfigure(void)
{
CSCTL0_H = 0xA5;
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting 8Mz
CSCTL2 = SELA_0 + SELS_3 + SELM_3; // set ACLK = XT1; MCLK = DCO
CSCTL3 = DIVA_0 + DIVS_2 + DIVM_2; // set all dividers 8/4=2Mz for SMCLK and MCLK,ACLK UNDIVIDED
CSCTL4 &= ~0x00C0;
for(k=0;k<=50000;k++);
for(k=0;k<=40000;k++);
CSCTL4 &= ~XT1OFF;

do
{
CSCTL5 &= ~XT1OFFG;
// Clear XT1 fault flag
SFRIFG1 &= ~OFIFG;
}while (SFRIFG1&OFIFG); // Test oscillator fault flag */
}

void initialization(void)
{
// -------------------- port pin initialization start-----------------------
P1SEL0=0x10;
P1SEL1=0x00;
P1DIR =0xFF;

P2SEL0=0x18;
P2SEL1=0x63;
P2DIR =0x84;

P3SEL0=0x50;
P3SEL1=0x50;
P3DIR =0xAF;

PJSEL0=0x30;
PJSEL1=0x00;
//---------------------- port pin initialization end--------------------------

// -------------------- timer A0(flow1) initialization start-----------------------
TA0CTL |=TASSEL_2+ID_2+MC_2+TAIE; //SMCLK,DIVIDEBY 2,CONTINIOUS MODE,INTERRUPT ENABLE
TA0CCTL0 |=CM_1+CCIS_1+SCS+CAP+CCIE; //CAPTURE ON RISING EDGE,SYNC,CAPTURE MODE,INTERRUPT ENABLE
//---------------------- timer A0(flow1) initialization end--------------------------

// -------------------- timer A1(flow2) initialization start-----------------------
//TA1CTL |=TASSEL_2+ID_1+MC_2+TAIE; //SMCLK,DIVIDEBY 2,CONTINIOUS MODE,INTERRUPT ENABLE
//TA1CCTL0 |=CM_1+CCIS_1+SCS+CAP+CCIE; //CAPTURE ON RISING EDGE,SYNC,CAPTURE MODE,INTERRUPT ENABLE
//---------------------- timer A0(flow1) initialization end--------------------------

// -------------------- timer B1(total2) initialization start-----------------------
TB1CTL |=TBSSEL_2+ID_1; //CONTINUOUS MODE,EXTERNAL CLOCK
//---------------------- timer B1(total2) initialization end--------------------------

// -------------------- timer B2(total1) initialization start-----------------------
TB2CTL |=MC_2+TBIE; //CONTINUOUS MODE,EXTERNAL CLOCK
//---------------------- timer B2(total1) initialization end--------------------------

//-----------------------------initialize uart for 9600 baudrate-------------------------------------------------
UCA1CTL1 |= UCSWRST;
UCA1CTL1 = UCSSEL_1; // Set ACLK = 32768 as UCBRCLK
UCA1BR0 = 3; // 9600 baud
UCA1BR1 = 0;
UCA1MCTLW |= 0x5300; // 32768/9600 - INT(32768/9600)=0.41
// UCBRSx value = 0x53 (See UG)
UCA1CTL1 &= ~UCSWRST;
//--------------------------------uart initialization end------------------------------------------------------
UCA0CTL1 |= UCSWRST;
UCA0CTL1 = UCSSEL_1; // Set ACLK = 32768 as UCBRCLK
UCA0BR0 = 3; // 9600 baud
UCA0BR1 = 0;
UCA0MCTLW |= 0x5300; // 32768/9600 - INT(32768/9600)=0.41
// UCBRSx value = 0x53 (See UG)
UCA0CTL1 &= ~UCSWRST;
__bis_SR_register(GIE); // enable global interrupt


}

void calculateflow(void)
{
// calculate flow1 and flow2
unsigned long int difference;

if(!overhade)
difference=currentcount1-previouscount1;
else
{
id[5]=overhade;
overhade--;

difference=65535-previouscount1;
difference=difference + currentcount1;
temp=overhade*65535;
difference=(difference+temp);
}
//temp=difference*ppuvalue;
flow1=500000/difference;

/*if(previouscount2 <= currentcount2)
difference=currentcount2-previouscount2;
else
{
difference=65535-previouscount2;
difference=difference + currentcount2;
}
temp=difference*ppuvalue;
flow2=65535/temp;*/
}

void calculatetotal(void)
{
// calculate total1 and total2
unsigned int difference;

previouscount3=currentcount3;
currentcount3=TB1R;
if(previouscount3 <= currentcount3)
difference=currentcount3-previouscount3;
else
{
difference=65535-previouscount3;
difference=difference + currentcount3;
}
count1=count1+difference;
total1=count1/ppuvalue;
mcount1=mcount1+difference;
mastertotal1=mcount1/ppuvalue;

/*previouscount4=currentcount4;
currentcount4=TB2R;
if(previouscount4 <= currentcount4)
difference=currentcount4-previouscount4;
else
{
difference=65535-previouscount4;
difference=difference + currentcount4;
}
count2=count2+difference;
total2=count2/ppuvalue;
mcount2=mcount2+difference;
mastertotal2=mcount2/ppuvalue;*/


}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
initialization();
clockconfigure();
// P2OUT |=BIT2;
id[0]=0x01;
id[1]=0x02;
id[2]=0x03;
id[3]=0x04;
id[4]=0x05;
transfer=0;
ppuvalue=100;
//dataptr=(unsigned char*)&total1;
while(1)
{
UCA1IE |= UCRXIE;
P1OUT &=~BIT5;
//TA0CTL |=TAIE;
TA0CCTL0 |=(CCIE);

finish=0;
while(!transfer || !finish)
{
if(updatedataflag==1)
{
calculateflow();
dataptr1=(unsigned char*)&flow1;
id[1]=*dataptr1+1;
id[2] =*(dataptr1);//currentcount1&0x00FF;
id[3]=*(dataptr1+3);
id[4] =*(dataptr1+2);

TA1R=0;
updatedataflag=0;
firstetime=0;
finish=1;
//TA0CTL &=~TAIE;
//TA0CCTL0 |=CCIE;
}
if((TA0CCR0 & COV))
TA0CCR0 &=~COV;

}

TB1CTL &=~MC_2;
UCA1IE &= ~UCRXIE;
P1OUT |=BIT5;
if((rxbuf[2]==0) && (rxbuf[4]==0) && (rxbuf[3]<80) && ((rxbuf[3]+rxbuf[5])<80))
{
// no_of_bytes1=no_of_bytes;
// kputchar(no_of_bytes);
//for(k=0;k<250;k++);
for(k=0;k<50;k++);
no_of_bytes=no_of_bytes-2;
checkcrc();
if(crc==crc1)
{
if(rxbuf[1]==0x03)
{
tx();
}
for(k=0;k<250;k++);
for(k=0;k<250;k++);
transfer=0;
firsttime=0;
rxptr=&rxbuf[0];
for(k=0;k<=87;k++)
rxbuf[k]=0;
no_of_bytes=0;


}
else
{
errorfn();
for(k=0;k<250;k++);
for(k=0;k<250;k++);
no_of_bytes=0;
transfer=0;
firsttime=0;
rxptr=&rxbuf[0];
for(k=0;k<=87;k++)
rxbuf[k]=0;
no_of_bytes=0;

}
}
/*if(txflag)
{
UCA1TXBUF =(currentcount1&0xFF00)>>8;;
for(k=0;k<=10;k++);
UCA1TXBUF =currentcount1&0x00FF;
for(k=0;k<=10;k++);
txflag=0;
}*/
//TA0CCTL0 |=CCIE;
}
}

// over flow intrupt isr for timerA0
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
switch(__even_in_range(TA0IV,14))
{
case 14: displayindicator++; // overflow
if(displayindicator>=16)
{

displayindicator=0;
}
overflowflag++;
break;
default: break;
}
}

// capture interrupt isr for timerA0
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
if(!firstetime)
{
previouscount1=TA0CCR0;
firstetime=1;
overflowflag=0;
}
else
{
currentcount1=TA0CCR0;
updatedataflag=1;
overhade=overflowflag;
TA0CCTL0 &=~(CCIE);

}
TA0CCTL0 &=~CCIFG;
}

// capture interrupt isr for timerA1
/*#pragma vector = TIMER1_A0_VECTOR
__interrupt void Timer_A1 (void)
{
if(!(TA1CCR0 & COV))
{
previouscount2=currentcount2;
currentcount2=TA1CCR0;
}
else
{
previouscount2=TA1CCR0;
TA1CCR0 &=~COV;
}
}*/

#pragma vector = USCI_A1_VECTOR // USCI ISR
__interrupt void USCI_A1_ISR(void)
{

if(!firsttime)
{
if(UCA1RXBUF_L==0x02)
{
TB1CTL |=MC_2+TBIE;
firsttime=1;
TB1CCTL1 = CCIE; // TBCCR0 interrupt enabled
TB1CCR1 = 6000;
rxptr=&rxbuf[0];
no_of_bytes=0;
}
}
*rxptr=UCA1RXBUF;
no_of_bytes+=1;
if(no_of_bytes<=87)
rxptr++;
TB1R=0;
UCA1IFG &=~UCRXIFG;
}

// Timer B0 interrupt service routine
#pragma vector = TIMER1_B1_VECTOR
__interrupt void Timer_B (void)
{
TB1CTL &=~TBIFG;
switch(__even_in_range(TB1IV,14))
{

case 2:
transfer=1;
TB1CTL |=MC_0;
TB1R=0;
break; // CCR1 not used
// case 4:

// break; // CCR2 not used
default: break;
}
TB1CTL &=~TBIE;
}
void tx(void)
{
unsigned char k=0,data,c,startadd;
for(k=0;k<2;k++)
txbuf[k]=rxbuf[k];
data=rxbuf[5]*2;
txbuf[2]=data;
//rxbuf[5]=data & 0x0F;
k=3;
startadd=rxbuf[3];

for(c=startadd;c<(startadd+data);c++)
{
txbuf[k]=id[c];
k++;
}
no_of_bytes=data+3;
calculate_crc(&txbuf[0]);
txbuf[k]=crc&0x00FF;
k++;
txbuf[k]=((crc&0xFF00)>>8);
//k++;
data=data+5;
for(k=0;k<data;k++)
kputchar(txbuf[k]);
}

-----------------------------------------------------------------------------------------------------------------------------------------

  • Could you please edit your post and use the code editor (the bottom-right button on the toolbar) to produce indented. Readable code?
    Sorry, but it is a real pain to look for an algorithmic problem in 12 pages of unindented, unformatted, foreign code.

**Attention** This is a public forum