Skip to content

Instantly share code, notes, and snippets.

@monpetit
Created July 20, 2016 02:58
Show Gist options
  • Save monpetit/e6705342c6bbd5a17d03dda4b5e53710 to your computer and use it in GitHub Desktop.
Save monpetit/e6705342c6bbd5a17d03dda4b5e53710 to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <stdio.h>
void usart_putchar(char data);
int usart_putchar_printf(char var, FILE *stream);
static FILE mystdout; /* FDEV_SETUP_STREAM(usart_putchar_printf, NULL, _FDEV_SETUP_WRITE); */
void usart_putchar(char data)
{
// Wait for empty transmit buffer
while ( !( UCSR0A & (1 << UDRE0)) ) ;
// Put data into buffer, sends the data
UDR0 = (unsigned char)data;
}
// this function is called by printf as a stream handler
int usart_putchar_printf(char var, FILE *stream)
{
// translate \n to \r for br@y++ terminal
if (var == '\n') usart_putchar('\r');
usart_putchar(var);
return 0;
}
void retarget_stdio(void)
{
// setup our stdio stream
mystdout.put = usart_putchar_printf;
mystdout.get = NULL;
mystdout.flags = _FDEV_SETUP_WRITE;
mystdout.udata = 0;
stdout = &mystdout;
}
void setup(void)
{
Serial.begin(19200);
retarget_stdio();
puts("*** START ***");
}
void loop(void)
{
static uint32_t count = 0;
static float realnum = 0.23;
printf("count = %04u\r\n", count++);
printf("real number = %7.2f\r\n\r\n", realnum);
realnum += 1.47;
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment