USART working Only When Freq=1Mhz ,Fuse bits are set correctly(I think))

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

hello ..

I know This is repeated ...

I'm using ATmega8 , The problem is that when i set the clock to internal RC 1MHZ (in fuse bits and code) with buad rate set to 2400 (error rate is 0.2% according to datasheet) it works perfectly !

But  when i set the clock to Internal 8MHz (both in fuse bits and code) with baud rate to 9600,4800,2400,19200 (I tried all 4) (error rate is 0.2% according to datasheet) it outputs none sense on Terminal screen

I ofcourse checked that the baud rate is the same in my serial terminal And the code ..

 

My Code:


#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

//-------------------------------------------------------------------------------------------------

void Usart_Init(unsigned long USART_BAUDRATE)
{
	unsigned int BAUD_PRESCALE;
	BAUD_PRESCALE = (((F_CPU / (USART_BAUDRATE * 16UL))) - 1);

	UCSRB = (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
	UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
};
//-------------------------------------------------------------------------------------------------
void Usart_Write(char Data2Write)
{
	while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
	UDR = Data2Write;
};
//-------------------------------------------------------------------------------------------------
void Usart_Write_Text(char *text_){
	unsigned char c;

	while ((c = *text_++) != 0) {
		Usart_Write(c);
	}
	Usart_Write(0);
}

int main(void)
{	

	DDRB=0xff;
	PORTB=0;

	Usart_Init(9600);

	Usart_Write_Text("Hello");
	Usart_Write(0x0d);        // new line
	Usart_Write_Text("-------------");
	Usart_Write(0x0d);

	for(char i =0 ; i<10 ; i++) // quick led flash Just to check that the atmega has started up
	{
		PORTB=255;
		_delay_ms(50);
		PORTB=0;
		_delay_ms(50);
	} 

    Usart_Write_Text("StartingUp... \r");
    while (1)
    {
		Usart_Write_Text("still alive : )\r");
		PORTB=255;
		_delay_ms(500);
		PORTB=0;
		_delay_ms(500);

    }
}

 

 

Fuse Bits

 

Edit: ((Spelling))

This topic has a solution.

A Beam of Light out of the War

Last Edited: Fri. Sep 18, 2020 - 01:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Couple more things to consider :

 

- It's working perfectly on Simulation (Proteus) in both 1MHz and 8MHz

- The same problem applies when using Internal 4Mhz

- I tried adding 1uF cap between Vcc and GND

- I'm using Ardunio board as Serial bridge between PC and my ATmeag 

- I tried changing the usb cable

A Beam of Light out of the War

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

A very silly question but you did also change the PC terminal baud rate to something other than 2400?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

IIRC, the M8 has three or is it four different internal RC oscillators, and each one has a calibration byte to go with each osc., but only the 1MHz is loaded into OSCCAL automatically.

So did you also find the cal byte and save it to eeprom so you can load it and save it to the OSCCAL reg upon startup? 

The internal RC osc, frequency will change with room temperature and VCC voltage level, so is not ideal for use with USART comms, best to use an external xtal (best) or external resonator, or external clock for solid operation.  Makes life easier if a known good clock source is used!!!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

May be ok to leave out the xtal, if messing around.  However,  why waste time not using a crystal?  It may borderline work sometimes, or for a while.  Build 100 & 10 won't work

You can leave out caps on Vcc and some might work---but you add those, right?

Save a headache & put in a little xtal....be right on the money.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

I recommend using a crystal:
7.3728Mhz is the best choice for Usart communication.
See in the data sheet. There you have 0% error in the baud rate.

 

Ellen

Senior electrical engineer

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

avrcandies wrote:
  why waste time not using a crystal?

Exactly!

 

Why deliberately create problems for yourself?!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for reply Everybody I really appreciate it 

 

clawson wrote:
did also change the PC terminal baud rate to something other than 2400?

 

Yes,ofcourse

 

ki0bk wrote:
did you also find the cal byte and save it to eeprom so you can load it and save it to the OSCCAL reg upon startup ?

Actually no , Because as i remember i have used The internal 8MHz clock on a new atmega8 without doing anythings to cal byte and it worked back then (in a year ago)

I'll try it now thx ..

 

ki0bk wrote:
The internal RC osc, frequency will change with room temperature and VCC voltage level,

Actually the temp in my room now is about 43c (Really Hot ) ...

 

ki0bk wrote:
best to use an external xtal (best)

Yes And that's what i tried but i messed up the fuse bits (My Bad ..  )

I had to buy another one till i figure out how to fix the broken one (Another subject)

I'm going to use the Xtal one more time but with more cation this time ..

 

I will redo some experiments with crystals (and cal byte) and post the results here, Thx Everyone .

A Beam of Light out of the War

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

AbDoO_ wrote:
I had to buy another one till i figure out how to fix the broken one (Another subject)

There is a tutorial here on how to fix "bricked" AVR's: https://www.avrfreaks.net/forum/...

 

Jim

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Hello Again 

I have just used a 8MHz Crystal with NO caps and IT WORKED 

Thanks Every body

Now couple more questions pls ..

 - How much important are the caps on the Crystal , and how dose their value  effect the clock ? any guide/tut on that ? (not that my application needs time measurements with timers)

 - I Really do need Newbie friendly step by step tutorial on calibrating AVR Internal clocks .. or if u give me a ready made code and circuit  ...That would be awesome ..

Thanks 

 

A Beam of Light out of the War

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

AbDoO_ wrote:
I have just used a 8MHz Crystal with NO caps and IT WORKED

Are you working on a solderless breadboard ... ?

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

AbDoO_ wrote:
if u give me a ready made code and circuit  ...That would be awesome ..

it is all in App Note AVR042 AVR h/w design considerations

 

Jim

Edit: you may find this old pdf helpful

 

Attachment(s): 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ki0bk wrote:
it is all in App Note AVR042

and you will find all the App Notes - and other useful stuff - on the Product Page:

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

Definitely worth a browse

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...