Yahoo Groups archive

AVR-Chat

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

Thread

Re: Simple casting winavr

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

Simple casting winavr

2008-01-24 by Bryan Martin

I am attempting to move to C (winavr) from bascom for programming but 
getting hung up on some fundamentals.

Given the following

unsigned char byte_in = 0xA;

I need to print the decimal value to UART so I want to see 10.  Similar to 
printf("%d", byte_in).  Lord I hope this makes sense.

Poor planning on your part does not constitute an emergency on my part.

Re: [AVR-Chat] Simple casting winavr

2008-01-24 by Mike

You will need to the I/O library
#include <stdio.h>

You will need to link printf to the USART
fdevopen((void*) Usart_Tx, (void*) Usart_Rx);

You will then need to provide the low level "get a byte to/from the USART 
routines (called Usart_Tx and Usart_Rx above)

YOu will also need to init the USART to the correct baud rate.


You might want to get a copy of the AVR Butterfly demo program that was 
ported to WinAVR and look at UART.h and UART.c for examples of Usart_TX, 
Usart_Rx, and the USART init routine.


-Mike




----- Original Message ----- 
Show quoted textHide quoted text
From: "Bryan Martin" <registration@myplaceinspace.com>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, January 23, 2008 6:04 PM
Subject: [AVR-Chat] Simple casting winavr


>I am attempting to move to C (winavr) from bascom for programming but
> getting hung up on some fundamentals.
>
> Given the following
>
> unsigned char byte_in = 0xA;
>
> I need to print the decimal value to UART so I want to see 10.  Similar to
> printf("%d", byte_in).  Lord I hope this makes sense.
>
> Poor planning on your part does not constitute an emergency on my part.
>
>
>
>
> Yahoo! Groups Links
>
>
>

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 :)
Show quoted textHide quoted text
----- 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

Re: [AVR-Chat] Simple casting winavr

2008-01-24 by Russell Shaw

Bryan Martin wrote:
> I am attempting to move to C (winavr) from bascom for programming but 
> getting hung up on some fundamentals.
> 
> Given the following
> 
> unsigned char byte_in = 0xA;
> 
> I need to print the decimal value to UART so I want to see 10.  Similar to 
> printf("%d", byte_in).  Lord I hope this makes sense.

static void
format(int num, char digits[6], char *width)
{
     char ndx = 0;
     if(num < 0) {
         digits[ndx++] = '-';
         num = -num;
     }
     else {
         digits[ndx++] = ' ';
     }
     int d = 10000;
     for(int i = 1; i < 6; i++) {
         char n = num/d;
         digits[ndx] = n + '0';
         if(n) ndx++;
         num %= d;
         d /= 10;
     }
     *width = ndx;
}

int
main(int argc, char **argv)
{
     char digits[6], width;

     format(-672, digits, &width);

     for(int i = 0; i < width; i++) {
	putchar(digits[i]);
     }
     putchar('\n');
}

Re: [AVR-Chat] Simple casting winavr -- oops, hit send too soon

2008-01-24 by Mike

You will need to the I/O library
#include <stdio.h>

You will need to link printf to the USART
fdevopen((void*) Usart_Tx, (void*) Usart_Rx);

You will then need to provide the low level "get a byte to/from the USART
routines (called Usart_Tx and Usart_Rx above)

You will also need to init the USART to the correct baud rate.

You might want to get a copy of the AVR Butterfly demo program that was 
ported to WinAVR and look at UART.h and UART.c for examples of Usart_TX, 
Usart_Rx, and the USART init routine.

After that code like

unsigned char a = 0xA;
printf("\r\nThe value of a = %d", a);

will cause printf to produce the string "\r\nThe value of a = 10" and send 
the string one character at a time to the low level routine Usart_Tx which 
is responsible for sending the characters to the USART.  An example of a 
polled Usart_Tx routine is

void Usart_Tx(char data)
{
    while (!(UCSRA & (1<<UDRE)));  // Wait for the USART to become ready
    UDR = data;                                   // Put the character into 
the Usart
}

-Mike



----- Original Message ----- 
Show quoted textHide quoted text
From: "Mike" <k5atm@comcast.net>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, January 23, 2008 6:50 PM
Subject: Re: [AVR-Chat] Simple casting winavr


> You will need to the I/O library
> #include <stdio.h>
>
> You will need to link printf to the USART
> fdevopen((void*) Usart_Tx, (void*) Usart_Rx);
>
> You will then need to provide the low level "get a byte to/from the USART
> routines (called Usart_Tx and Usart_Rx above)
>
> You will also need to init the USART to the correct baud rate.
>
>
> You might want to get a copy of the AVR Butterfly demo program that was
> ported to WinAVR and look at UART.h and UART.c for examples of Usart_TX,
> Usart_Rx, and the USART init routine.
>
>
> -Mike
>
>
>
>
> ----- Original Message ----- 
> From: "Bryan Martin" <registration@myplaceinspace.com>
> To: <AVR-Chat@yahoogroups.com>
> Sent: Wednesday, January 23, 2008 6:04 PM
> Subject: [AVR-Chat] Simple casting winavr
>
>
>>I am attempting to move to C (winavr) from bascom for programming but
>> getting hung up on some fundamentals.
>>
>> Given the following
>>
>> unsigned char byte_in = 0xA;
>>
>> I need to print the decimal value to UART so I want to see 10.  Similar 
>> to
>> printf("%d", byte_in).  Lord I hope this makes sense.
>>
>> Poor planning on your part does not constitute an emergency on my part.
>>
>>
>>
>>
>> Yahoo! Groups Links
>>
>>
>>
>
>
>
>
> Yahoo! Groups Links
>
>
>

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

2008-01-24 by Bryan Martin

HA..  Got it.  You got me looking in the right direction with the itoa. 
Found example code at http://en.wikipedia.org/wiki/Itoa and now its working 
as desired without the added bloat.

Again I owe ya.  Thanks guys
Show quoted textHide quoted text
----- 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

Re: [AVR-Chat] Simple casting winavr

2008-01-24 by Bryan Martin

Thanks Russell.  I will check this out also and see if it works for my goal 
of smaller code.
Show quoted textHide quoted text
----- Original Message ----- 
From: "Russell Shaw" <rjshaw@netspace.net.au>
To: <AVR-Chat@yahoogroups.com>
Sent: Wednesday, January 23, 2008 8:58 PM
Subject: Re: [AVR-Chat] Simple casting winavr


> Bryan Martin wrote:
>> I am attempting to move to C (winavr) from bascom for programming but
>> getting hung up on some fundamentals.
>>
>> Given the following
>>
>> unsigned char byte_in = 0xA;
>>
>> I need to print the decimal value to UART so I want to see 10.  Similar 
>> to
>> printf("%d", byte_in).  Lord I hope this makes sense.
>
> static void
> format(int num, char digits[6], char *width)
> {
>     char ndx = 0;
>     if(num < 0) {
>         digits[ndx++] = '-';
>         num = -num;
>     }
>     else {
>         digits[ndx++] = ' ';
>     }
>     int d = 10000;
>     for(int i = 1; i < 6; i++) {
>         char n = num/d;
>         digits[ndx] = n + '0';
>         if(n) ndx++;
>         num %= d;
>         d /= 10;
>     }
>     *width = ndx;
> }
>
> int
> main(int argc, char **argv)
> {
>     char digits[6], width;
>
>     format(-672, digits, &width);
>
>     for(int i = 0; i < width; i++) {
> putchar(digits[i]);
>     }
>     putchar('\n');
> }
>


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


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

Re: [AVR-Chat] Simple casting winavr

2008-01-24 by Russell Shaw

Bryan Martin wrote:
> I am attempting to move to C (winavr) from bascom for programming but 
> getting hung up on some fundamentals.
> 
> Given the following
> 
> unsigned char byte_in = 0xA;
> 
> I need to print the decimal value to UART so I want to see 10.  Similar to 
> printf("%d", byte_in).  Lord I hope this makes sense.

static void
format(int num, char digits[7])
{
     char ndx = 0;
     if(num < 0) {
         digits[ndx++] = '-';
         num = -num;
     }
     else {
         digits[ndx++] = ' ';
     }
     int d = 10000;
     for(int i = 1; i < 6; i++) {
         char n = num/d;
         digits[ndx] = n + '0';
         if(n) ndx++;
	num -= n*d;
         d /= 10;
     }
     digits[ndx] = 0;
}

int
main(int argc, char **argv)
{
     char digits[7];

     format(-672, digits);

     puts(digits);
     putchar('\n');
}

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.