Hello,
I made a simple application running on a MSP430G2553 (launchpad) which reads the position of a potentiometer on port p1.4 and then generates a shorter or longer impulse on P1.6 depending on the position of the potentiometer. The impulse is generated when button P1.3 is pressed.
The sampling of P1.4 is triggered by Timer0output1; I am filtering 10 consecutive sampled values into filtratAD so I filter out the eventual "noise" given by an old potentiometer. The application works as expected if I use filtratAD as the impulse period. However if I try to call pow (filtratAD,0.7) and use the computation result as the variable impulse period, then I get wrong behaviour (random length impulses are generated).
Here is the relevant code:
#include <msp430.h>
#include <math.h>
volatile int sumaAD=0;
int indexAD=0;
volatile int filtratAD=0;
volatile int sqrroot=0;
volatile int temp=0;
#pragma vector=ADC10_VECTOR;
__interrupt void ADC10(void)
{
if (indexAD>9) //filter 10 values
{
filtratAD=sumaAD/10; //filtrat AD holds the filtered value of the last 10 inputs
indexAD=0;
sumaAD=0;
}else
{
indexAD++;
sumaAD=sumaAD+ADC10MEM;
}
ADC10CTL0&=~ENC; // wait for next sample trigger
ADC10CTL0|=ENC;
}
#pragma vector=TIMER1_A1_VECTOR; //stinge sudura *** ce se scurge perioada de timp necesara
__interrupt void TIMER1A3(void)
{
P1OUT &= ~BIT6; //falling edge generation on P1.6, end of pulse
TA1CTL &= ~(MC0|MC1); //turns off timer
TA1R=0; //resets timer
P1IFG=0; //clears all pending interrupts
P1IE|=BIT3; //reenables button interrupt (for new impulse generation)
int j=TA1IV; //acknowledge interrupt
}
#pragma vector=PORT1_VECTOR;
__interrupt void P13(void)
{
if (P1IFG&BIT3) //button pressed
{
sqrroot=(int)(pow(filtratAD,0.7)); //if i'm using pow on filtratAD, code is not working
//sqrroot=(int)(filtratAD); //if I am using filtratAD directly, code is working properly.
__delay_cycles(50000); //button
if ((P1IN&BIT3)==0) //debounce
{
P1IE&=~BIT3; //disable button interrupt until this impulse is generated
P1OUT |= BIT6; //impulse generation rising edge
TA1CCR0=100+sqrroot*2; //timer1 counts up to TA1CCR0 value then generates falling edge
TA1CTL|=MC0; //start timer 1
}
P1IFG = 0; //clear interrupt flag
}else
{
P1IFG = 0; //clear interrupt flag
}
}
Main function contains nothing but peripheral intialization code and blocks in an endless loop, all things of use are done in the above interrupt service routines.
What I find strange with the debugger is that the line "sqrroot=(int)(pow(filtratAD,0.7));" generates a correct result in sqrroot but also modifies the value of filtratAD. I do not know why this happens. Any advice greatly appreciated.
Best regards,
Florin