Bug in return from procedure PSoC5

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AuKa_264411
Level 3
Level 3
25 replies posted 10 replies posted 5 replies posted

It seems that Cypress Tech Support is no more. Oh dear!

Here is my problem: I have small procedure that converts a binary number to an ASCII string, and returns the number of characters in that string. It includes leading zero blanking if desired (cFixedDigitCount=0) or a fixed length message with leading zeros. It looks like this.

uint8 cBinaryToAscii (uint16 iSource, uint8 sConversion[], uint8 cFixedDigitCount)

{//return the number of characters in the string

//cFixedDigitCount to allow for leading zero  

    uint8 cPoint,cI;

    uint16 iRemainder;

    cPoint=0;

    while (iSource!=0)

    {

        iRemainder=iSource%10;

        sConversion[cPoint]=(uint8)(iRemainder) | 0x30;

        cPoint++;

        iSource/=10;

    }

    if (cFixedDigitCount)

    {//only if non-zero, else leading zero blnking

        if (cPoint<(cFixedDigitCount))

        {

            for (cI=0;cI<(cFixedDigitCount-cPoint);cI++)

            {

                sConversion[cPoint]='0';

                cPoint++;

            }

        }

    }

    return cPoint;

}

Up till now it seems to have worked. The program has grown a lot since I tested it out, but I have not seen problems.

I have just used it in a new module as follows

cJ=cBinaryToAscii(iRetrievedParameter,cASCIIResult,0);

cJ is a local uint8, cASCIIResult is a 5 uint8 array

When I step through the cBinaryToASCII, or run through it with a breakpoint, the number appearing for cPoint is correct, 4 in this specific case. But in the calling module cJ has a value of 0x1A. If I change cJ from a local variable to a global one, it works correctly.

Any idea what I am doing wrong, or is there a bug in the compiler?

Compiler details

PSoC Creator 4.2 (4.2.0.641)

0 Likes
1 Solution

OK. SO the problem is with the optimization of the code, even though my optimization level is set to Debug. The line that followed "cJ=..."

was a for statement using cI as a counter and cJ as the starting address, but I got them inverted. Seems that this error produced code that did not assign the correct value to cJ.

View solution in original post

0 Likes
4 Replies
AuKa_264411
Level 3
Level 3
25 replies posted 10 replies posted 5 replies posted

It seems that the problem is not in the return, but with the variable named "cJ". I use cJ as an index everywhere as a local variable, and it doesn't appear to be creating/initialising it correctly.

0 Likes

OK. SO the problem is with the optimization of the code, even though my optimization level is set to Debug. The line that followed "cJ=..."

was a for statement using cI as a counter and cJ as the starting address, but I got them inverted. Seems that this error produced code that did not assign the correct value to cJ.

0 Likes

Try setting optimization level to "none"

​Bob

0 Likes

Bob

Thanks. I seem to remember reading that "debug" was at the same level as "none". I will try it, although since the problem is now cured, I am not sure how to prove it.

0 Likes