Hi all,
I'm having some troubles getting the timer interrupt to work in the way I want it to and I was hoping someone here would be able to give me some advice on where I'm going wrong.
I have a pulse input with varying duty cycle on pin 1.1 what I want my code to do is light up the LED on pin 1.0 when the duty cycle is above 50% and turn it off when it is below 50%.
I think the trouble I am having is understanding when the interrupt is 'triggered' and how I can get it to trigger on the rising and falling edge.
please see the code I am using below.
#include <msp430.h>
unsigned int Ton=0;
unsigned int Toff=0;
unsigned int TTotal =0;
unsigned int first_pulse=0;
int main(void)
{
volatile unsigned int i;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
for (i=0; i<20000; i++) // Delay for crystal stabilization
{
}
P1DIR = 0x01; // Set P1.0 out,1.1 input dir
P1OUT &= ~0x01; // LED off
P1SEL = 0x02; // Set P1.1 to TA0
//P2DIR = 0x01; // P2.0-ACLK
//P2SEL |= 0x01;
BCSCTL1 |= DIVA_3; // ACLK/8
CCTL0 = CM_1 + SCS + CCIS_0 + CAP + CCIE; // Rising edge + CCI0A (P1.1)
// + Capture Mode + Interrupt
TACTL = TASSEL_2 + MC_2; // SMCLK + Continuous Mode
_BIS_SR(LPM0_bits + GIE); // LPM0 + Enable global ints
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA0(void)
{
if (first_pulse==0) //detect first falling edge
{
Ton=TACCR0; //take current value of Timer Register and store it in integer 'Ton'
first_pulse=1; //set the first pulse flag to 1.
// goto here;
}
TTotal = TACCR0; // on next interrupt (rising edge) capture the timer register value and store it in 'TTotal'
Toff = TTotal - Ton; // calculate the amount of time off by subtracting Time on from Total Time.
if(Ton >= Toff){ //if the amount of time on is greater than the amount of time off (duty cycle = >50%) then set p1.0 (light up red led)
P1OUT |= 0x01;
first_pulse=0; //reset first pulse flag
}
else
{
P1OUT &= ~0x01;
}
//here:
//P1OUT^=LED1;
}
Any advice would be appreciated.