On Fri, Apr 22, 2005 at 03:05:51PM -0000, arhodes19044 wrote:
> I have not used the printf functions yet since I just started using
> these uC's. But, don't I have to use printf and its variants with a
> certain output stream?
Not with 'sprintf()' which writes its output into a memory buffer. A
simple way to convert a float to ascii is simply to:
#include <avr/pgmspace.h>
#include <stdio.h>
...
sprintf_P(my_buf, PSTR("%f"), my_float);
...
Where 'my_buf' is a char [] big enough to hold the result.
If you use 'printf()', then yes, you need to provide some type of
output function, but it is quite flexible. Initialize like this:
int my_putc(char ch)
{
...
return ch;
}
...
/* initialize libc stdio so 'printf()' works */
fdevopen(my_putc, NULL, 0);
printf_P(PSTR("Hello world!\n"));
...
The 'my_putc()' function just needs to be able to handle output a
character at a time, but other than that, it can drive any output
device that you can dream up. For simple polled serial, it's very
simple, something like:
int my_putc(char ch)
{
loop_until_bit_is_set(UCSR0A, UDRE);
UDR0 = ch;
return ch;
}
Of course, you will need to initialize the UART also, before calling
'printf()'.
For interrupt driven serial, it's slightly more complicated -
basically put the character into a buffer and let interrupt driven I/O
continue the process. That way, your 'printf()' can return
immediately if that is important.
> I have an LCD display setup as a 4-bit parallel device. I do not have
> it set up for any interrupt driven output, but I might do that to
> increase speed in the calling program. Right now I just send the
> stuff character bycharacter until it is done and then come back to the
> calling routine. I will have to see if sprintf will work in such a
> way.
'sprintf()' will not work that way, but you can easily make 'printf()'
work as described above by simply providing your LCD equivalent of
'my_putc()' above.
For more information on this, see the avr-libc documentation here:
http://www.nongnu.org/avr-libc/user-manual/
Specifically:
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html
-Brian
--
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/Message
Re: [AVR-Chat] Re: some C string conversion functions?
2005-04-22 by Brian Dean
Attachments
- No local attachments were found for this message.