Send Decimal with USART in ATMEGA32

1.1k Views Asked by At

I have problem in send a serial data via USART it's send a ASCII not decimal.

this is my code :

    while(!(UCSRA&(1<<UDRE))){}
    UDR=12;

and this is header :

UBRRH=(uint8_t) (UBRR_CALC>>8);
UBRRL=(uint8_t) (UBRR_CALC);
UCSRB=(1<<TXEN)|(1<<RXEN);
UCSRC=(1<<URSEL)|(3<<UCSZ0);

how to send decimal data , i want to read from an ADC and send with USART...

Thank you very much

2

There are 2 best solutions below

0
vlad_tepesch On

If 'decimal' means string representation of numbers to you, you should look at sprintf or itoa.

You need a character buffer that can be filled by one of these functions with a string representing your number.

Please read a C-Tutorial or book about different basic data types especially the difference between characters/strings and integral data types.

0
pingul On

You can't convert a decimal number like float to char without losing all the data. However, you only want to expose the bits so you can send them via USART, and then get back your decimal value. You could do this via a union:

union FloatChar {
    float f;
    char c[4]; // or however big a float is on your compiler
};

You could then use it like

union FloatChar fc;
fc.f = adc.getValue();
usart.send(fc.c);

and use the same union to convert it back to floats when it gets to the other side of the connection.