This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem passing variable to function in ISR

So, I have a problem sending a value to a function at different parts of my code. Here is my code:

void LongRightShift(unsigned char NumBitsShift)
{
// My Code Here
     NumBitsShift = NumBitsShift; // So you can place a breakpoint here and read NumBitsShift
}

Now, when I call

LongRightShift(2);

in the main part of my program, NumBitsShift = 2. When I call it in the Decimator2_ISR, NumBitsShift = 1;

Does this have to do with the way I'm passing it or is there a conflict with running other functions in an ISR?

Any help you can give me would be appreciated. Thanks!

Parents
  • void AverageOutput(void)
    {
            unsigned char tempvar = 0;
            unsigned char NumOutSamples = 0;
            unsigned char Pressure1 = 500;
    
            if(Pressure1<600)
            {
                    NumOutSamples = 0;
                    AveragedPressureOut=Pressure1;
            }
            else
            {
                    //More code here
            }
            tempvar+=1; // Place breakpoint on this line
    }
    

    If that's your actual code, and you're not terribly worried about all the compiler warnings you get about it, then you you have the warning level set way too tolerant. For starters, this line

    unsigned char Pressure1 = 500;
    

    must have triggered a warning about an out-of-range initializer --- 500 is too big for an unsigned char.

    As-is, for sufficiently high optimization level this entire function could be optimized into a one-liner

    void AverageOutput(void)
    {
         AveragedPressureOut=244;
    }
    

Reply
  • void AverageOutput(void)
    {
            unsigned char tempvar = 0;
            unsigned char NumOutSamples = 0;
            unsigned char Pressure1 = 500;
    
            if(Pressure1<600)
            {
                    NumOutSamples = 0;
                    AveragedPressureOut=Pressure1;
            }
            else
            {
                    //More code here
            }
            tempvar+=1; // Place breakpoint on this line
    }
    

    If that's your actual code, and you're not terribly worried about all the compiler warnings you get about it, then you you have the warning level set way too tolerant. For starters, this line

    unsigned char Pressure1 = 500;
    

    must have triggered a warning about an out-of-range initializer --- 500 is too big for an unsigned char.

    As-is, for sufficiently high optimization level this entire function could be optimized into a one-liner

    void AverageOutput(void)
    {
         AveragedPressureOut=244;
    }
    

Children
  • My apologies to everyone here for the confusion. I didn't post my entire code because it is rather lengthy and tried to post a fairly straight forward example of what I'm seeing but apparently, I made a few mistakes when posting an "example" that obscured the issue.

    Pressure1 is actually an unsigned SHORT (it was the end of the day and I looked right past this. It's originally in my VARS.C file so I had moved it over here and, apparently, posted it incorrectly).

    However, all your posts did help me understand how the compiler works and how it removes code that goes nowhere and, sure enough, I did have that realization that I did something very dumb. The reason tempvar was in there is I was testing my function with multiple pressures and created an if statement to give those values that looked like this:

            if(ProcessedSensorNumber==1)
            {
    
                    unsigned char i = 0;
    
                    if (tempvar==0)
                    {
                            Pressure1 = 500;
                    }
                    else if (tempvar==1)
                    {
                            Pressure1 = 550;
                    }
                    else if (tempvar==2)
                    {
                            Pressure1 = 600;
                    }
                    else if (tempvar==3)
                    {
                            Pressure1 = 1000;
                    }
                    else if (tempvar==4)
                    {
                            Pressure1 = 1002;
                    }
                    else if (tempvar==5)
                    {
                            Pressure1 = 1004;
                    }
                    else if (tempvar==6)
                    {
                            Pressure1 = 1202;
                    }
                    else if (tempvar==7)
                    {
                            Pressure1 = 1800;
                    }
            }
    

    This is why I needed that

    tempvar+=1;
    

    line. The thing that caused all the problems I was seeing is that I placed the variable declaration IN THIS FUNCTION, rather than in my VARS.C file, which means it gets reinitialized everytime it enters the function and makes the increment pointless (hence why it was not simulating correctly).

    The function is now giving me problems with this logic statement:

    if(((Pressure1-AveragedPressureOut)>410)||((Pressure1-AveragedPressureOut)<-410))
    {
         // some code here
    }
    else
    {
         // more code here
    }
    

    when AveragedPressureOut = 500 (0x01F4) and Pressure1 = 550 (0x0226) (both are signed shorts)

    Now from what I understand, 550-500 = 50 so, since it is between -410 and 410, it should go into the else statement code but it appears to be going into the if statement code. I don't really understand why.

    I apologize for all the confusion earlier but thanks for the help! I understand what's going on with the compiler a lot better, which is always a good thing!

  • I'm afraid there's still some clarity that needs to be increased:

    Pressure1 is actually an unsigned SHORT (it was the end of the day and I looked right past this. It's originally in my VARS.C file so I had moved it over here and, apparently, posted it incorrectly).
    [...]
    when AveragedPressureOut = 500 (0x01F4) and Pressure1 = 550 (0x0226) (both are signed shorts)

    Not only did you contradict yourself there, but that bit of information you appear confused about is actually crucial to the issue at hand. The problem is that if even one of those variables actually is unsigned, in C51, then so is the resulting '50' you're comparing with the signed number -410. At that point you would enter the realm of comparisons between values of different signedness. You don't want to go there.

  • Nevermind, apparently, I had one of them declared as unsigned again. Is it really only Monday . . . heh.

  • Yeah, you caught it right before I posted here. I should've taken the extra minute to check on these things before posting them as truths. Sorry about that!

  • Well, software development is an iterative process :D