Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Re: Simple casting winavr

2008-01-24 by Bryan Martin

Thanks for the quick response Don.  Your example works as expected (not that
I doubted you).  Can I have a do over please.

One of the reasons for moving to C was due to the size of code being
produced when using bascom.  Nothing against bascom.  What I am doing is
getting data back from a RTC in BCD format.  All that is going nicely but
now im at the point of converting the data im getting back for display
purposes.  I was attempting to stay away from printf/sprintf etc... due to
the amount of program space it takes up.  For instance before your example
my code was using 7%.  Just adding your example jumped the code size up to
26% due to stdio.h.  So needless to say you have a price to pay.  Quick and
fat or slow and lean.  Sadly, like my personal life I need to be moving
tward the latter :)  While I understand I am reinventing the wheel I just
need chunks here and there without the all the extras.

I already have the code for outputting to USART and its working nicely.  I
can send it strings all day long such as output("testing 123\r\n") and get
the results I am after.  However I am getting seconds such as 0xA back and
would like to send it to output for display as 10.  Instead I get a garbage
character _ which is the absolute value.  Not sure if it matters but my
output is defined as follows

void output(unsigned char *data)

Please let me know if this is not clear.  Thanks again for looking at this
and responding so quick.  Best I remember you helped me out of a bind last
year about this time :)

----- Original Message ----- 
From: "Don Kinzer" <dkinzer@easystreet.com>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, January 23, 2008 8:19 PM
Subject: [AVR-Chat] Re: Simple casting winavr


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





--------------------------------------------------------------------------------


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.516 / Virus Database: 269.19.9/1238 - Release Date: 1/22/2008 
8:12 PM

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.