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 looping logic with a toggle switch

Other Parts Discussed in Thread: MSP430F2012

Hello All,

I am having an issue with coding logic. The if statement declares when a toggle switch is high, the following code will execute. The while statement above delcares that it will run 1000 cycles. This works properly

The issue is after the 1000 cycles are completed, I can not push the button to restart the 1000 cycle count. I must repower the device, as the cycle count stays at 1000. I have tried various solutions by implementing my conditional statments in different orders, and by resetting the cycle count. How would I achieve the desired solution, or what logic do I need to review?  Thank you, for your help. 

#include <msp430f2012.h>
#include <msp430.h>
int cycles;

void main(void)                   
{
 
  WDTCTL = WDTPW + WDTHOLD;      //Stop Watch Dog Timer
  BCSCTL1 = CALBC1_1MHZ;         //Set range
  DCOCTL = CALDCO_1MHZ;          //SMCLK = DCO = 1MHz
  P1OUT = 0;                     //Initializes P1 to off state
  P1DIR = BIT1 + BIT2;           //Sets P1.1 + P1.2 to output direction
  P1DIR &= ~(BIT0|BIT3);         //Sets P1.0 + P1.3 to input direction

while(1)

{
  
  while(cycles<1000)             //1000 cycles
  {
   
  if(P1IN & BIT0)                //Initiate cycle start
  { 
  cycles++; 
 
  if(P1IN & BIT3)                //Configure Switch (Choose Program) 
  { 
    
  P1OUT |= BIT1;                 //Turns P1.1 on statically
  P1OUT |= BIT2;                 //Turns P1.2 on statically
  __delay_cycles(2500);          //2.5 ms hold time  
  P1SEL |= (BIT2);               //Delcaers TA1
  TACTL= MC_1 + TASSEL_2;        //Tells CCR0 to count up with SMCLK
  TACCTL1=OUTMOD_7;              //Sets Set/Reset Mode with TACCR1
  TACCR0=66-1;                   //Set CCR0 to 15 kHz (Hold Frequency)
  TACCR1=33;                     //Sets 50% duty cycle
  __delay_cycles(2500);          //2.5ms hold time   
  P1SEL &= ~(BIT2);              //Delcares A2
  P1OUT &= ~BIT1;                //Turns P1.1 off
  P1OUT &= ~BIT2;                //Turns P1.2 off
  __delay_cycles(500);           //500 microsecond clamp
  P1OUT |= BIT1;                 //Turns P1.1 on
  __delay_cycles(500);           //500 microsecond on time
  P1OUT &= ~BIT1;                //Turns P1.1 off
  __delay_cycles(4000);          //4ms delay
  }
 
  else
  {      
  P1OUT |= BIT1;                 //Turns P1.1 on statically
  P1OUT |= BIT2;                 //Turns P1.2 on statically
  __delay_cycles(5000);          //5 ms hold time
  P1OUT &= ~BIT1;                //Turns P1.1 off
  P1OUT &= ~BIT2;                //Turns P1.2 off
  __delay_cycles(500);           //500us off time
  P1OUT |= BIT1;                 //Turns P1.1 on
  __delay_cycles(500);           //500us on time
  P1OUT &= ~BIT1;                //Turns P1.1 off
  __delay_cycles(4000);          //4 ms off time
  }}}}}

**Attention** This is a public forum