samedi 2 juillet 2016

Printf with redefined stdio not formatting correctly using usart

I'm working with an Arduino Uno with an Atmega328p microcontroller using an AVR bootloader with Atmel Studio.

I've set up USART communcation and it successfully can send data that is read by a terminal on my computer but the formatting is incorrect when using printf.

    /*
 * USART_HelloWorld.c
 *
 * Created: 6/20/2016 4:17:16 PM
 * Author : Kevin
 */ 

#define F_CPU 16000000
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
#include <avr/io.h>
#include <stdio.h>

static int USART_Transmit(char, FILE*);
static FILE mystdout = FDEV_SETUP_STREAM(USART_Transmit, NULL, _FDEV_SETUP_WRITE);

USART_Init(unsigned int ubrr){
    UCSR0B |= (1<<TXEN0)|(RXEN0);//enables transmitting and receiving
    UCSR0C |= 3<UCSZ00; //8 bit data, 1 stop bit
    UBRR0H |= (unsigned char)(ubrr>>8);  //sets baud rate
    UBRR0L |= (unsigned char)ubrr;//sets baud rate

}
static int USART_Transmit(char data, FILE *stream){
    if(data=='n') 
        USART_Transmit('r',stream);
    while(!((UCSR0A)&(1<<UDRE0)));
    UDR0 = data;
    return 0;
}
int main(void)
{
    USART_Init(MYUBRR);
    stdout = &mystdout;
    printf("Server name= 0x%XnMy name is %snThis number is %d!n", 0xDEADBEEF, "Kevin", 5);
    while (1);
}

I'm referencing this when making this.

I should get output like:

Server name=0xDEADBEEF
My name is Kevin
This number is 5!

But i get:

Server name= 0xBEEF
My name is 
This number is 270!

Any suggestions

Aucun commentaire:

Enregistrer un commentaire