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

I am using puthcar( ) function to send a series of bytes serially thru UART. My problem is that i cannot see the output through serial window of simulator . its printing just some junk values. when i am doing the same with printf(). I am getting response which could be observed from serial window.Can any one tell me why this is happening.

Nitin

Parents Reply Children
  • Its also mentioned that printf() used puthcar to print . then why is it that putchar is not working while printf() works. is it possible to get source code of printf that might sort out thing.

    Nitin

  • "the same codes i sent to printf() and its working perfectly."

    But printf cannot print individual characters directly - it must have a string.
    I you pass a string to putchar, you will certainly get rubbish!

    Why don't you show us how you're calling the two functions? (not forgetting those 'pre' and 'pre' tags!)

    "is it possible to get source code of printf that might sort out thing."

    I don't think Keil publish their source, but you can step through it in the debugger to see what's going on.

  • They do publish the source of "putchar.c"/

  • send_buf is an unsigned char array


    the code goes like this
    printf("[%x]",(int)send_buf[i]) ;
    This code works and it prints hex codes in serial window

    putchar(send_buf[i])
    prints junk values only in serial window


    Whats the reason and how can it be corrected

    Thank you
    Nitin

  • The two calls you describe do completely different things - so the fact that the first gives readable output and the second doesn't is absolutely and totally irrelevant!

    Both calls are working correctly - it is your expectation that is faulty!

    The first one takes the numerical value of send_buf[i], formats it into printable text representing its hexadecimal value, then prints the text (using a number of putchar calls); eg, if the value is 0xA5, it will print "A5"

    The second simply takes send_buf[i] and sends it straight to the UART - so if send_buf[i] is not a printable ASCII code (eg, 0xA5) you will get junk.

  • "eg, if the value is 0xA5, it will print 'A5'"

    To be precise, since you specified the format string "[%x]", it will actually print "[a5]"

    "Whats the reason and how can it be corrected"

    You need to review your 'C' course notes/textbook/whatever and understand the difference between formatted output using printf, and direct output using putchar.

  • thank you Andy for clearing my misconception

    Nitin