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.

Caclulating duration of push-button press.

Other Parts Discussed in Thread: MSP430G2553

Iam using MSP430G2553. 

The following is the code i prepared for calculating the duration of a push button press and displaying it's binary equivalent by 4 LEDs (time between 0s-15s)
The count() seems to work..though, the glow() is not getting executed it seems...
Could anyone please help me with what is going wrong here??

#include "msp430g2553.h"
int i=0;
void count()
{
while(P1IN&0x08)
{TAR=0;
}

while(!(P1IN&0x08))
{P1OUT^=0x01;
i=(TAR/1000000);
}
}
void glow()
{ //Loop forever, we work with interrupts!

switch(i)
{ case 0 : P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
break;
case 1 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 2 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT2;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 3 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT1 + BIT2;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 4 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT5;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 5 : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT5 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 6 : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT5 + BIT2;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 7 : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT5 + BIT2 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);

}
break;
case 8 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 9 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 10 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT2;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 11 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT2 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 12 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT5;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 13 : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT5 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 14 : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT4 + BIT5 + BIT2;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
case 15 :{ P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT|= BIT4 + BIT5 + BIT2 + BIT1;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
}
break;
default : { P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
P1OUT |= BIT1 + BIT2 + BIT5 + BIT4;
_delay_cycles(5000000);
i=0;
P1OUT&=~(BIT1 + BIT2 + BIT5 + BIT4);
break;

}
}
}

void main()
{WDTCTL=WDTPW + WDTHOLD;
BCSCTL1=CALBC1_1MHZ;
DCOCTL=CALDCO_1MHZ;
TA1CTL=TASSEL_2 + ID_0 + MC_2;
P1DIR|=0x01;
P1DIR|=BIT1 + BIT2 + BIT4 + BIT5;


P1REN|=BIT3;
while(1)
{count();
glow();

}
}

  • The first thing that jumps off the page:
    > i=(TAR/1000000);
    TAR is only 16 bits, so this result is always 0. You'll probably need to use a software counter to reach 15 seconds.

    More generally, there's no real debouncing for the pushbutton here. The 5-second delays provide a form of debouncing I suppose, but one that requires considerable patience. The 5-second quantum also means you'll never see, e.g. a 1-second timing result.

**Attention** This is a public forum