Search code examples
carduinoembeddedavruart

atmega16 serial communication not working (both uart_send and uart_receive)


I'm trying to communicate between a BLE using Atmega16 with android app(Just to send a character and receive it back).

I'm able to transfer data between BLE with arduino and app but when i'm using atmega16 micro-controller it's not working. When i'm trying to send a character and recieve it back some characters are getting back exactly (0 to 9,q,w,r,t,y,u,p,z,s,x,v,:,^,=,?,;), but others are not.

I think the problem is with my baud rate and CPU frequency. I tried many combinations but none of them are returning whole charcters back exactly. These are my uart initialization and Interrupt functions

I've used CPU frequency 14.7456MHz and Baud rate 230.5k. I tried using baud rate 9600 at 8Mhz frequency but for these values random garbage values are coming back.

#define F_CPU 14745600UL
#define BAUDRATE 230500UL
#define BAUD_PRESCALE ((F_CPU / (BAUDRATE * 16))) - 1

void uart_init() {
    UBRRL = (unsigned char) (BAUD_PRESCALE);
    UBRRH = (unsigned char) (BAUD_PRESCALE >> 8);

    UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
    UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);

    // Need to disable JTAG twice. Read spec sheet for details.
    // http://www.avrfreaks.net/comment/618701#comment-618701
    MCUCSR = (1 << JTD);
    MCUCSR = (1 << JTD);
}        

ISR(USART_RXC_vect) {    
    uint8_t c = UDR;        
    UDR = c;
    PORTA = c;
}

Any help!!


Solution

  • We need to change fuse bits not only when using external crystal/resonator, but we also have to change fuse bits when we are using internal RC oscillator. The default configuration of fuse bits is for frequency 1Mhz. For different frequencies of internal RC oscillator we need to set the fuse bits accordingly. This fuse bit calculator will be helpful.

    How to set fuse bits is explained in this blog.

    I don't know earlier that we have to set different fuse bit configurations for different internal frequencies also.

    Finally i solved my problem with your support. Next i have to transfer strings and much more.

    Thank you all.