ATmega8 with UART,Confused with Clocks

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

First I have read  these :

Using the USART with AVR-GCC

and Every pinned forum 

 

and i have done alot (like alot) of googeling .. no thing helped .. so please help

 

I'm Running atmega8 with the default fuses as shown : 

The Circuit with most of the fuses

I'm simulating with IsIs Proteus 

problem is unknowen chars that the atmega sends (I know I know you have seen this a billion time :) ) 

now I'm sure that the problem is with the clocks  

My Code :

/*
 * Learn.c
 *
 * Created: 3/15/2019 7:07:23 PM
 * Author : MainPc
 */ 

#define F_CPU 1000000UL // What Should i put here .. what is it related with the fuses?
//And for god sake what dose UL mean(after the number) ????
#include <avr/io.h>
#include <util/delay.h>

#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

int main(void)
{
	DDRB =0xff;
	DDRC =0x00;

	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
    while (1)
    {
		PORTB=255;
		_delay_ms(500);
		PORTB=0;
		_delay_ms(500);		

		while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
		UDR = 'a'; // I also tried ascii values like 65 (= A )
    }
}

Every second there is  an led flash on portb (500ms on 500ms off) which comes with sending the 'a' out

now in the simulation it is 10 time slower .. the leds flash 5s on and 5 off ..

I did some tests and come out with this.. consider it :

- if i set the F_CPU = 8000000UL  nothing change at all

- if i set the F_CPU = 8000000UL but set the fuses is the simulator as ( CKSEL = 0100) (It is now Internal rc.8MHz) uart works as a charm ( it sends 'a' to the screen)

but the leds are slower ( 1s on 1 s off )

 

HELP

 

nothing bad from the simulator i tested on it more complicated thinks in the past so don't blame it .. blame me.

and pls explain the division8 and prescaler in ATmega 8 thing ( NOT A SINGLE WORD IN THE DATASHEET ABOUT THEM) and the UL thing also

This topic has a solution.

A Beam of Light out of the War

Last Edited: Fri. Apr 19, 2019 - 06:20 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1


First F_CPU must match the actual system clock which is set by the fuses.

The default fuse settings set up a 1MHZ clock.

Now look at the datasheet, USART section, Example of Baud Rate Settings

Find in the table fosc = 1MHz and 9600 baud,  It will look like this:

Note the Error for 9600 is -7%, the baud rate you select MUST be less then |2%| for successful serial comms!

So you need to pick another baud rate OR enable the U2X bit....

Hint: 4800 baud will work well, or 9600 with U2X bit set.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Thanks for your time ..

 

Now i have set my F_Cpu to 1000000UL with Int Rc.1 MHz fuses 

and my USART_BAUDRATE is 4800

the Leds work fine (500ms on and 500ms off as spoused to  ) 

And the UART Works fine as well ..

thank you very much

didn't know that the error value is actually the absolute of the written in the datasheet

By the way.. Should i call i uart or usart (according to the code )?

 

and what dose the UL after numbers mean ?? 

Thanks Again 3>

A Beam of Light out of the War

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

AbDoO_ wrote:

By the way.. Should i call i uart or usart (according to the code )?

 

and what dose the UL after numbers mean ?? 

AVRs have USART or Universal Sync / Async Receiver Transmitters,  A UART does not do sync comms.

 

UL is Unsigned Long, on an AVR Long = 32 bit number,  Unsigned is positive values only, range 0-4294967296 

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Comment for the original poster...

 

"synchronous" means "with a clock", like SPI or TWI/I2C. You have to transmit both the  clock and the data. Most UARTs do not do synchronous though the added internal circuit to do it is fairly minimal. 

 

Jim

 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net