Problem with Serial communication in Atmel 7

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I am trying to enable simple Uart communication with my computer.

I am using atmega32 microcontroller and inbuilt Terminal in Atmel Studio 7

 

This is my code:

 

#ifndef F_CPU
#define F_CPU 1000000ul
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>



void init()
{
    UCSRB |=  (1<<TXEN) | (1<<RXEN);
    UCSRC |= (1<<URSEL) | (1<<UCSZ0) | (1<<UCSZ1);
    UBRRL = 6
    UBRRH = (6 >> 8);
}



void USART_Transmit(unsigned char data)
{
    while(!(UCSRA & (1<<UDRE)));
    UDR = data;
}

unsigned char USART_Receive(void)
{
    while((UCSRA & (1<<RXC)) == 0);
    return UDR;
}

int main(void)
{
   unsigned char c;
   init();
  
   
    while (1) 
    {
        c =  USART_Receive();

        USART_Transmit(c);
        
   }
}
The problem is when I try to send the character I'm getting back garbage value like this (¥§­¶ß··öŸ)

Can you help me please!

 

 

This topic has a solution.
Last Edited: Wed. Nov 11, 2020 - 09:24 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Welcome to AVRFreaks!

 

Well it looks like your attempting to do 9600 baud with a 1MHz clock, and the data sheet says:

 

Notice the error rate for this baud rate is greater then 2%!

Don't worry, you are about the 1 gazillionth newbie to try that with the same results.

So to make serial comms work, you should use a stable and accurate clock source, that usually means a xtal connected to your AVR and fused so it runs using it.

Also clear the clock/8 fuse while your at it.  Make sure you post what fuse settings you want to use BEFORE you attempt to change them on your AVR so we can look at them and verify they are correct, or you may brick your AVR!   

Now, forget trying to use the terminal in AS7, it was some interns summer project that never worked so well.

Instead install any number of terminal programs, such as TeraTerm, PuTTy, Bray'sTerm, etc and use a USBttl cable to connect to your AVR's TXD/RXD pins.

Make sure TXD on the cable, connects RXD on the AVR, and

RXD on the cable connects to TXD on the AVR,

and connect the GND on the cable to GND on your AVR.

 

That should get you going.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Also clear the clock/8 fuse while your at it. 
ATmega32 doesn't have that. The "old school" AVRs had four separate internal oscillators at 1MHz, 2MHz, 4MHz and 8MHz. So what he would really need to do is change CKSEL bits to switch away from 1MHz (I'm guessing 8MHz might be a good choice). Note however that whichever of the four you choose the chip loads the factory calibration for 1MHz into OSCCAL at power on. So if you want more accuracy after selecting 2/4/8Mhz (and for UART you probably will!) then use your programmer to read out the factory calibration value for your selected oscillator then early in the code write that value into OSCCAL to calibrate the oscillator to be a bit more accurate (assuming you intend to operate at 5V and 25C).

 

Be warned that changing CKSEL fuse bits carries some danger - if you set the wrong CKSEL you may find it quite difficult to contact the chip again.

 

An alternative approach (as the table Jim showed illustrates) is that using a 1MHz clock if you use U2X mode and put the value 12 rather than 6 into UBRR then the error drops from -7.0% to just 0.2% which should work nicely.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hello,

 

I'm trying to enable a 16MHz external cristal/oscilator on my Atmega32A microcontroller.

I'm using a Engbedded AVR Fuse Calculator and this is a AVRDUDE argument:  

-c usbasp -p m32 -P usb -U lfuse:w:0xff:m -U hfuse:w:0x99:m

 

Is this correct?

 

Thank you!!!