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.

msp430 launchpad program

Other Parts Discussed in Thread: MSP430G2553

Hello,

I am making my program using msp430g2553 launch pad. In this i have taken input from P2.4 and i want my output from P2.0, P2.1 and P2.2.

In this program i have to generate the pulse of 500ms from input. and my output will be three low pulses from the three pins. Meaning  whenever i connect my oscilloscope probe to 2.4 i can see the pulse. But the program i made it didn't work. when i connect to the oscilloscope it is showing nothing.

when my input pulse is high i will get the three low pulses output.

here is my code, please check it and tell me what is wrong in the code? since it doesn't contains any error.

code:

#include <msp430g2553.h>

#define CPU_F ((double)16000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))

char C_1, C_2;

void System_Clock_Init( void )
{
    char i;
    _NOP();
    WDTCTL = WDTPW + WDTHOLD;                                                            // Close Watching dog
    delay_ms(50);                                                                                               / / 1MHz
    if( ( CALBC1_16MHZ == 0xff ) || ( CALDCO_16MHZ == 0xff ) )              // FLASH A was cleared!!!
        ((void (*)())RESET_VECTOR)();
    BCSCTL1 = CALBC1_16MHZ;                                                                      //ACLK=32768Hz
    DCOCTL = CALDCO_16MHZ;  //MCLK=DCOCLK=16MHz
    BCSCTL2 = 0x06;//SMCLK=DCOCLK/8=2MHz
    BCSCTL3 = 0x00;                                                                                            / /cap=1pF
    IE1 &= ~OFIE;                                                                                                   / /close crystal error interrupt
    
    do
    {
        IFG1 &= ~OFIFG;     
        for( i = 0; i < 100; i++ )
        {
            _NOP();
        }
    }
    while (IFG1 & OFIFG);   
    
    _NOP();
}

void IO_INIT(void)
{
    NOP();
    P2SEL &= ~BIT4;
    P2SEL2 &= ~BIT4;
    P2REN &= ~BIT4;
    /*P2DIR |= BIT2;*/
    P2IES |= BIT4;
    P2IE  |= BIT4;
    P2IFG  &= ~BIT4;

    P2SEL &= ~(BIT0+BIT1);
    P2SEL2 &= ~(BIT0+BIT1);
    P2REN &= ~(BIT0+BIT1);
    //P2DIR &= ~(BIT0+BIT1);
    P2OUT &= ~(BIT0+BIT1);

    P2SEL &= ~BIT2;
    P2SEL2 &= ~BIT2;
    P2REN &= ~BIT2;
    //P3DIR &= ~BIT1;
    P2OUT &= ~BIT2;
    
   
}

void main(void)
{
    System_Clock_Init();
    _NOP();
    //WDTCTL = WDT_ARST_1000;
    _NOP();      
    IO_INIT();
    _NOP();
    _EINT();//open all interrupt
    _NOP();
    LPM3;
    _NOP();
    while(1);
}

//------------------------------------------------------------------------------
//Function: Start Timer 0 A0 for Hour Timing 500ms
//------------------------------------------------------------------------------
#pragma vector = PORT2_VECTOR
__interrupt void PORT_C_Interrupt(void)

{
 
  _NOP();
  delay_us(20);

  if( C_1 == 0xff)
  {
    if(!( P2IN & BIT4 )) C_2 = 0xff;
  }
  else
  {
      if(!( P2IN & BIT4 )) C_1 = 0xff;
  }
  if(( C_1 == 0xff) && ( C_2 == 0xff))
  {
    //output A B D
    P2OUT |= BIT0;
    delay_ms(2);
    P2OUT |= BIT1;
    P2OUT &= ~BIT0;
    delay_ms(2);
    P2OUT |= BIT2;
    P2OUT &= ~BIT1;
    delay_ms(2);
    P2OUT &= ~BIT2;   
    delay_ms(2);
    
    P2OUT ^= BIT1;                 //P2 toggle
    P2IFG &= ~BIT1;                //IFG cleared 
    C_1 =0x00;
    C_2 =0x00;
  }
  _NOP();
}


  • Hello Singh,

    You commented out your P2DIR register settings for the output pins, therefore pins P2.0-P2.2 are initialized as inputs and will not output any voltage when the respective P2OUT bits are set. You also clear the wrong IFG inside of the ISR, trapping the code to continually loop through the ISR without exiting back to LPM3.

    Regards,
    Ryan
  • Hello,

    Thanks for the reply.

    If i didn't commented P2DIR register, I am getting only one pulse as a output only in the pin 2.2 and nothing else.

    I have to get the output from all the pins.

    And can you explain more about the wrong IFG that i have cleared.

    Thanks

  • The following lines are necessary for a BIT4 input and BIT0-BIT2 outputs on port 2:

    P2DIR &= ~BIT4; P2DIR |= BIT0 | BIT1 | BIT2;

    You can then modify the P2OUT register to change the output pins BIT0-BIT2 to high or low. And you use P2IFG &= ~BIT1; instead of P2IFG &= ~BIT4; which as I understand it is the input pin that should be cleared. If I am incorrect then you will need to further describe your intended application. You should also try further debugging your program to get a more firm understanding of the issue.

    Regards,
    Ryan
  • Hello Ryan,

    Thanks for replying.

    I have modified my code and also added what you have said and getting the output.

    Now i want to reduce the size of the pulses i.e. to reduce width of the pulse. How this can I achieve? by using duty cycle or by using timer?

    Thanks

  • As your solution currently stands you will have to manipulate the timer ISR with cycle delays, a more proper solution would be to use the TA1/2 CCR1/2 pins to output a PWM. Code examples are provided: www.ti.com/.../slac485

    Regards,
    Ryan

**Attention** This is a public forum