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 with sprintf

I have to following code:

//      Write screen
        n = sprintf( screen_msg,      " TV: %#4.1f  FV: %#4.1f  CV: %#4.1f  SV: %#4.1f\r\n", ad_value[2], ad_value[1], ad_value[0], ad_value[3] );
        n += sprintf( screen_msg + n, " TI: %#4.1f  AT: %#4.1f  CT: %#4.1f  ST: %#4.1f\r\n", ad_value[4], ad_value[5], ad_value[7], ad_value[6] );
        n += sprintf( screen_msg + n, "\r\n" );
        n += sprintf( screen_msg + n, "ENGINE      CHARGE     CLK EN      DIS" );

        write_0( screen_msg );

When the string is printed to my LCD CV, SV, and ST are always zero, regardless of the value in the ad_value array. If I print just those values in the same routine they appear as they should. When I debug the program the entire string appears as it should. My screen_msg buffer is 256 characters and the entire string in only 170.

I have looked over the code a dozen times and can't figure out what's going on. Can anyone see something that I may be missing?

Thanks,
Paul

Parents
  • Does this print valid data?

    strcpy(screen_msg,"hello world");
    write_0(screen_msg);
    

    Your use of sprintf() should work unless the C51 architecture have specific problems.

    But, why bother with sprintf and many, many values when all floats have a common formatting and are stored in an array?

    p = screen_msg;
    for (i = 0; i < 8; i++) {
        n = sprintf(p,"%s %4.1f ",prompt[i],ad_val[i]);
        if (n < 0) return FAIL;
        p += n;
    }
    strcpy(p,"\r\nMy final message here");
    wrrite_0(screen_buffer);
    

    And, as already mentioned, do consider fixed-point arithemetic for your poor chip.

Reply
  • Does this print valid data?

    strcpy(screen_msg,"hello world");
    write_0(screen_msg);
    

    Your use of sprintf() should work unless the C51 architecture have specific problems.

    But, why bother with sprintf and many, many values when all floats have a common formatting and are stored in an array?

    p = screen_msg;
    for (i = 0; i < 8; i++) {
        n = sprintf(p,"%s %4.1f ",prompt[i],ad_val[i]);
        if (n < 0) return FAIL;
        p += n;
    }
    strcpy(p,"\r\nMy final message here");
    wrrite_0(screen_buffer);
    

    And, as already mentioned, do consider fixed-point arithemetic for your poor chip.

Children
No data