Can't get my AVR run from external clock ==> USART garbag

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

I am struggleing to get USART communication on-line with a mega8515-16, but all I can read out is garbage, some bytes at around 220-255, I am trying to send "test!#13#10".
I am using Hyperterminal 9600 Baud, Even Parity, No hardware control 1 Stop bit to receive data. I get only part of the data (I can see how fast it should be going with the LED), and garbage :(

Below is my source code, I am currently running on internal oscillator, but as soon as I know what fuse bits to program, i'll move to an external crystal... But I have wasted one of my uC, because of bad fuse bits, and don't wanna waste the other one too :(

Below is my source code:

.include "m8515def.inc"

.def temp = r16
.equ CLOCK = 4000000
.equ BAUD = 9600
.equ UBRRVAL = CLOCK/(BAUD*16)-1

ldi temp, LOW(RAMEND)
out SPL, temp
ldi temp, HIGH(RAMEND)
out SPH, temp

ldi temp, 0xFF
out DDRC, temp

ldi temp, LOW(UBRRVAL)
out UBRRL, temp
ldi temp, HIGH(UBRRVAL)
out UBRRH, temp

; Frame-Format: 8 Bit, Even parity
ldi temp, (1<<URSEL)|(3<<UCSZ0) | (2>>UPM0)
out UCSRC, temp

sbi UCSRB,TXEN

ldi R22,0xFE
ser R23
loop:
inc R22
out PORTC, R22 ; Blink a LED on PORTC0

ldi temp, 'T'
rcall serout
ldi temp, 'e'
rcall serout
ldi temp, 's'
rcall serout
ldi temp, 't'
rcall serout
ldi temp, '!'
rcall serout
ldi temp, 10
rcall serout
ldi temp, 13
rcall serout
rjmp loop

serout:
sbis UCSRA,UDRE
rjmp serout
out UDR, temp
ret

axos88

Last Edited: Sun. Jan 28, 2007 - 10:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I am currently running on internal oscillator
but you are saying that
Quote:
.equ CLOCK = 4000000
in your equate instead of 1000000 which is the default value.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I have changed the default value to 4Mhz internal... But i have double checked with Clock = 1 000 000 as well... Still garbage

axos88

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

Because at 1Mhz, there is a 7% error at 9600b/s, i tried it with 4800 b/s too, now there is NO reception... And another VERY WIERD thing that is happening... I am listening to music with headphones. Whenever I connect the AVR to the COM port, a buzz appears on the headphone.. Interference, but... WHY???

I have connected solely the PIN2 of the com port to the AVR (through a MAX232), didn't connect the grounds... Should i connect them?

axos88

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

axos88 wrote:
I have connected solely the PIN2 of the com port to the AVR (through a MAX232), didn't connect the grounds... Should i connect them?

How would you expect it to work otherwise? You have no reference for the signal to return any current that is used in the data signal transfer.

You need a common ground (GND) reference between the two systems.

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

Last Edited: Sat. Jan 27, 2007 - 10:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The internal oscillator is not 4.000000MHz, it's 4MHz.
4MHz covers whatever from 3.50000000MHz to 4.49999999MHz. Can be +/- 20%.

Usart communications need stable clock sources in the neighbourhood of 2% or less, which can be achieved at least by external crystals, external ceramic resonators and oscillator modules as the main clock source, and even from 32768Hz watch crystals when using the watch crystal as the calibration reference for internal RC oscillator.

The datasheet says how much it can vary by supply voltage and temperature and how much is the initial tolerance.

I wonder how many times a week this question comes up here. Can we post a sticky message about the subject?

- Jani

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

Yes you must connect the ground. At 4800 baud it should work well. I don't know how precise is the internal oscillator, some say it's good enough with calibration. I have alwas used crystals. Which programmer do you have? Be carefull not to mess up the fuses.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Yeah I already messed up the fuses once...

Now that I connected the ground, I am still getting garbage, but other kind of garbage, and at least it's periodic. Instead of the 10-byte-length "test!!!!#13#10" I am getting '3€`f†æø' (7byte only).

I can confirm that I am running at ~4 Mhz internal.

Can you please tell me what fuses should i program to get it running on external crystal, 4Mhz, 27pf capacitors? I already wasted one of my uC's, and don't wanna lose this one too, i can only order them in a 7-days :((

axos88

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

I am using SI-PROG and PonyProg

axos88

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

CKOPT and a few (4?) CKSEL fuse bits. The datasheet explains how the bits should be configured to use the external cystal, and remember that in PonyProg, a checked box means 0 (programmed) and unchecked box means 1 (unprogrammed).

EDIT:

You can propably revive the dead AVRs which have misprogrammed fuse bits by applying external clock to them (xtal1 or xtal2 pin, whichever is the input). External logic level clock around (1 to 4 MHz) can be gotten from a oscillator module, another AVR, signal generator etc. You can even build an oscillator from an inverter and crystal and 22pF caps or an RC oscillator.

- Jani

Last Edited: Sat. Jan 27, 2007 - 11:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't use that type of programmer so may have to wait until someone comes up with the answer. As soon as you can afford an AVRISP buy one! It will save you a lot of time.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I tried setting the fuses, but something is wrong. I set
CKOPT = 0 (checked) and CKSEL=1111, and when I try to read the device, the first few tens of bytes read O.K., but after that it reads garbage... Why is that?

axos88

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

Quote:
the first few tens of bytes read O.K., but after that it reads garbage... Why is that?
Perhaps because your program is only a few tens of bytes and the rest is unprogrammed (0xff??) ? Can you read the signature bytes correctly and consistently?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

What I meant under "garbage" is that the data beyond my program is NOT 0xFF... The first 500-600 bytes (it varies with each read) reads O.K. (the program data, and a few more 0xFFs), and after that, random data.

If i keep the device under 4MHz internal clock, but do not remove the crystal and capacitors from the circuit, the same thing happens, only the O.K. data is longer... If I remove the crystal and the capacitor, everything works fine. The same thing does NOT happen to 1Mhz internal.... Do you have any idea why this could be happening:?:

axos88

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

I have looked through a tutorial on these fuse bits, and I am setting the fuses O.K.!... what could be wrong? My crystal is damaged? Am i using wrong capacitor values? I am using 27pF, whereas the recommended is 12-22pF. I saw the 27pF value on a schematic, and bought those at the time

axos88

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

Does the program work and can you read the signature bytes correctly and consistently? Don't worry about the "garbage" for now. I use 15pF with 8-16MHz crystals but the 27pF with the 4MHz crystal should be OK. How do you determine that there is garbage in the chip? Are you looking at the flash trough a debugger like a Dragon ot JTAG ice or are you reading the flash through the programmer and saving it info a file? Don't get side tracked by this if it all works.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I am reading the flash through a programmer and looking at the bytes.
The program works on the chip (or at least as far as i can see (flashing some leds))
The signiture bytes read O.K.

axos88

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

Okay... Another thing, that maybe has some logic... The "garbage" has a pattern: like this: 0x00 0x00 0x01 0x01 0x02 0x02 0x03 0x03 (etc)

axos88

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

Then just stick to the part that has real code. If that's OK don't look any further. You just don't know how the programmer does the reading or writing, perhaps the buffer used is not cleared or initialised properly past the actual code space. When you can afford a JTAG ICE (will not work with the M8535 but OK with the M16) then have a look at the real flash inside your chip. On with the task of getting the USART working... and the rest of your programming life :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I know how the programmer works, and it should NOT read out any kind of garbage... Especially not after a chip erase! and it worked fine before, no garbage was read out! So there MUST be some kind of problem....

axos88

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

Typo alert:

ldi temp, (1<<URSEL)|(3<<UCSZ0) | (2>>UPM0) 

disables parity. Try instead:

ldi temp, (1<<URSEL)|(3<<UCSZ0) | (2<<UPM0) 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have finally got access to the school's ocilloscope, and did the tests... I didn't know how to test the frequency of the clock, please tell me how I could do that!

I have tested the output on the TX pin, it looks like the picture. The frame should be transmitting a 204 decimal that is 11001100 binary. The weird thing is that the signal is inverted! Where it should be 5V its 0V and vica versa... What Am i doing wrong? There is the output from the max232, (also inverted) I am pretty sure i have hooked the oscilloscope up O.K., and it wasn't inverted, because i tried to test a 9V battery, and was ok...

The frame consists of 1 start bit (logical 1), the 8 data bits (11001100), and 1 stop bit (logical 0 as far as I know), so the frame is 10-bit long, and it should be 1110011000, but it is exactly inverted, its 0001100111

The frame length is exactly 1ms that is 100uS/bit = 10 000 bits/sec, instead of 9600, but even if not 100% precise, it is within margin of error (0,4% per bit, 4% in total (i know it's not quite additive, but eh...))

Please tell me how could I test for the crystal frequency, just to be 100% sure.

Should I connect the crystal, 2x10-22pF, set the fuses, and probe on XTAL2, or XTAL1? or how? (ATMEGA8515)

Thanks, a lot

Attachment(s): 

axos88

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

You are interpreting the images wrong.

For starters, the start bit is logical 0.
Then, there is the 204d which is 11001100 in binary. USART transmits bits out LSB first, so it is 00110011.
And in the end, the stop bit is logical 1.

So 0001100111 in whole. That's exactly what you have captured on your screen. There is nothing wrong or inverted here. And yes the maxim output is also correct.

You should have put a delay every time you send something out. And you might have chosen the data better, like 0x55 which is would be on screen 0101010101 (square wave), or 0xAA (0010101011) which is just the data flipped.

The crystal can be tested just by probing the crystal pins with a 10x probe. 1x probe will propably stop the crystal from oscillating.

Any reasonable scope has frequency measuring capability.

- Jani

EDIT: I forgot to mention that the usart data line idles at 1 too. This is why start bit is 0, and stop bit is 1. This is defined so because in ancien times when dinosaurs still ruled the Earth, the serial communications was a current loop. Logical 1 meant current is flowing, and this is normal. If logical 0 has been detected for too long a time, that is called the "break condition", which means current has stopped flowing because of cable failure or something.

EDIT 2: Yes the fuses need to be set to external oscillator, the crystal pins won't wiggle when internal oscillator is selected.

- Jani

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

For your test "string", set for 8 data bits, no parity, one stop bit, and repeatedly send a 'U' character whenever UDR is empty. The you don't have to worry about which bit is which; the 'scope trace will be alternating high/low, should be all of equal width, and any can be measured to assure the proper bit time.

(or a frequency meter can be used)

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

And I put the scope senzor (well that thing, I don't know how it's called) to XTAL1 / XTAL2? which one?

axos88

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

The scope senzor thingy is called a probe.

Just try both pins, easiest to measure from the crystal.

The other pin is output, and the other is input, the crystal might stop oscillating if you put the probe to the input one, so it would be best to measure from the AVR output pin.

If the next question is which one is input and output from XTAL1 and XTAL2, then read the datasheet.

- Jani

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

X10 setting on the probe has lower capacitance, but the signal might be so small you cant see it

Imagecraft compiler user

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

It's working!! Hurray!!! Yeah, At first, I got everything right, besides the GROUNDING (that's why I read garbage). After that I introduced Parity, but instead of << i wrote >> and thus i didn't enable parity on uC side, that's why I read garbage.... And now I got everything right...
Finally... grrr.. stupid mistakes that make you wonder for half a week

And it works with internal oscillator!

axos88

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

axos88 wrote:
It's working!! Hurray!!!

Great :)

axos88 wrote:
And it works with internal oscillator!

But not necessarily long, or not necessarily in the next AVR you try this on. Just wait until the AVR heats 0.1C, and the RC oscillator will be so off the communication fails :)

- Jani

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

Can't get it to work with external crystal though

I am experimenting with the fuses right now (reading the datasheet)

EDIT: Ok, i got it up and running! Kewl, not errors! Not one single communication error... there were a few with the internal.... What I was doing wrong was that I kept no space between transmissions, and I plugged the serial port in without resetting the device, and probably the PC understood the middle of a character as a start bit... so there was no way it would work. I tried resetting it, and voila! Perfectly works!

Thanks very very very much guys! You have been a lot of help!!

EDIT2: I might have been a little annoying somethimes, sorry 'bout that! :)

axos88

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

Alright, think about all the cool stuff you can do now! It's always nice to hear what people have achieved after the initial stumbling.

- Jani

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

Just to note that now you understand how useful a scope can be you might want to look at http://www.zeitnitz.de/Christian... which is a "poor man's" solution to owning a scope. It may not be any good for high frequency singalling but for low rate signals (like UART traffic) it should work just fine.

Cliff

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

Thanks Cliff, I'll consider making one for myself, but I don't really wanna blow my soundcard, and I don't know much about voltage dividers. Can you give me a link to read about them?

The XTALin signal should be a sine wave? I thought it was a square one, and was surprised when it showed up to be a beutifull sine wave on the scope... One question about scopes: Our school just got one, and even the teacher can't really use it. I should connect the ground of the scope with the ground of the device I am trying to probe, shouldn't I? Because when I connect the two grounds, my beutifull sine wave gets a little distorted, but if I disconnect the two, the distortion disappears... That's wierd in my point of view, but maybe I am missing something again :))

There is one thing to note here... I have no idea how, but the teacher sais that they did not get any probes with the scope... So I used a home-made one... a probe from a multimeter, and a long wire winded around, (for grounding)...

axos88

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

You must always connect the ground of the scope to the ground of the device you are probing and try to connect as close to the active signals as possible. Only then will you get a true picture of the shape of the signal.

As for voltage dividers. If you connect two 10K resistors in series between 0V and Vcc and measure the voltage between them you should find it is 1/2 Vcc - that's the nature of a potential divider. If you made the one connected closest to Vcc 100K and the one connected to Gnd remained 10K you'd find the reading was 1/10th Vcc. The voltage you get at the mid-point is the ratio of Vcc of the two resistors. So if you want to drop 5V to 1V then make one of the resistors 5 times the value of the other. As the two resistors are effectively a direct connection from Vcc to Gnd you want to keep them as high values (like 10K, 47K, 100K) and not small values otherwise there'll be a lot of wasted current flowing through them. (I = V/R after all)

Cliff