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 printf()

Hello,
I want to ask a little bit help.
I want to use the printf instruction with the Noice debugger.

I can printf char or float fine but when I try to printf int or long I get a totally different numbers throu the serial port.

eg. WORKS
void main (void)
{
unsigned char f;
for (f=0;f<=255;++f)
printf( %bu \n",f);
}

but DOESN'T WORK

void main (void)
{
unsigned int f;
for (f=0;f<=255;++f)
printf( %u \n",f);
}
Thanks for any advice
Cheer George

Parents
  • I haven't used that fuction very often yet, but when I was using it, I have noticed, that you should be very accurate with the types of the arguments. It is always helpful to cast the parameter. In that example that doesn't work, you might want to try to cast f.

    void main (void)
    {
    unsigned int f;
    for (f=0;f<=255;++f)
    printf( %u \n", (unsigned int)f );
    }
    

Reply
  • I haven't used that fuction very often yet, but when I was using it, I have noticed, that you should be very accurate with the types of the arguments. It is always helpful to cast the parameter. In that example that doesn't work, you might want to try to cast f.

    void main (void)
    {
    unsigned int f;
    for (f=0;f<=255;++f)
    printf( %u \n", (unsigned int)f );
    }
    

Children
  • This is an old chestnut!

    It is very important that the argument types must match precisely the types specified in the format string.

    Read the section Problems Using the printf Routines in the C51 manual, and see the table of format characters in the description of printf.
    In particular, note that %d and %u are for ints; for chars you must:
    either use the b prefix;
    or cast your char to an int

  • Hello,
    Thanks for the answers.
    Of course I read the c51 manual and I know type have to match with variable.
    I gave two examples in my post and I think both of them have a correct match.
    I tried to write (integer) in the printf too but still wrong.
    I forgett to tell when I try that short programs with Dscope both of them work fine so I have to belive the problem is in the Noice remore debugger.

    Cheer George

  • Don't forget the &ltpre&gt and &lt/pre&gt tags to make code posts legible:

    void main (void)
    {
       unsigned char f;
       for (f=0; f<=255; ++f)
          printf( %bu \n", f); // Works
    }
    void main (void)
    {
       unsigned int f;
       for (f=0; f<=255; ++f)
          printf( %u \n", f); // Doesn't Work
    }
    I presume that the missing opening quote in the format strings is just a typo in the post?

    Could you give more details of exactly what happens when it doesn't work?

    Are you using your own putchar?

  • Maybe there is a timing issue. Do these 2 statements work?

    printf( "%s \n", "x" );
    printf( "%s \n", "xyz" );

  • Hello,
    Yes the opening quote is in the original program, I made a mistype.

    What happens when doesn't work??
    It's interesting.
    let say variable is
    unsigned int c
    when c=3 it writes me to the screen
    eg. 832456
    I'm going to check again and I'll write the correct answer.

    I tried the following program
    printf (" %bu %bu\n",c,c)
    and I get the high and the low byte of the unsigned int c on the screen.

    I found a new bug in my system.
    When I want to use the unsigned int od long in a for statement it count only for 32767. If I want to count more the processor locks and I have to reset.
    eg.
    unsigned int c
    for (c=0; c<= 32767, ++c) it works but
    for (c=0; c<= 33000, ++c) it doesn't work and the program falls an unfinished cycle as I suppose.

    Cheer George



  • I forgett....
    No I use the c51 putchar throu the built in serial interface of the 8032.
    I use the Noice remote debugger and its
    terminal program.
    I tried some other terminal programs but the result was allways wrong.

  • I think yes, they'll work but I'm going to try soon.
    There is no problem to printf the char, unsigned char and the strings.
    Cheer George

  • unsigned int c
    for (c=0; c<= 32767, ++c) it works but
    for (c=0; c<= 33000, ++c) it doesn't work


    Do you have ANSI Integer Promotion disabled?

    What happens if you explicitly cast the constant to unsigned int; ie,

    for (c=0; c<= (unsigned int)33000, ++c)

  • Hello,

    for (c=0; c<= (unsigned int)33000, ++c)

    I haven't tried yet but I'll tomorrow.

    I put the "unsigned int" to the printf function but it didn't wok.

    I tried the following program
    unsigned int i;
    for (i=0; i<=10; ++i)
    printf("%u \n",i)
    and the result was
    04
    65536
    1310724
    1966084
    2621444
    3276804
    3932164
    5242884
    5898244
    6553604
    and so on

    it seems to me the last digit is usually 4 isn't it the lenght of the string that produces the printf?

    If I tried
    printf("%x \n",i);
    the result was
    04
    100004
    200004
    300004
    400004
    and so on.

    if I use the unsigned long instead of int the result is
    14
    24
    34
    44
    54
    64
    and so on.

    I don't know why but my system is unstable.
    the above programs works if the off-chip code start at 4003h
    but I changed to 4100h the or shomewhere 5000h it doesn't work or if works gives a different results.
    My off chip code memory is from 4000 to 7fff.
    May I ask you to contact me the keilc51@freemail.hu it would be easier for me to contact you.

    Thanks
    George



  • Considering that
    1. C51 passes parameters in fixed memory locations, and
    2. C51 stores multibyte values in big-endian order (high-order byte first)

    If

    unsigned int i;
    printf("%x \n",i);
    gives you
    100004
    200004
    300004
    400004
    and so on

    it sounds like your parameters are getting scrambled!
    The low nibble 1, 2, 3, 4, ... values of i seem to be appearing where the code expects to find the high nibble; because it's expecting an int, it then just reads on in memory, printing junk!

    Maybe you have a memory addressing problem?


  • Memory addressing problem??
    I don't know.
    I didn't mentioned but I allways modify the hex file before I download it.
    The reason is there are a strange line in the hex record.
    03000002xxxx
    it'means c51 put a jump instruction to start routin at location 0.
    Because my system has no writeable RAM at 0 it can't download the program.
    So I change that line
    03400002xxxxxxx
    which means load the jump instruction at the first read/witeable location.
    Everithing is in the correct address att the hex file and I do not change the others so I don't think that I made
    the memory addressing problem.
    What is your oppinion?

    I tried to work with Keil monitor51 but it also has some problem. I put ask to the list but I have no answer yet.
    Can you give me an advice where can I find good and working downloader or simple simulator program.

    Cheer George