tags:

views:

751

answers:

4

I'm developing pressure measuring device. I've used MSP430F133 chip and using IAR embedded workbench. It shows pressure in 3 diff units.

I'm taking 32 samples and averaging it. Unit selection on P5, according to the unit selected output value is calculated and displayed on LCD.

now a unit "IN WC" is showing binary averaged vale of input, just for analysis.

the problem. in default unit(MM WC) values are displaying correctly

but in a test situation when pressure is released it goes down and LCD read as below

+31.8
+31.7
+31.6
+31.5
+31.4
+31.3
+31.2
+31.2
+31.1
+31.5 (wrong reading randomly between *.4 to *.7)
+30.9

As you can there is one wrong value is being displayed, I'm not able to figure out the reason.

source file link http://rapidshare.com/files/202907123/main1.c.html

Also here: http://pastebin.com/m15eaeb79 (To avoid the rapidshare download hassle)

+1  A: 

This looks fishy, the || probably should be &&:

        for (i=0; i<= 3||res[i]!='\0'; i++)

But I don't see how it causes your problem.

Also, you should clean up and simplify your code. As it is it is very hard to read.

starblue
+4  A: 

In the below code ptiveValue = value and d1 = value so d2 is always 0 then in your loop you have for (i=0; i<= 3||res[i]!='\0'; i++) which should be for (i=0; i<= 3&&res[i]!='\0'; i++) so it always prints out what was left in the buffer not what you want

Bad code:

if (cntd <= 4)
{
    d2 = (unsigned int) abs((ptiveValue - d1) * 10000); // get 4 digits of real part
    itoa1(d2, res, &cntreal);  
    for (i=0; i<= 3||res[i]!='\0'; i++)
    {
       wr_lcd_dr(res[i]);

    }
 }

Fixed code

if (cntd <= 4)
{   
    // get 4 digits of real part
    d2 = (unsigned int) ((ptiveValue - (unsigned int)(d1)) * 10000); 
    itoa1(d2, res, &cntreal);  
    for (i=0; (i<= 3) && (res[i]!='\0'); i++)
    {
       wr_lcd_dr(res[i]);     
    }
 }

You are also overwriting your buffer and possibly creating weird behavior.

unsigned short Adcinb[32];
for (i = 0; i <= 63; i++)
Adcinb[i] = 3180;

Should be

unsigned short Adcinb[32];
for (i = 0; i < 32; i++)
Adcinb[i] = 3180;
Rex Logan
He is overwriting other memory, which may or may not give this result, but it only happens once at the beginning of the program. Something that certainly needs to be fixed, but probably not the source of the problem...
Adam Davis
Since the MSP430 does not have a stack or heap for memory who know variable he is clobbering. But I agree probably not source of problem.
Rex Logan
Some variants of MSP430 do have buffer/stack.
Sharique
I know but not the typical stack that most think of when working with a stack and it shares the memory space so corrupting the the stack or other memory variables is easy to do with a buffer overrun.
Rex Logan
i tried with the fixed code by Rex Logan.but same problem...i checked the analog input to ADC with scope that doesnot shows any spikes or anything
A: 

Looking over your code I don't see any particular reason you would be getting that value unless it reflected the actual values being sensed.

Can you run the program and output each of the 32 values before averaging for the number that has the problem, the number before it, and the number after it?

Alternately, write out the new sample each time you get a sample and give us that data.

Adam Davis
+2  A: 

Unfortunately none of the two links to the source code works anymore. But from what I can see the cause might be the fact that the expected 'correct' last digit at this place is a zero. My guess is theat somewhere in the calculation or visualisation code this zero is erroneously taken as a stop condition and causes a random digit to be shown at its place. (only a '31' is provided to the output but 3 digits are sent to the display)

The '||'/'&&' issue above shows that the code isn't very straight forward and if this is true for the rest too, a wrong stop condition here and a fixed-length loop there might cause this.

Just a 'wild guess'(TM) but the best I can give without knowning the actual code.