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.

to make timer work in capture mode

Other Parts Discussed in Thread: MSP430G2231

i m trying to make the timer a work in capture mode on both rising and falling edge

i use the following code to implement it.....i am generating a wave at port p1.4 and connect it to pin p1.1 which is capture input to timer. i am glowing the red led at the rising edge to indicate start of the capture..then i switch off red and glow green led to incate falling edge then i am glowing red if the CCIFG flag is set...but oly red is glowing all the time

is this logic correct ???

please help me..iam using MSP430G2231 launchpad.i am new to launch pad. 

code:

 

#include <msp430g2231.h>

void main (void)

{

    WDTCTL = WDTPW + WDTHOLD;                                                                  // stop watchdog timer

    P1SEL = BIT1;                                                                                                       // take capture input at p1.1

    TACTL = TASSEL_2  +  MC_2;                                                                          // SMCLK , mode=2

TACTL = TACLR;

P1DIR  =  0x51;                                                                                                       //set p1.0,p1.4,p1.6 as output port

P1OUT  =  0x00;                                                                                                     // all off initially

TACCTL0  =  CM_3  +  SCS  +  CCIS_0  +  CAP  +  CCIE;                                    // setup the timer in capture mode

P1OUT  =  0x01;                                                                                                    //glow red led

volatile int j;

while( j < 50000)                                                                                                 // wait for some random time

{

j ++;

}   

    P1OUT = 0x10;                                                                                            //make p1.4 high 

    //if(BIT4)

    //{

   // if(j  ==  50000)

   // {

    P1OUT = 0x40;

  //  }    

    //}

    volatile int i=0;

    while(i<50000)                                                                                 //wait for random time                                                                                               

    {

    i++;

    }

    P1OUT  ^=  0x10;                                                                          //toggle p1.4

    _BIS_SR(GIE);                                                                               // enable interrupt

}    

#pragma vector=TIMERA0_VECTOR

__interrupt void TIMERA(void)

{   

TACCTL0&=~COV;                                                                     // clear overflow flag

    TACCTL0 &= ~CCIFG;                                                                 // clear CCIFG flag

  if(CCIFG)

  {

  P1OUT = 0x01;                                                          // if capture done glow red led 

  }

}

 

 

  • Hari,

    What is generating the wave for your capture? Is it external? You say you are inputting it into P1.4 but have P1.4 set as an output? You also say that you connect P1.1 and P1.4, so how are both LEDs not behaving the same? Please describe your hardware.

    Also, you are lacking a while(1) loop so your main() function ends right after you turn off the green LED.

  • i m generating signal at p1.4 and giving it to p1.1 . or else wat is the other way to give input signal ??? can i connect the vcc and gnd pin to p1.1 to generate the signal???

    hw will i knw the capture has happend?? how can u read the capture register ???  can i transfer the value directly to so variable say x?? if so hw cn i view the value of x in ccs??

     

  • Hari, 

    It sounds like what you may want is a port interrupt. If you have the Launchpad, the buttons use port interrupts to detect button press. You can configure port interrupts to be either rising or falling edge. Look at the Launchpad code to see how to configure Port pins for port interrupts here:

    http://e2e.ti.com/group/msp430launchpad/w/contents/1130.aspx

    For example, if you connect P1.0 and P1.2 and set P1.0 for interrupt on rising edge, and P1.2 for interrupt on falling edge, you could toggle your LEDs and increment some global variable to keep track of how many "captures" have happened.

    Your P1.0 interrupt would turn on red and turn off green to indicate rising edge, and then the P1.2 interrupt would turn on green and turn off red to indicate falling edge.

    If you do wish to use timers (you can) here is a post that discusses a similar implementation:

    http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/61141/219404.aspx#219404

    But you really NEED to add a while(1) loop somewhere in your main function. Right now your main function ends and you do not want that. Alternatively you could put the device to sleep in the last line of code and then rely on the interrupts to do your work for you. It could look like this:

     

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

    But if you want P1.4 to continue to be the source for P1.1 you need a while(1) around your P1.4 toggling to keep it going. Right now your code sets P1.4 high, then waits, sets it low, and then ends. Obviously you want this to cycle. Additionally you want to change your P1.4 clock to always be P1OUT ^= 0x10; When you write P1OUT = 0x10; you are setting P1.4 high, but also pulling P1.1 low

     

  • i am just generating a square wave on p1.4 and i am shorting p1.4 and p1.1

    MikeS said:
    But if you want P1.4 to continue to be the source for P1.1 you need a while(1) around your P1.4 toggling to keep it going. Right now your code sets P1.4 high, then waits, sets it low, and then ends. Obviously you want this to cycle

    if i give while (1) before p1.4 the control stays inside the loop only .. will the rest of the code be executed??

    i just want to indicated the rising edge with on led and when falling edge comes it should turn off one and should glow another led so as to make sure capture has happend..how to do it??? and hw to get time between the falling edge and rising edge??

    can u tell me the code for it???

    and how to give rising and falling edge input to p1.1?? please tell hw to configure timer to generate various signals?? 

     

  • Hari,

    I mean a while(1) loop around your code like this:

    while(1)

    {

    P1OUT ^= 0x10;

    delay here;

    P1OUT ^= 0x10;

    delay here;

    }

    This code gives a periodic "clock" because it continuously loops through that loop, changing the state of P1.4

    You need to look at the code examples. For Launchpad devices (the ones that come with the box) those examples are here:

    http://www.ti.com/litv/zip/slac463a

    In particular, you should look at the Port pin interrupt examples msp430g2xx1_P1_0X.c and the timer examples msp430g2xx1_ta_XX.c

    Also, to get a real clock output instead of what you are doing with pin toggling followed by software delays, you could use PWM on a pin. See code examples msp430g2xx1_ta_XX.c with the higher ta_XX values.

    And to stress this again, you need to either go to sleep or put a while(1){} at the end of your program to prevent it from finishing.

  • Hari S Pillai said:
    i am glowing the red led at the rising edge to indicate start of the capture

    Not only that. Yoe P1OUT=,,, statements affect ALL bits of P1OUT, so when doing P1OUT=0x01 for switching on the red LED, you set all other outputs on P1 to 0.
    Thsi immediately toggles another capture event for the falling edge this causes, and in main, togglign the output will always set it, causing another rising edge.

    Also, i don't see anything about a green LED.

    But wost of all:

    Hari S Pillai said:
    if(CCIFG)

    Sicne CCIFG is a constant with the value BIT1, this condition is equivalent to  "if(true)". Also, CCIFG is set for fallign and rising edge. To check whether it was a rising or falling, you'll need to check the current line state or the current (latched) value of the SCCI bit. If you don't check it, you'll only know that an edge of any kind happened, bu tnot which one.

    More:

    Hari S Pillai said:
    volatile int i=0;
        while(i<50000)                                                                                 //wait for random time
        { i++;}

    The comment is right: the wait time is random. Not random because it is different on each execution, but because it is totally unspecified because it is unknown what code the compiler will generate. Since the loop has no side-effects and no other effect than settign I to 50000 at the end, the compiler may even optimize the whoel loop away.
    Defining i volatile doesn't help. Since it is a local variable, the compiler knows that it is created on stack and nobody else has a reference on it, so the compiler will simply ignore the 'volatile'.
    use __delay_cycles(50000) instead. It is no real delay too, but at least then it will take exactly 50000 CPU cycles (the effective delay time still depends on the MCLK speed)

    But even then, your LEDs will flicker with 20Hz, whcih is almost at the edge of human recognition.

    One last thing: your main code does not run in a loop, so after the first manipulation of the P1.4 output, it simply exits into the void. Depending on the compiler, this may result in teh CPU going to sleep, reset, enternan endless while(1); loop or anything else. main() is not meant to return and it is not defined what will happen if it does. It depends on teh compiler you use.
    However, for your test, you'll anyway want to loop main through toggling P1.4 and waiting.

  • it worked...... thanx for the replies:)

     

  • Hey Hari,

    Could you please share your codes? Could you send it to my email at zhu_zhixin@hotmail.com  I really appreciate your help!!

     

    Thanks!

**Attention** This is a public forum