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.

Help with MSP430 toggle a pin based on values in an array



Hi everyone, I have the following code where I am trying to set a pin to 1 or 0 depending on the value in an array.  What am I doing wrong and how do you suggest I change my code?  Thank you!!

 

#include  <msp430x16x.h>

#include "stdint.h"

#include "intrinsics.h"

volatile int i=0;

int transmit_reg;

void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

  BCSCTL1 |= XTS;                           // ACLK = LFXT1 = HF XTAL

  P1DIR |= 0x16;                            // P1.4 output

  TACCTL0 = CCIE;                           // CCR0 interrupt enabled

  TACCR0 = 976;

  TACTL = TASSEL_2 + MC_2;                  // SMCLK, contmode

  _BIS_SR(LPM0_bits + GIE);                 // Enter LPM0 w/ interrupt

  while(1)

  {}

}


// Timer A0 interrupt service routine

#pragma vector=TIMERA0_VECTOR

__interrupt void Timer_A (void)

{

  int transmit[10] =  {1,0,0,0,0,1,1,1,1,1};

  transmit_reg = transmit[i];

  if (transmit_reg == 1)

{P1OUT |= 0x16;                      // Set P1.4 High

  TACCR0 += 976;                      // Add Offset to CCR0

i++;}

  else

{P1OUT &= ~0x16;

TACCR0 += 976;

i++;}

  if(i > 9)

  {

  i=0;

  }

}

 

  • I am running my clock at 8Mhz but I want a value from the transmit array to be send @ a 8.2Khz rate.  That is why I used timer A to count up to 976.  Is there a better way to do this?

  • What behavior are you actually observing?

    On a separate topic, you have several instances whereby you mention targeting the P1.4 pin but your code actually affects more bits in the P1 registers than just bit 4.

    P1OUT |= 0x16;                      // Set P1.4 High

    This statement above affects bits 4, 2 and 1.  The logical-OR of 0x16 expands out to 00010110b, not a decimal 16 which would be 00010000b.

  • I'm not really getting anything significant from the oscilloscope.  I see some very short (in time duration) alternating spikes, but I don't believe thats what I am sending in my transmit register, because when I change my values, I see the same thing.  

     

    What I really need help with is:

    1) is the way I am setting up my transmit array and reading through the values correct?  

    2) also where am I supposed to declare my array/intermediate register/counter i?  Inside main, before main, or in my interrupt routine?

    3) do I need a while(1) where I have it or no?  

     

    Thanks for your help!!

  • Kevin Ting said:
    I am running my clock at 8Mhz

    I don't see this in your code. There is nothing that sets MCLK to 8MHz. It is running on default DCO frequency (~800kHz on the 1x family). If you have a crystal attached, you don't activate and use it in this code.

    Kevin Ting said:
    is the way I am setting up my transmit array and reading through the values correct?  

    Sort of.

    You define your array as local variable inside the ISR. This puts the array on stack each time the ISR is entered. Highly ineffective. But should work.
    Also, your reading of the array is ineffective too. A simple " if (transmit[i])" would do. However, most likely the compiler is optimizing this for you anyway. Same for the increment of I and CCR0 in both branches of the IF.

    However, it should work.
    What you should see is a repeated pattern of ~6ms 'high' and ~4ms 'low' on pins P1.1, P1.2 and P1.4 (see Brandons post).

    Kevin Ting said:
    also where am I supposed to declare my array/intermediate register/counter i?  Inside main, before main, or in my interrupt routine?

    The intermediate register is superfluous (yust access the array directly where you need it). The counter is good as global volatile variable. It could as well a static local variable, if you don't need to acess it outside the ISR.
    The array is best defined as a global const (unless you'll later need to change it, then as a global volatile array.)

    Kevin Ting said:
    do I need a while(1) where I have it or no?  

    It's good where it is. Since you enter LPM0 right before it and never leave LPM0, it is not really necessary, but it doesn't hurt. And later, you'll might need it. It only costs two bytes of code and is a good reminder for later.

**Attention** This is a public forum