1

I have been unable to solve a simple USART transmit/receive program. The problem is that I used this in the past and worked effortlessly but now it returns in my Linux machine question marks and in windows some boxes and Chinese characters. I don´t understand what changed. I have tried a lot of different things like changing the code and the parts but nothing seems to solve it.

I´m using a AVR atmega 328p, and a pololu usb programmer. I flashed it from the atmel studio in windows and from avrdude in linux.

Here is the code:

#define F_CPU 1000000
#define BAUD 9600

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


void init_USART(void);
void transmit_Byte(char data);
char receive_Byte(void);

int main(void)
{
    char TEST;
    init_USART();                               /* Initialize USART */

    while (1) 
    {

        TEST =  receive_Byte();
        transmit_Byte(TEST);

    }
}


void init_USART(void)
{
    UBRR0H = UBRRH_VALUE;                       
    UBRR0L = UBRRL_VALUE;

    #if USE_2x
        UCSR0A |= (1 << U2X0);
    #else
        UCSR0A &= ~(1 << U2X0);
    #endif

    UCSR0B = (1 << TXEN0) | (1 << RXEN0);           /* Enable USART 
                                                    transmitt/receive */
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);         /* 8 data bits, 1 stop 
                                                            bit */ 
}

void transmit_Byte(char data)
{
    loop_until_bit_is_set(UCSR0A, UDRE0);           /* Wait for empty 
                                                    transmit buffer */
    UDR0 = data;
}

char receive_Byte(void)
{
    loop_until_bit_is_set(UCSR0A, RXC0);
    return(UDR0);
}

This is my first question so I apologize in advanced if I did something wrong.

Thanks a lot for your time!

edit: I have tried a ftdi232 and a mcp2221

edit 2: After addin UL still not as expected but got less unknown characters.

SOLVED: But I don't understand why. I changed the fuse CKDIV8 to 1 and it started working. The datasheet says it comes factory with 8 MHz clock and the CKDIV8=0 so it is set to 1 MHz. So why is it inversed? I also tried previously clock_prescale_set(clock_div_8); as the datasheet suggest for prescaling and it didn't work neither.

What is the difference between setting the CLDIV8 and the avr/power.h function?

5
  • does your µC really run at 1MHz? May 6, 2017 at 20:36
  • If so, @ 1 MHz, try to write #define F_CPU 1000000UL May 6, 2017 at 21:01
  • Yeah it is set to 1 Mhz. I tried the UL after F_CPU and BAUD and i got less unknown characters but still not what is expected i will put a photo May 7, 2017 at 1:45
  • From latest datasheet, I read the CKDIV8 is set, so you have to set CKDIV8=1 "The device is shipped with the CKDIV8 Fuse programmed." Please edit your code above with the correct settings, so if someone comes across this page ... +1 May 7, 2017 at 11:06
  • "Note that the fuses are read as logical zero, “0”, if they are programmed" - quote from reference guide "Memory Programming->Fuse bits". CKDIV8 sets prescaler register value 8, that equals division of CLOCK by 8. I assume clock_prescale_set(clock_div_8); does the same thing, so they equal. To set CLOCK to 8Mhz you should do something like clock_prescale_set(0);
    – nopasara
    May 9, 2017 at 16:48

0

Your Answer

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

Browse other questions tagged or ask your own question.