Print int through self defined transmit function


We have created a transmit function to print variables through UART0. It looks like this

#define SPEED 19200
void InitUART0()
unsigned int bittimer = ( F_CPU / SPEED / 16 ) - 1;
/* Set the baud rate /
UBRR0H = (unsigned char) (bittimer >> 8);
UBRR0L = (unsigned char) bittimer;
set the framing to 8N1 /
UCSR0C = (3 << UCSZ00);
Engage! */
UCSR0B = (1 << RXEN0) | (1 << TXEN0);

void TransmitByte0(unsigned char data)
while ( !(UCSR0A & (1 << UDRE0)) )
UDR0 = data;

If we print a single character it seems fine but it we print an int like 100 it gets converted to ASCII and printed as ā€˜dā€™.

How can we print the int as its actual value?

We use an Arduino Mega 1280.


Why don't you just use the standard Serial methods? If you dislike them, you might still want to look up their implementation. Have a look at HardwareSerial.cpp and Print.cpp.


Thanks for your answer!

We read data from a laser navigation unit (SICK NAV200) and we actually use some code someone else wrote last year.

The position, x and y, is 4 bytes long and we read it like this: x = ((int32_t)&laserBuffer[4]); y = ((int32_t)&laserBuffer[8]);

Which works fine. We actually tried to use but we never got it to work with the laser unit. We need to only read 4 bytes for x and y and so on...

We tried to use Serial.print() to print to the computer but then we got a compilation error, "already defined" or something. :-/

Would you please be a little bit more precise on what you do / did and what happened? Analysis on vague hints is pretty hard to do.

Maybe not just fractions of what you think what went wrong but a little bit more context?