Ok... this is n00b. My AVR keeps resetting

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

I think the AVR keeps resetting itself (I have no idea why, I have pulled the reset line high with 8k2)

The LED shows a dim light (because of a high-frequency blink I think)

My code is:

rjmp START

START:
cli
ldi XH, 1
ldi XL, 0xFF
mov r2, XL
mov r3, r2
out DDRB, XL
out PORTB, XL
rjmp WAIT

L2:
//eor r2, r3
clr r2
out PORTB, r2
L3: rjmp WAIT

WAIT:
ldi r18, 0xFF
ldi r19, 0x1
ldi r20, 0x80

WL:
subi r20,1
sbci r19,0
sbci r18,0
nop
nop
....
nop
cpi r19, 0
brne WL
rjmp L2

So, is there anyone who can tell me why is this happening?

axos88

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

Do you have a resistor on the LED? I know its stupid but it happened to me. If the LED draws too much current than it starves the oscillator of power an it resets.

EDIT: Also see if it happens on another AVR.

-Cory

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

Be certain that you don't have the fuses set so as to enable the watchdog timer.

The code doesn't establish a stack pointer as is the norm, but you don't do any subroutine calls or interrupts so it shouldn't matter.

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

I think the first (and most serious) problem with your code is that it seems to be written in assembler. Correct that and I'm sure it'll start working fine =)

Otherwise, I'd tend to agree with Cory or Steve. The code looks fine to me, and simulates fine in AVR Studio (the LED should remain off after the first brief period, which I assume is what you intended).

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

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

Quote:
I think the first (and most serious) problem with your code is that it seems to be written in assembler. Correct that and I'm sure it'll start working fine =)

could not agree more :)

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

LEt's not start the 'c' vs. assembler war again. Maybe the OP only knows assembler. I have been using assembler for 20+ years, and keep in mind 'c' is compiled into assembler. Oh well forget that.

Quote:

WAIT:
ldi r18, 0xFF
ldi r19, 0x1
ldi r20, 0x80

WL:
subi r20,1
sbci r19,0
sbci r18,0
nop
nop
....
nop
cpi r19, 0
brne WL
rjmp L2

The first thing I noticed is that you appear to be mixing decimal with hex.
In the second line you should change the text to
"LDI r19,0x01"

Correct the others as well.

Also,

Quote:

sbci r19,0
sbci r18,0

Essentially you are waiting for the carry flag since the constant you are using is zero.

Quote:

L2:
//eor r2, r3

Remove the //
Quote:

clr r2
out PORTB, r2
[/quote

By doing this you have cleared the r2 register and will always output 0x00 to portb. No matter what anything you have done earlier.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//eor r2, r3

I'm presuming that you are trying to comment this line out. Comments in assembler start with ; not //.

subi r20,1 
sbci r19,0 
sbci r18,0 
nop 
nop 
.... 
nop 
cpi r19, 0 

With this code, r18 is useless since r19 will be 0 before r18 ever changes. What exactly did you intend here?

Regards,
Steve A.

The Board helps those that help themselves.

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

R18 doesn't function in his code, but is there (I presume) as a delay multiplier. If he set r19 to 0xFF and set the cpi instruction to compare on r18 instead of r19, it would square his delay time.

Also, with the eor flag, I assume he's commented it out (improperly, but it'll still get ignored none-the-less) so that the program will always output 0x00 to PORTB intentionally. That way, if the program is working correctly, the LED will stay off permanently. Since it's flickering, he knows it's resetting somewhere.

The comment was meant as a joke, and please definitely don't start up the C vs ASM debate again =)

Clancy _________________ Step 1: RTFM Step 2: RTFF (Forums) Step 3: RTFG (Google) Step 4: Post

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

What is a Negative Zero in Binary?

Imagecraft compiler user

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

Quote:

What is a Negative Zero in Binary?

According to Wikipedia http://en.wikipedia.org/wiki/%E2...(number)
Quote:
Representations

In a 1+7-bit sign-and-magnitude representation for integers, negative zero is represented by the binary value 10000000. In an 8-bit one's complement representation, negative zero is represented by the binary value 11111111. In IEEE 754 floating point numbers, negative zero is represented by the exponent and mantissa being zero and the sign bit being one.
...
In programming languages such as C, C#, C++, and Java, whilst it is possible to obtain negative zero as the result of an expression (for instance as the result of arithmetic underflow on a negative number), negative zero and positive zero compare equal under default (numerical) comparison. Thus a simple comparison is not sufficient to test whether a number is negative zero.


However, my C compiler in the Data Types information says that a 16-bit int uses the high-bit-only-set (0x8000) to be -32768. This is also the value in limits.h . What value does your compiler have for INT_MIN?

If you meant the floating point representation, wouldn't that be high-bit-only-set as well?

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

First of all. Wow. Never got so many answers so quickly. But I later realized that my watchdog timer has been set (I don't know when I did that, but oh well), and that was the cause of the problem.

To answer some of your suggestions:
1. I really did want to wait for the carry, to make more than 256 iterations, this way i could achieve 256*256 = 64k , or 256*256*256 = 4M iterations
2. Regarding the clr r2, that was a temporary debugging thing, i wanted the led to light up for a time , and then shut off, so i knew it was working.

Thanks guys

axos88

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

There are far easier ways to create a time delay loop than all of this.

Rethink your code.

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

jgmdesign wrote:
There are far easier ways to create a time delay loop than all of this.

Rethink your code.

Jim

Sure there are... Interrupts, timer/counter, etc. However this was a short, fast, and simple way to see if everything is O.K. with the uC. (And it was not :) )

axos88

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

So, you are saying that the code workes now that the WDT has been disabled?

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Of course

axos88

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

Quote:

Of course

Well then, good for you!!

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Congrats on getting your beginning program working! Onward to bigger and better applications!

Here are some tips on the resetting for future reference. Random resetting appears occasionally in program development.

Generally there are three reasons why an microcontroller and AVR in particular, resets unexpectedly.

1) Watchdog timer is on and timed out. The AVR has the watchdog off by default, which has saved a lot of grief for students. But there is a fuse that sets it on constantly. Some device programmers set fuses by writing a byte value to the fuse register high or low. These can set the Watchdog_On unintentionally.

2) The stack pointer is not initialized. Most common cause of resetting.

3) Interrupts are on without having the vector address initialized. The reset address is 000 and the reset code usually follows the interrupt vector table. If an interrupt happens and the vector table is not initialized, then the program will see an op code of 000. This is a NOP. The other vectors will probably be NOP also. The program will execute these NOPs until it hits the reset code after the interrupt vectors.

Here's a couple things that can cause resets but they are rare.

4) Sometimes with C the pointers are uninitialized and they point to value 000. This is the reset vector address.

5) The program jumps to a random address where there is no code. The program skips over the 0xff opcode until it rolls over to address 000, which is the reset vector. This is rare.

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

Thanks, Simonetta, this was very useful, even though I knew most of it (all happened to me :)). I don't know what happened to the fuse, how it got on, because I never touched it, maybe while I was experimenting with my faulty USBTiny programmer, it was overwritten by mistake. Now it works okay, but i wonder how lucky I was not to overwrite the Serial Programming fuse :)

I usually write a test program whenever I take a new uC, or make a new programmer, just to make sure they all work okay, so if there is any problem with my bigger code, I don't bang my head to the wall after 3 hours of debugging, just to come up with the Watchdog being set, or something like that. :)

Yeah, now on to my little monitor-and-log-tempreature-and-write-to-SD-card-and-draw-nice-little-graphs-along-the -way project :)

axos88

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

Quote:
Comments in assembler start with ; not //.
Even though I use semicolons for comments (; old habits..) the C style comment // works just as well with assembler 2. One can use C definitions files with the assembler without problems ie I use the TWI state code whic h is part of the C application note with the assembler:
/****************************************************************************
  TWI State codes
****************************************************************************/
// General TWI Master status codes                      
#define TWI_START                  0x08  // START has been transmitted  

etc.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly