Stack Exchange
AVR UART - Java Android bluetooth communication
I have bluetooth module connected to AVR (Atmega32A) via UART. Some bytes that are transmit from bluetooth module to AVR are not properly recived.
For example the bytes that are properly transmit/recived (UTF-8):
Bluetooth module transmit byte X->recived byte X'
'w'->'w'
's'->'s'
'z'->'z'
'm'->'m'

bytes recived not properly:
'q'->'y'
'p'->'~'
'1'->'9'

Bluetooth connection settings:
Bps/Par/Bits: 115200 8N1
init UART:
#define F_CLK   16000000
#define BAUD    115200

uint16_t ubrr_value = (uint16_t) (((F_CLK)/(16 * BAUD)) - 1);
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);

// 8 bit frame, async mode
UCSRC=(1<<URSEL) | (3<<UCSZ0);
//recive and transmit mode
UCSRB = (1<<TXEN) | (1 << RXEN);

transmit/recive byte by uart:
char USART_ReceiveByte()
{
    while(!(UCSRA & (1<<RXC)));
    return UDR;
}

void uart_sendRS(char VALUE)
{
    while(!(UCSRA & (1<<UDRE)));
    UDR = VALUE;
}

main loop:
while(1)
{
    recivedByte = USART_ReceiveByte();
    uart_sendRS(recivedByte);
}

i would be so glad to know why it does not work properly
EDIT: if i change the order there is result:
'y'->'y'
'~'->'~'
'9'->'9'

EDIT2: probably there is something wrong with setting UBRRL and UBRRH (ubrr_value = 7 in this case), does someone can confirm if it is proper and if the microcontroller can handle such a high BAUD?
#define F_CLK   16000000
#define BAUD    115200
uint16_t ubrr_value = (uint16_t) (((F_CLK)/(16 * BAUD)) - 1);
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);


If you change the order, do the same values or same positions fail?

if i change the order there is result:
'y'->'y'
'~'->'~'
'9'->'9'

The problem here is that you are not initialising the UART properly. You need to set the U2X bit in UCSRA if you wish to use the baud rate as you wish it configured. If you are using avr-libc you may use the following code to properly compute the BAUD rate.
void uart0_init(void) {
#   define BAUD 115200
#   include <util/setbaud.h>
    UBRRH = UBRRH_VALUE;
    UBRRL = UBRRL_VALUE;
#   if USE_2X
    UCSRA |= _BV(U2X);
#   else
    UCSRA &= ~_BV(U2X);
#   endif
#   undef BAUD
    /* other uart stuff you may need */
}

If you look at the datasheet for your microcontroller, section 20.12, you will find a table with this information precomputed for you. Cheers.

Yes, you are right there was problem with types conversion when UBBRL/H was computed and as i dont have JTAG (that would allows me debugging) but USB ASP i managed to count UBRRH and UBRRL values manually (not from equation: f_cpu/(baud * 16)). but finally i use BAUD = 19200 as i cant run it with synchronous mode (i put URSEL in UCSRC), in async mode BAUD = 115200 has 6% error in data flow
do you know why it doesnt work? even with baud=19200
UCSRC=(3<<UCSZ0)

U2X gives a double speed mode, it gives still too high error factor

also the result of using Your code is: there is still some const offset for some characters, for example:
IN bluetooth->OUT AVR
'1'->'9'
but Your code works well with BAUD=19200
