Problems with RS232 using Mega16

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

Hello,

I''ve been trying to get a simple thing like a serial display to work properly but whatever I do I just can't. I use a mega16, connected to a max232 and on to the computer to check the communication using LookRS232. I get a line status error.

As I go into AVR studio 4, I load the hex and run the program and then checks the USART registers it seems like the UBRRH changes as I change the UCSRC register, and it shouldn't
:cry:
So, I get a lot unwanted in the UBRRH when I change in the UCSRC that Idon't want it and vise versa.

The init code for the USART looks like:

UCSRA=0x00;
UCSRB=0x08; // Lets enable transmission
UBRRH=0x00;
UCSRC=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // asynchronys, 8 bit, 1 stop & not parity
UBRRL=0xCF; // Transferspeed - 2400

Anybody known what I'm doin' wrong?

/Mr G

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

I get the distinct feeling that this is a bug in AVR Studio, not your software. UBBRH and UCSRC share the same I/O location, 0x20. When you write to that location with the MSB set, the write is actually sent to UCSRC. When you write to it with the MSB cleared, the write is sent to UBBRH. When I simulated your code in AVR Studio, I saw the same symptoms as you described. (I compiled using WinAVR.)

However, looking at the disassembly view, I see that the correct instructions are being issued:

15:       	UBRRH=0x00; 
+0000005C:   BC10        OUT     0x20,R1          Out to I/O location
16:       	UCSRC=((1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0)); // asynchronys, 8 bit, 1 stop & not parity 
+0000005D:   E886        LDI     R24,0x86         Load immediate
+0000005E:   BD80        OUT     0x20,R24         Out to I/O location

So, your code is doing the right thing; it is AVR Studio that is wrong in this case -- it doesn't know the difference between UBBRH and UCSRC.

- Luke

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

ok...

Sounds good.

I still have the problem though. My thoughts are going in the direction of my resonator not being exact enought, or me doing the sending in the wrong way.

This is how it looks:

void eb(void)
{
while( !(UCSRA & (1<<UDRE)) ) //Wait for empty buffer
{
AvrXDelay(&MyTimer1, 1); // 1 ms delay
}
}

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

NAKEDFUNC(task1)
{
AvrXDelay(&MyTimer1, 2000); // 2s delay

// Send data to display

eb();
UDR = 0xfe; //Command

eb();
UDR = 0x01; //Clear screen

eb();
UDR = 0x47; //'G'

for(;;) // Infinite loop
{

AvrXDelay(&MyTimer1, 1); // 1 ms delay

}
}

----------------------------------------

/Gert

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

Quote:
I still have the problem though

What do you get? Does anything show up on the terminal at all?
I am only guessing but, what is your clock source (internal R/C can cause problems).
If using the internal R/C, have you loaded the OSCAL?
If external clock source, have you set up the fuses correctly?
Is the terminal program set up the same as AVR for baud rate and data format?

Use the "Code" tags, it makes your code much more readable.

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

ok... on the display I get a whole bunch of chars that shouldn't be there at all... and in LookRS232 I get the following message....

----------------------------------------------
A line-status error occurred
00
A line-status error occurred
00
80 00
A line-status error occurred
80 00 80
A line-status error occurred
-------------------------------------------- all in hex....

I use the internal clock reference for the USART... The speed is set in UBRRL.... I use a 8MHz ceramic resonator so the speed is set to 2400 baud. I use 8 bit, 1 stop bit and no parity, this is set in UCSRC.

What's OSCAL?? The terminal program and the Micro have the same setup.

In what way can the internal R/C cause problems?

/Gert

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

Hi,

Quote:
In what way can the internal R/C cause problems?

The mega AVR's come from the factory with (usually) a 1MHz internal RC oscillator enabled via fuses. Have you changed your fuse selection such that the AVR is operating off your 8MHz ceramic resonator? Make sure you selected Ext. Crystal/Resonator and not Ext. Clock (or Ext. Osc...can't remember which one it is called).

Regards,
Steve

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

If you are not sure what clock source your AVR is running from you can try setting UBRR to 25 (the value for 2400 baud with AVR @ 1MHz) and see what you get on the terminal.

If you have not changed the fuses of your Mega16 in the past, be careful, setting them wrong can cause your AVR to stop working.
Sorry for the warning if you have changed fuses in the past.

What hardware are you using? STK500 or something else?

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

Quote:

The mega AVR's come from the factory with (usually) a 1MHz internal RC oscillator enabled via fuses. Have you changed your fuse selection such that the AVR is operating off your 8MHz ceramic resonator? Make sure you selected Ext. Crystal/Resonator and not Ext. Clock (or Ext. Osc...can't remember which one it is called).

Ok.. I didn't know they had internal oscillators... So I'lll try this first.
Which AVR's have Internal Crystals/Resonators?

Quote:

You are using the UBRR value for double speed transmission but not enabling double speed transmission in UCSRA (bit U2X)

According to the datasheett... looking at the value for an external 8MHz resonator/Crystal... and single, not double ... it should be the right value! This however seems to be wrong value anyway since I didn't know there is an internal osc.

Thanks.. I try Changing both right away.

/Gert

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

Quote:
Quote:
You are using the UBRR value for double speed transmission but not enabling double speed transmission in UCSRA (bit U2X)

According to the datasheett... looking at the value for an external 8MHz resonator/Crystal... and single, not double ... it should be the right value! This however seems to be wrong value anyway since I didn't know there is an internal osc.

Yes, I was wrong. You also might notice that I corrected it.

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

Ok guys....

Works like a charm.... Thank you very much...
LIke I said, I didn't know there was an internal clock so I was pretty lost from the beginning. It also explanes some other stuff I found a bit weird.

So to everybody, thank you very much for your time and help.

/Gert J

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

Hi,

Quote:
Which AVR's have Internal Crystals/Resonators?

Just for the record (not to be picky) they come with internal RC oscillators (RC = Resistor/Capacitor). I think all the mega AVR's have this and the newer tiny AVR's such as tiny13, tiny2313 do also. Some of the older ones do as well but I don't remember which ones. The front page of the datasheet should tell you. Also, if it is important to you then the Devices page here can tell you.

Regards,
Steve