1
\$\begingroup\$

I am working on a project using Atmega16 micro-controller of AVR family. I am using UART for my project. I need to send data to my PC AT 9600 baud rate via an USB-TTL converter and view the data using 'putty'.

I am getting absurd data in putty. I am not an electronics guy but a computer science guy so need a little bit of help.

I guess I might be writing the wrong fuse bits or something else. Please specify the fuse bits if possible

The program for the micro-controller is below

#include<avr/io.h>
#include<util/delay.h>

#define UBRR_VALUE 51

//function to initialize UART
void uart_init(void)
{
    UBRRH=(UBRR_VALUE>>8);//shift the register right by 8 bits
    UBRRL=UBRR_VALUE;//set baud rate
    UCSRB|=(1<<TXEN)|(1<<RXEN);//enable receiver and transmitter
    UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);//8 bit data format
}

//function to transmit data
void uart_transmit(unsigned char data)
{
    while(!(UCSRA & (1<<UDRE)));
    UDR=data;
}

void transmit_string(char *str_data)
{
    while(*str_data)
    {
        uart_transmit(*str_data);
        str_data++;
    }
}

int main(void)
{
    uart_init();
    while(1)
    {
        //transmit_string("hello");
        uart_transmit('h');
    }
    return 0;
}

The fuse bits specifications are given below

enter image description here

enter image description here

Thanks in advance for any type of help

\$\endgroup\$
2
  • 1
    \$\begingroup\$ What is your clock frequency & how did you calculate your baud rate divisor? \$\endgroup\$
    – brhans
    Commented Aug 9, 2016 at 11:56
  • 1
    \$\begingroup\$ Gibberish characters are normally a result of an incorrect baud rate setting. You haven't given enough details to check those settings. \$\endgroup\$
    – Andrew
    Commented Aug 9, 2016 at 11:59

1 Answer 1

5
\$\begingroup\$

Your baud rate divisor seems to select a baud rate of 19200 instead of 9600:

baud = (clock speed) / ( 16 * (UBRR + 1) )

Try to set UBRR_VALUE to 103 and see if your communication gets better.

A proper way would be to define F_CPU and BAUD, and let the macros from setbaud.h do the calculation:

#include <avr/io.h>

#define F_CPU 16000000

static void
uart_9600(void)
{
#define BAUD 9600
#include <util/setbaud.h>
    UBRRH = UBRRH_VALUE;
    UBRRL = UBRRL_VALUE;
#if USE_2X
    UCSRA |= (1 << U2X);
#else
    UCSRA &= ~(1 << U2X);
#endif
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hello...I saw the micro-controller board..there was a 12 MHz crystal oscillator...so i calculated ubrr value using F_CPU as 12000000 and I get ubrr value as 77 for 9600 baud rate....Communication works perfectly now...thanks \$\endgroup\$ Commented Aug 10, 2016 at 16:16
  • \$\begingroup\$ @DebeshMohanty I'm glad I could help. You can "accept" my answer if you feel like it: that would tell the other users the information in the answer is relevant to them if they have a similar problem. \$\endgroup\$ Commented Aug 10, 2016 at 16:19
  • \$\begingroup\$ BTW, I have assumed F_CPU of 16MHz by looking at the settings dialog in your screenshots. Perhaps you should update your settings with the real clock frequency there as well. \$\endgroup\$ Commented Aug 10, 2016 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.