Answered: mlab / mcc variable usage problems.

I am using mlab and mcc-18-2.40 compiler.

I find that I am unable to set initial variable values. I have declared my variables outside the scope of any C function and tried both declaring them as “static” and non-static. I have even tried declaring them local to the function.

I have the compiler variables options set to auto. The current state of the code has the variables declared as static at global scope and seems to allow me to change the values but I dont seem to be able to set the intial value. I have tried setting the initial value both at the time of declaration and in a function.

I have also noticed through attempting to debug this with printf_lib that printf on many calls becomes confused and seems to print variables in the wrong order. I am using the standard Vex Starter Code with modifications that I have made to it.

I am thinking that I have a compiler switch set stupidly or something similar.

Thanks,
-Tom-

We can not help much from what you have given us. Please post your code or email it to us at [email protected]

Hi Ricky,

Thanks for the response I upload the entire MLAB IDE project to the suggested email address.

I really believe I have done something very simple and foolish in this project. I created the project by trimming down the default code and I suspect I have something missing.

Been looking at the memory organization documents for the 18f8520, and the MLAB MCC compiler users guide.

I think the next step would be to take the default project add my variables only and compare mapfiles and assembly language generation between the two. But if you can eye this and see the problem it would save me hours.

-Tom-

Ricky,

If you look down at the comment with your name on it you will get a description of the problem. Basically if I attempt to update the s_elapsed
or s_loop variables the initial value do not seem to be zero when loop starts

Thanks,
-Tom-


void User_Autonomous_Code(void)
{
/* Initialize all PWMs and Relays when entering Autonomous mode, or else it
will be stuck with the last values mapped from the joysticks. Remember,
even when Disabled it is reading inputs from the Operator Interface.
*/
pwm01 = pwm02 = pwm03 = pwm04 = 127;
pwm05 = pwm06 = pwm07 = pwm08 = 127;
pwm09 = pwm10 = pwm11 = pwm12 = 127;

s_elapsed = 0UL;
s_loop = 0UL;
s_elapsed_start = 0UL;

#ifdef ZI_MAIN_LOOP_LOGGING
printf(“entering autonomous mode loop(%04X): %d elapsed(%04X): %d\r”, (char*)&s_loop, s_loop, (char*)&s_elapsed, s_elapsed);
#endif

while (autonomous_mode)   /* DO NOT CHANGE! */
{
    if (statusflag.NEW_SPI_DATA)      /* 18.5ms loop area */
    {
        Getdata(&rxdata);   /* DO NOT DELETE, or you will be stuck here forever! */

#ifdef ZI_MAIN_LOOP_LOGGING
printf(“completed getdata loop(%06X): %d elapsed(%06X): %d\r”, (char*)&s_loop, s_loop, (char*)&s_elapsed, s_elapsed);
#endif

#ifndef ZI_HANDLE_ENCODER_TICKS
HandleEncoderTicks();
#endif

#ifdef ZI_MAIN_LOOP_LOGGING
printf(“prior to putdata loop(%06X): %d elapsed(%06X): %d\r”, (char*)&s_loop, s_loop, (char*)&s_elapsed, s_elapsed);
#endif

        Putdata(&txdata);   /* DO NOT DELETE, or you will get no PWM outputs! */

#ifdef ZI_MAIN_LOOP_LOGGING
printf(“completed putdata loop(%06X): %d elapsed(%06X): %d\r”,(char*)&s_loop, s_loop, (char*)&s_elapsed, s_elapsed);
#endif

        PrintWord(s_loop);
        PrintWord(s_elapsed);
        
        // Ricky: If I include these line in the code the initial values of
        // of s_elapsed and s_loop do not seem to be zero from the 
        // logging. But if I comment these lines out compile and run the
        // values do seem to be zero from the logging output.   
        s_elapsed = s_elapsed + 18UL;
        s_loop++;
    }
}

}

**The project was missing the main.c file. Also, you should have only one library file in the project.

Please email the main.c file so we can try to compile.**

Thanks.

I will send you the main.c when I get home tonight because I dont have it with me now.

I want to mention that last night I cleaned up some of the library and .asm files from the project that werent needed. I determined this by looking at the StarterCode project.

After I cleaned up the project I noticed that both in the mapfile and during execution the problem variables moved from below 0x200 to over 0x700 hex memory location. But sadly this didnt correct the problem with them not getting initialized.

I will follow up tonight, and thanks again.

I believe I understand what is happening now. The problem is actually not the variable initialization. The printf statements do not actually write to the terminal until after the autonmous loop has been running for a certain amount of time.

You will see that when the s_elapsed and s_loop variables are finally logged you will always get s_loop * 18 ms = s_elapsed.

How come the printfs dont work when the microcontroller is first initialized?

**We emailed you code and it is now initializing. The problem is in the code that was removed. If you what to know the exact problem, slowly go back and add sections of code that we removed until you find your exact problem.

There is a limit to how much you can print. Keep your print(f) to a minimum or you will overrun the buffer and then you can not trust the data you are seeing. **