Lukis triya
Lukis triya

Reputation: 1

Send Decimal with USART in ATMEGA32

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

Upvotes: 0

Views: 1259

Answers (2)

pingul
pingul

Reputation: 3473

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.

Upvotes: 1

vlad_tepesch
vlad_tepesch

Reputation: 6883

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.

Upvotes: 0

Related Questions