I am trying to use TACLK (external timer frequency). Up to now I am succesfull to obtain frequency
up to about 10kHz. Howver, when I try to obtain the waves with more than 20kHz frequency,
the reading of number of waves starts to fail (becomes smaller numbers e.g., 18328).
Following is my code. Could someone suggest some way to improve for the higher frequency
if that exists?
------------------------------------------------
#define CCR_MSEC 1049 // for every 1 msec count
#include <msp430x471x7.h> // F471x7
#define LED_OFF P5OUT &= ~0x02; // F471x7 D1 LED (P5.1)
#define LED_ON P5OUT |= 0x02;
#define LED_TOGGLE P5OUT ^= 0x02;
unsigned long list[11];
int g_idx = 0;
unsigned long g_msecCount, g_event, g_secCount;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
FLL_CTL0 |= XCAP14PF; // Configure load caps
P1SEL |= 0x20; // P1.5 (TACLK)
// External Freq. input
TACCTL0 = CCIE; // for TIMERA0_VECTOR
TACCR0 = 1; //STB
TACTL = TASSEL_0 + MC_1; // TACLK (external freq.), Up mode
// Internal Real Time Clock
TBCCTL0 = CCIE; // for TIMERA0_VECTOR
TBCCTL1 = CCIE;
TBCCR0 = 65536 - 1;
TBCCR1 = CCR_MSEC - 1; // 1048.5
TBCTL = TBSSEL_2 + MC_2 + TBIE;
// ACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz
g_secCount = 0;
g_msecCount = 0;
g_event = 0;
__bis_SR_register(LPM0_bits + GIE);
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
g_event++;
}
#pragma vector=TIMERB1_VECTOR
__interrupt void Timer_B1(void)
{
switch( __even_in_range(TBIV, 10))
{
case 2: // CCR1 1msec
TBCCR1 += CCR_MSEC - 1;
TBCCR1 %= (65535 - 1);
g_msecCount++;
break;
case 10: // overflow
break;
}
}
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B0(void)
{
static long sqNo = 1;
static long preMsec;
long diffMsec;
if(sqNo == 17) {
diffMsec = g_msecCount - preMsec;
g_secCount++; // every 1 sec
LED_TOGGLE;
sqNo = 1;
preMsec = g_msecCount;
list[g_idx++] = g_event * 2;
g_event = 0;
if(g_idx > 10) {
_NOP();
return;
}
}
sqNo++;
}