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.

Pulse Train generation using the MSP430 ( code issues)



Hello,

I'm trying to execute a relatively simple code to generate a set of 14 pulses using the MPS430F5342 controller. The code i've used to generate these pulses is given below. 

#include <msp430.h>

 

unsigned int i = 0;

 

int main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                     //Disable Watchdog timer

  P1DIR = 0x02;                                                       //Port 1.1 to Output

  P1SEL = 0x02;                                                     //TA0.0 Special Function

  TA0CCR0 = 1000;                                              //Set CCR0

  TA0CCR1 = 500;                                               //Set CCR1

  TA0CCTL0 = OUTMOD_3;                              //Set/Reset Output Mode

  TA0CTL = TASSEL_1 + MC_1;                     //ACLK Clock Source, Up mode

  __bis_SR_register(GIE);                             //Interrupt Enable

  while(1);

}

 

#pragma vector=TIMER0_A1_VECTOR

__interrupt void TIMER0_A1_ISR(void)

{

  i++;

if (i>=14)                                                    //14 Pulse Check

{

  TA0CTL = 0x0000;                              //timer A disable

}

}

The issue is that the PWM is generated but it doesn't stop generating the pulse. Any help will be appreciated. Thanks.

  • You don't enable any timer interrupt, you only globally allow interrupts (GIE) but there are none.

    For what reason do you configure TA0CCR1? Is this intended to be your ISR trigger point? (halfway through the cycle)? If so, you need to set CCIE inside TA0CCTL1 to enable the interrupt. (note that CCIFG will set independently of CCIE, bu twon't have an effect if CCIE isn't set).

    In your ISR, you need to manually clear CCIFG, or else your ISR will be called over and over again right after it exits.

    I fopyu want a cycle of 1000 clock pulses, CCR0 needs to be set to 999, since from 999 to 0 there is another step (1000 total)

  • Hey Jens,

    Thank you for the reply. I had forgotten to enable the CCIE bit, and i have done so in my main code. However, i'm still unsure whether the logic i'm using is correct or not. Let me just briefly explain it.

    Im trying to get the MSP430 to generate a burst at 40 kHz  The length of the burst has to be 14 clock pulses. So what i had planned to do was set the clock to 16 MHz  and set a CCR1 value such that it divides that clock frequency, and in the interrupt toggles the output pin so that the pin generates a square pulse at the required frequency , i.e, 40 kHz. With the code above i had just tried to generate something similar to the above logic. However, if there is a more elegant way of doing it, I would be happy to alter the code.

    Thanks

  • Also, i edited my code as you suggested and it works. Just for reference, here is the edited version - 

    #include <msp430.h>

    unsigned int i = 0;

    int main(void)

    {

      WDTCTL = WDTPW + WDTHOLD;                     //Disable Watchdog timer

      P1DIR = 0x01;                                 //Port 1.1 to Output                                

      TA0CCR0 = 1000;                               //Set CCR0

      TA0CCR1 = 500;                                //Set CCR1

      TA0CCTL1 = CCIE;

      TA0CTL = TASSEL_1 + MC_1;                     //ACLK Clock Source, Up mode

      __bis_SR_register(GIE);                       //Interrupt Enable

      while(1);

    }

    #pragma vector=TIMER0_A1_VECTOR

    __interrupt void TIMER0_A1_ISR(void)

    {

      i++;

      P1OUT ^= 0x01;

      TA0CCTL1 &= ~CCIFG;

    if (i>=14)                                      //14 Pulse Check

    {

      TA0CTL = 0x0000;                  //Disable Timer

      P1OUT = 0x00;                        //Output set to low

    }

    }

**Attention** This is a public forum