Atmega328p USART

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

I am trying to learn usart with atmega328p.I know what is Baud Rate but I didn't understand what is BAUD_PRESCALE.I looked datasheet and internet but didnt get it.Do you have simple explanation about that ? 

 

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

 

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

 

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

 

void init_uart(void);

 

int main(void)
{    

    while (1) 
    {
        
    }
}
void init_uart(void){
    UBRR0L = BAUD_PRESCALE;         
    UBRR0H = (BAUD_PRESCALE>>8) ; 
    UCSR0B |= ( (TXEN0<<1) | (RXEN0 <<1) ) ;    
    UCSR0C |= ( (UMSEL00 << 1) | (UCSZ00 << 1) | (UCSZ01 <<1) ); 

}

 

 

 

This topic has a solution.
Last Edited: Thu. Jul 30, 2020 - 06:48 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hold on, did you edit that? I built the original post and got the same error but it was because of a missing semi-colon on:

 UBRR0H = (BAUD_PRESCALE>>8)  

but now your post has that. (and I find that the latest code builds without error)

Last Edited: Thu. Jul 30, 2020 - 10:59 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Please see Tip #1 in my signature, below, for how to properly post source code.

 

In general, a "prescaler" is something which divides the input clock frequency before (hence "pre") it reaches the part in question.

 

You can see it in the 'Clock Distribution' diagram in the ATmega328p datasheet:

 

 

But your code seem to be using it with a different meaning - it is just the value being loaded into the Baud Rate Register, UBRR

 

I would suggest that you change the name to avoid confusion!

 

EDIT

 

Which raises the question: where did you get that code ?

 

Please give a link so that we can see the context!

 

 

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...
Last Edited: Thu. Jul 30, 2020 - 11:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ynsemrzmz wrote:

    UBRR0L = BAUD_PRESCALE;         
    UBRR0H = (BAUD_PRESCALE>>8) ; 

You can do that in one go:

    UBRR0 = BAUD_PRESCALE;         

The compiler knows how to deal with 16-bit registers

 

 

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

Yeah i saw that and i corrected it.Thank you

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

http://www.lojikprob.com/avr/c-i... Here is the source that i used for learning avr programming but the language is not English

 

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

ynsemrzmz wrote:

Yeah i saw that and i corrected it.Thank you

You can probably mark post #1 as the solution then ;-)

 

(except that, technically, I think you can't!)

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

The original question was, "what does 'prescaler' mean?" - I think I answered that in #3 ?

 

If so, please mark the solution (see Tip #5); if not, please say what needs clarification

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

ynsemrzmz wrote:
http://www.lojikprob.com/avr/c-i... Here is the source that i used

The author gives contact details here:  http://www.lojikprob.com/hakkimda/

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

Err no, that wasn't the original question (which has since been completely replaced!) It was more like...

 

Why is the code giving me the error:

		.././main.c: In function 'init_uart':
D:\test\test\main.c(31,10): error: called object is not a function or function pointer
		  UBRR0H = (BAUD_PRESCALE>>8) 
		          ^
		make: *** [main.o] Error 1

 

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

clawson wrote:
the original question ... has since been completely replaced! 

Oh - in that case, I never saw the original question.

 

By the time I saw it, it was as it is now:

ynsemrzmz wrote:
I am trying to learn usart with atmega328p.I know what is Baud Rate but I didn't understand what is BAUD_PRESCALE.I looked datasheet and internet but didnt get it.Do you have simple explanation about that ? 

 

#ifndef F_CPU
#define F_CPU 16000000UL
#endif

 

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

 

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

 

void init_uart(void);

 

int main(void)
{    

    while (1) 
    {
        
    }
}
void init_uart(void){
    UBRR0L = BAUD_PRESCALE;         
    UBRR0H = (BAUD_PRESCALE>>8) ; 
    UCSR0B |= ( (TXEN0<<1) | (RXEN0 <<1) ) ;    
    UCSR0C |= ( (UMSEL00 << 1) | (UCSZ00 << 1) | (UCSZ01 <<1) ); 

}

 

@ynsemrzmz: this is why it is really bad to edit a post - especially after someone has replied to it!

 

If you want to make a correction or update, do it in a reply.

 

EDIT

 

Or add it like this - flagged as an "EDIT" - so that people can see what you've done.

 

The forum currently says that the last edit to #1 was  Thu. Jul 30, 2020 - 11:50 AM , and the timestamp for #3 is  Thu. Jul 30, 2020 - 12:08 PM - so well after the OP had been "completely replaced!"

 

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...
Last Edited: Thu. Jul 30, 2020 - 02:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ynsemrzmz wrote:
I didn't understand what is BAUD_PRESCALE

So are you now OK with that ?

 

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

Yeah I got a better understand of baud prescale but i think i have to improve my digital electronics knowledge.Thank you