UART1 speed error

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

Hi all,

 

I have just built a new PCB, with a new ATmega324PA chip installed, and my first run of the board I have a timing/ speed error on the output of the RS232.  The board starts up OK, and I can flash an LED at different speeds, So I have the board running and the programmer talking to the board.

 

I have used a new crystal (see attachment) and two 18pF caps, and I am not sure if this is my speed problem. I am pretty sure my code is correct because I have used this code before, although UART0 on another board.  Below is my initialization function for setting up USART1 at 9600 using a 4Mhz crystal.  Setting up the Baud rate speed I used the table on page 191 on the ATmega324 datasheet - also note for some reason the title of 4Mhz is missing from this page, but looking at the above and below pages I assumed 4Mhz, 9600 is Baud 23.

 

Also this is my first design on my new windows 8.1 PC, so if the below is OK, it may be the driver on the USB - RS232 cable.  

 

Look forward to your input.

 

Thanks,

 

Tuurbo46

 

#define F_CPU 4000000UL
#include <avr/io.h>
#include <stdint.h>
#include <util/delay.h>
#include <string.h>
#include "rs232.h"
typedef unsigned char byte;

void initialize_rs232(void)
 {
	#define BAUD 9600
	#define MYUBRR F_CPU/16/BAUD -3 // equals 23
	// #define MYUBRR 23

	// Set frame: 8 data, 1 stp
	UCSR1B = (1 << RXEN1) | (1 << TXEN1);
	UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);  

	//Set baud rate
	UBRR1H = (MYUBRR >> 8);
	UBRR1L = MYUBRR;

}

void putcr(char ch)
{
	while( ( UCSR1A & ( 1 << UDRE1 ) ) == 0 ); // Wait for ready
	UDR1 = ch;

}

 

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define MYUBRR F_CPU/16/BAUD -3 // equals 23

Where did you get that equation from??? Are the fuses set to use the external crystal? Recheck the data sheet for the UBR @4MHz? (hint it's wrong above)

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Hi Js,

 

Previously this code was used on a ATmega64A with a 16Mhz crystal.  I never set any external fuses.  Maybe this was a default setting.  OK I will look at this now.  Also below are my calculations for working out the BAUD. 

 

16Mhz calcualtions:

 

16M/16/9600-1 = 103.16 which is correct for a 16Mhz crystal.

 

8Mhz calcualations:

 

4M/16/9600-1 =  26.04 which is 3 out from the number quoted on the datasheet 23 for a 4Mhz crystal.  At this point do I just leave it at 26.04, or do I change something else to make it 23?

 

Thanks for your help.

 

Tuurbo46

 

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

26.04 which is 3 out from the number quoted on the datasheet 23 for a 4Mhz crystal.

Do you want to re-look at that? 23 is for 3.6864 MHz not 4 MHz.  Also try this formula (F_CPU / (16UL * USART0_BAUD)) - 1

 

 I never set any external fuses.

You need to set the fuse for External CRYSTAL (not oscillator) or you will lose the chip.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Thu. Nov 26, 2015 - 07:33 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks, I will use the new formula and work and my BAUD this way.

 

OK I have looked at the datasheet page 31, and external clock setting is CKSEL 0000.  I have gone to device programming >> fuses >> SUT_CKSEL and there is no 4MHz option - there is a long list. Could you advice me on which one to select as I do not want to pick wrong one and melt the chip?

 

Thanks

 

Tuurbo46

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

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Ok so i transposed your formula and 25.0416 comes out as 9600.025 which is correct.  So yes 23 was wrong.  I miss read the datasheet.

 

On the fuse setting I am thinking something like EXTXOSC_3MHZ_8MHZ.  Is this correct because my crystal is external and between 3MHz and 8MHz.

 

Thanks

 

Tuurbo46

 

 

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

JS,

 

Thanks you so much, that has sorted it now.  I would of never worked that out.  The only change is the BODLEVEL does not work at 4.3V, I switched it back to disabled and it works.

 

So for future reference on other new designs, where would I find that data, or am I supposed to know this?

 

Many thanks,

 

Tuurbo46