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.

Poll timer_B capture/compare flag instead of using an interrupt

Other Parts Discussed in Thread: MSP430F2410

uP =  MSP430F2410

Task = Run timer b to up count to one second and then set the CCIFG flag.

Goal = Read the CCIFG flag, go do something, reset the up counter to zero, start all over again. I do not want to use and interrupt. I want to poll the interrupt flag ( I think)

Problem = The loop is running too fast and the IF statement is not running at one second. The output at P1.1 is always on (at least it looks like its on) How can I get the IF statement to trigger a fast pulse every one second and false.

Code:

#include  <msp430F2410.h>

     #define ONE_SECOND   (32768)   // ACLK counts in 1 sec
   #define ONE_SECOND_TIMER  TBCCR0   // RTC timer is TB6
   #define ONE_SECOND_TIMER_CONTROL   TBCCTL0
     signed int timeout;

  void main(void)
  {
     WDTCTL = WDTPW + WDTHOLD;          // Stop watchdog timer
     P1DIR |= 0x02;                                             // Set P1.1 to output direction
    
     TBCTL = TBSSEL_1 + MC_1 + TBCLR;   // ACLK = (watch crystal=32768), upmode,
                                                                              // clear TBR
    
   ONE_SECOND_TIMER = ONE_SECOND;    // Set TBCCR0 to 32768 so timer
                                                                                    // will rollover at 1 sec (ACLK / 32768)

   ONE_SECOND_TIMER_CONTROL = ( OUTMOD_4 );   // not sure if this is needed?

   timeout = 0x00;                         // trying different things with this variable


   for (;;)
   {
         if ( (TBCCTL0&CCIFG) != CCIFG )
       {
        P1OUT ^= 0x02;
       }
   }
   
      
  } // end main

 

  • Your code, as it is written, will toggle P1.1 as fast as possible until CCIFG gets set. Then it stops in the current state, wehther it may be on or off.

    To have it emit a pulse and then wait until a second has expired before emitting the nex tpulse, you'll have to do something like that:

    for(;;)
    {
      while (!(TBCCTL0&CCIFG)); // wait for the end of a second
      TBCCTL0 &= ^CCIFG;            // reset the interrupt flag
      P1OUT ^=BIT1;                        // toggle the port pin (begin of pulse)
      __delay_cycles(1234);          // duration of the pulse: 1234 CPU clock cycles
      P1OUT ^= BIT1;                      // toggle pin once more (end of pulse)
     } // done, rerun loop

    The duration of the pulse won't affect the time between teh beginnings of two pulses (unless you pick more than 1s delay)

**Attention** This is a public forum