0
\$\begingroup\$

I am trying to use the timers on the microcontroller in order to count up to 10us then add 1 to the int timer. I intend to use this in a project I am doing where I count the time between pulses from a circuit I have built. The code is as flows:

#include <msp430fr4133.h>
#include <driverlib.h>

unsigned int count = 0;
unsigned int button1 = 0;
unsigned int button2 = 0;

#pragma vector=TIMER0_A0_VECTOR
__interrupt void TimerA0_ISR0(void)
{
  count++;
}

void main(void)
{
  WDTCTL = WDTPW | WDTHOLD;               // Stop watchdog timer

    // Disable the GPIO power-on default high-impedance mode
    // to activate previously configured port settings
    PMM_unlockLPM5();  
    //Setting up buttons
    GPIO_selectInterruptEdge(GPIO_PORT_P1, GPIO_PIN3,GPIO_LOW_TO_HIGH_TRANSITION);
    GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN3); 
    GPIO_clearInterrupt(GPIO_PORT_P1, GPIO_PIN3);
    GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN3);

    GPIO_selectInterruptEdge(GPIO_PORT_P1, GPIO_PIN4, GPIO_LOW_TO_HIGH_TRANSITION);
    GPIO_setAsInputPinWithPullUpResistor(GPIO_PORT_P1, GPIO_PIN4);
    GPIO_clearInterrupt(GPIO_PORT_P1, GPIO_PIN4);
    GPIO_enableInterrupt(GPIO_PORT_P1, GPIO_PIN4);

    //Setting up timer to count to 10us 
    TA0CTL = TACLR;
    TA0CTL = TASSEL_2|ID_0|MC_1|TACLR;   //SMCLK, divider 1, up mode, clear timer, interrupt enable, interrupt pending
    TA0CCTL0 = CM_1|CCIS_2|OUTMOD_2|CCIFG;
    TA0CCR0 = 10000;      //count to 10us = 10/1MHz/1

    while(1)
    { 
      button1 = GPIO_getInputPinValue(GPIO_PORT_P1, GPIO_PIN3);
     if(button1==0)
      {
        TA0CCTL0 &= ~ CCIE;              //disable interrupt if button 1 pressed
        timer = 0;    
      }
      button2 = GPIO_getInputPinValue(GPIO_PORT_P1, GPIO_PIN4);
      if(button2==0)
      {
        TA0CCTL0 |= CCIE;                //enable interrupt if button 2 pressed
      }
    }
}

The way the code should work is when the second button is pressed the interrupt will enable and the counting will begin. Then when the first button is pressed the interrupt will disable and reset the timer.

The problem I am having is that when the interrupt is enabled, it will never enter the interrupt and increase int timer. I have set up the timer using the user guide and datasheet but I am unsure if I have set it up correctly. If anyone could shed some light on this problem it would be much appreciated!

UPDATE

The code has been changed slightly to fix suggested problems but still does not work.

\$\endgroup\$
  • \$\begingroup\$ ~COV does not do what you think it does. \$\endgroup\$ – CL. Apr 8 '16 at 18:40
1
\$\begingroup\$

CL is right, your use of ~COV is wrong and is ruining your other settings. Here is what happens:

COV is defined in memory as:

 #define COV (1 << 1)

Inverting COV (with ~COV) does not just invert that bit, it actually inverts the entire COV field. So if you know that COV is (in binary) 0000 0000 0000 0010, then ~COV becomes 1111 1111 1111 1101. You then take this value and OR it with everything else you just carefully set up, effectively wiping out those settings.

Your TA1CCTL1 register is thus ALWAYS set to 1111 1111 1111 1101, or just ~COV.

If you want to clear COV bit, you need to do it properly:

TA1CCTL1 &= ~COV; 

If you also want to clear bits A and B (for example), you could do:

TA1CCTL &= ~(COV | A | B);
\$\endgroup\$
  • \$\begingroup\$ Thank you, I understood this to mean that it would set COV to bit 0. I have made changes to this and went back through the datasheet are reconfigured my timer, however my code still isn't working and I have no clue as to why. I have updated the code above with the changes I've made. If you could help with why this is it would be much appreciated. \$\endgroup\$ – Aidan Howie Apr 11 '16 at 11:18
0
\$\begingroup\$

The interrupts were not correctly enabled!

The way this was fixed is by using __enable_interrupt();. This function fixed the interrupts and allowed them to work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.