1
votes

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);
1
If you change the order, do the same values or same positions fail?Chris Stratton
if i change the order there is result: 'y'->'y' '~'->'~' '9'->'9'milkProvider

1 Answers

0
votes

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.