Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Simple casting winavr

2008-01-24 by Don Kinzer

--- In AVR-Chat@yahoogroups.com, "Bryan Martin" <registration@...> 
wrote:
> I need to print the decimal value to UART so I want to see 10.
This issue has nothing to do with casting, by the way.  That aside, 
the C run-time library has a function to convert a value to a string 
and the one that you mentioned is close but not the right one.  You 
need sprintf() or, alternately, itoa().

uint8_t byte_in;
uint8_t buf[5];
sprintf(buf, "%u", byte_in);

That will get you a null-terminated sequence of digits in the buffer.  
Note that I used the %u format specifier which is for unsigned decimal 
conversion.  The %d that you used if for signed decimal conversion.

Next, you need to output those characters via the UART.  For this, you 
need a different function, one that is not part of the C run-time 
library.  The reason that this is so is because there is no "standard" 
way to implement a UART interface.  You might be using an interrupt-
driven output routine with a fancy queue arrangement while I might be 
using a simple polled output techinique.

You can find examples of both interrupt-driven and polled I/O drivers 
for AVR UARTs in many places.  If you search for "procyon" or "stang" 
and "UART" you'll find but one example written by Pascal Stang.

You'll find that C has significantly more power and flexibility than 
Bascom but the price that you pay is a steeper learning curve and 
having to search to find code you can use or write your own for many 
things that Bascom provides for you.

Don Kinzer
ZBasic Microcontrollers
http://www.zbasic.net

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.