Question about innitialising USART

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

I'm using an Atmega324P.

I'm not sure I am understanding the innitialisation code for the USART in the datasheet:

/*---------------------------------------------------
Setup Rs232 serial port
USART0
---------------------------------------------------*/
void RS232_Init(uint8_t baud)
{

//Set baud rate
/* Set baud rate */
UBRR0H = (unsigned char)(baud>>8);
UBRR0L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

Am I supposed to be given the function the Baud rate as in 9600 or do I need to pass something else to it?

I'm also not sure I understand the bit in green. I get that there are 2 registers that need to be set for the baud rate but what is the (unsigned char) there?

This is taken from page 178 of the datasheet.

Thanks

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

Why not have a quick look at how it is done ine the examples sitting right there in your computer? C:\WinAVR-20100110\doc\avr-libc\examples\stdiodemo

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Quote:
Am I supposed to be given the function the Baud rate as in 9600
No, you need the baud rate divisor. See the datasheet for the formula (or for standard rates and cpu frequencies, just read it from the chart).

Regards,
Steve A.

The Board helps those that help themselves.

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

Koshchi wrote:
Quote:
Am I supposed to be given the function the Baud rate as in 9600
No, you need the baud rate divisor. See the datasheet for the formula (or for standard rates and cpu frequencies, just read it from the chart).

Thanks.

js wrote:
Why not have a quick look at how it is done ine the examples sitting right there in your computer? C:\WinAVR-20100110\doc\avr-libc\examples\stdiodemo

Maybe I am just tired but I can't make head or tails of what they are doing in that :?

Look, I started with this http://winavr.scienceprog.com/av...

I also went through Deans one. The problem is that the 324P has dual Usarts and that makes adapting anything a little more problematic.

I'm trying to RTFM. :lol:

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

Quote:
the 324P has dual Usarts
So you need to make 2 copies of the code with different numbers. These are my modified files from stdiodemo for the 2 USARTs

edit and "simplified" versions I have used in some projects. F_CPU is obtained from the project settings

#define	USART0_BAUD 115200			//Baud rate USART_0 115K for laser unit
#define	USART1_BAUD 115200			//Baud rate USART_1
#define	Monitor_baud  9600			//Baud rate for monitor mode 9600	
.
.
.
// Initialize the USART0 , tx/rx, 8N1.

void usart0_init(void)
{
  UBRR0L = (uint8_t) (F_CPU / (16UL * USART0_BAUD)) - 1;
  UBRR0H = (uint8_t) ((F_CPU / (16UL * USART0_BAUD)) - 1) >>8;
  UCSR0B = _BV(TXEN0) | _BV(RXEN0); 							/* tx/rx enable */
}

int usart0_putchar(char c, FILE *stream)
{
	loop_until_bit_is_set(UCSR0A, UDRE0);
	UDR0 = c;
	return 0;
}


int usart0_getchar(FILE *stream)
{
  uint8_t c;
	loop_until_bit_is_set(UCSR0A, RXC0);

	if (UCSR0A & _BV(FE0))
		return _FDEV_EOF;

	if (UCSR0A & _BV(DOR0))
		return _FDEV_ERR;

	c = UDR0;
	
	return c;
}

//Initialize the USART1 , tx/rx, 8N1.

void usart1_init(void)
{
  UBRR1L = (uint8_t) (F_CPU / (16UL * USART1_BAUD)) - 1;
  UBRR1H = (uint8_t) ((F_CPU / (16UL * USART1_BAUD)) - 1) >>8;
  UCSR1B = _BV(TXEN1) | _BV(RXEN1); 							/* tx/rx enable */
}


int usart1_putchar(char c, FILE *stream)
{
	loop_until_bit_is_set(UCSR1A, UDRE1);
	UDR1 = c;
	return 0;
}


int usart1_getchar(FILE *stream)
{
	uint8_t c;

	loop_until_bit_is_set(UCSR1A, RXC1);

	if (UCSR1A & _BV(FE1))
		return _FDEV_EOF;

	if (UCSR1A & _BV(DOR1))
		return _FDEV_ERR;

	c = UDR1;
	
	return c;
}

Attachment(s): 

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Thanks,

What does the FILE *stream part of your input do?

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

Quote:

What does the FILE *stream part of your input do?

Read this:

http://www.nongnu.org/avr-libc/u...

It's the mechanism by which you connect printf() (also possibly scanf() for input) to your own output or input routines. It allows printf() (for example) to either direct output to a UART or an LCD or whatever other output device you choose.

It's done in terms of Unix file streams and devices. If you have programmed PCs you may already be familiar with stdout, stdin and stderr?

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

clawson wrote:
Quote:

What does the FILE *stream part of your input do?

Read this:

http://www.nongnu.org/avr-libc/u...

It's the mechanism by which you connect printf() (also possibly scanf() for input) to your own output or input routines. It allows printf() (for example) to either direct output to a UART or an LCD or whatever other output device you choose.

It's done in terms of Unix file streams and devices. If you have programmed PCs you may already be familiar with stdout, stdin and stderr?

Thanks. I've had a read :)

Apart from Delphi, the only PC programming I have ever done was an elementry course in C.

It was so elementary, we never even covered pointers or any of the other variable types. Everything I know about C, I've had to teach myself.

Not familiar with Unix either. I grew up on dos and windows. Never got into Linux.

Is there a thread I can read up on? or a site someone can recommend.

Regarding the original problem, it was another error in the Datasheet. The following works

void RS232_Init(void)
{

//PRR |= (1<<PRUSART0);
/* Set baud rate */
UBRR0L = (uint8_t) (F_CPU / (16UL * USART0_BAUD)) - 1;
UBRR0H = (uint8_t) ((F_CPU / (16UL * USART0_BAUD)) - 1) >>8;


/* Enable receiver and transmitter */
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01)|(0<<UMSEL00)|(0<<UMSEL01);
}

Now I need to read up on sending strings down the USART.

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

Crunch 'n munch on this. These make things easier, for future work .

http://www.nongnu.org/avr-libc/u...

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Thanks alot :)

I'll remember that.