Search code examples
timermsp430receiverinfrared

MSP430 G2553 TimerA Compare Mode for IR receiver


My Infrared-Receiver sends me digital data on Port P1.1. I already have my Timer configured that if theres a falling or rising edge a interrupt gets triggered. I wanna know how i get the actual time difference between the edges. Afterwards i wanna save them in an array.

Main:

// Stop watchdog timer
WDTCTL = WDTPW + WDTHOLD;

//1mhz = 0.000001
BCSCTL1 = CALBC1_1MHZ;               // load calibrated data
DCOCTL = CALDCO_1MHZ;

//Define Outputs
P1DIR = green_led+red_led+IR_Send;
P2DIR = LED1+LED2+LED3;

//Define Inputs
P1DIR &= ~IR_Recv;

//Set IR_Recv as input for Timer (TA0.CCI0A)
P1SEL |= BIT1;

//Timer_A using SMCLK/8 = 0.000008s and Continuous mode
TACTL = TASSEL_2 | MC_2 | ID_3;

// falling edge and rising edge capture mode, CCI0A, enable IE
CCTL0 = CM_3 | CCIS_0 | CAP | CCIE;

//Enter LPM and enable Global Interrupts
__bis_SR_register(CPUOFF + GIE);

Interrupt Routine:

//gets called when falling or rising edge is detected on IR_Recv
#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
if(edgeCount < 10){
    rxData[edgeCount] = TACCR0;
    TACCR0 = 0;
}
edgeCount++;

P2OUT ^= LED2;
//Clear interrupt Flag
TACCTL0 &= ~CCIFG;
//go back to LPM
__bic_SR_register_on_exit ( CPUOFF );
}

Solution

  • To get the time difference, in ticks, between two captured events, just let the timer keep running without resetting it, and subtract the captured time stamps:

    uint16_t difference = rxData[1] - rxData[0];
    

    This computation is correct even when the timer counter happens to overflow.