New To AVR ASM, Trying to get Blinking LED working

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

I'm taking my first steps in to avr asm and I am stuck on my first endeavor.
I'm using a mega8, and trying to get PD4 to blink an LED (nothing fancy, using the internal oscillator).

If I remove my rcall delay calls from my loop:, the LED lights up fine, however, with the delays in place, the LED does not light up. The weird part is, if I swap the sbi and cbi calls in loop: the LED will light up, but not blink. I suspect this is due to a failure inside the first rcall delay call. But I can't seem to find where. I have tried to follow the AVR ASM tutorials that are available online, but clearly I am missing something (probably very simple).

Any advice would be greatly appreciated. Thank you.

.include "m8def.inc" 

.def delay1	= r17
.def delay2	= r18
.def delayv	= r19
.equ led	= 4 ; PORTD bit number to blink LED on

	rjmp main

delay:
	clr	delay1
	clr	delay2
	ldi	delayv, 100
      
delay_loop: 
	dec	delay2		
	brne	delay_loop 	
	dec	delay1		
	brne	delay_loop 	
	dec	delayv		
	brne	delay_loop 	
	ret     		; go back to where we came from


main:
	sbi	DDRD, led	; connect PORTD pin 4 to LED

loop:
	cbi	PORTD, led	; turn PD4 high
	rcall	delay		; delay for an short bit
	sbi	PORTD, led	; turn PD4 low 
	rcall	delay		; delay again for a short bit
	rjmp	loop		; recurse back to the head of loop

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

You haven't initialised the stack pointer.

Leon Heller G1HSM

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

.include "m8def.inc"

.dseg
.def temp = r16
.def delay1 = r17
.def delay2 = r18
.def delayv = r19
.equ led = 4 ; PORTD bit number to blink LED on

.cseg
.org $000
rjmp main

delay:
clr delay1
clr delay2
ldi delayv, 100

delay_loop:
dec delay2
brne delay_loop
dec delay1
brne delay_loop
dec delayv
brne delay_loop
ret ; go back to where we came from

main:
ldi temp,low(RAMEND)
out SPL,temp
ldi temp,high(RAMEND)
out SPH,temp

sbi DDRD, led ; connect PORTD pin 4 to LED

loop:
cbi PORTD, led ; turn PD4 high
rcall delay ; delay for an short bit
sbi PORTD, led ; turn PD4 low
rcall delay ; delay again for a short bit
rjmp loop ; recurse back to the head of loop

This should work fine, if not try this delay routine which is tested:

;*************Delay(1sec) Module***********************
delay_1sec:
push delay1
push delay2
push delayv

ldi temp,$ff
mov delay1,temp

ldi temp,$13
mov delay2,temp

ldi temp,$06
mov delayv,temp
de:
dec delay1
brne de

dec delay2
brne de

dec delayv
brne de

pop delay1
pop delay2
pop delayv
ret

ATMEL--Heart Beat
Nothing Impossible

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

See the Getting Started page at www.avrbeginners.net . The second page of the section has a delay-loop LED flasher. Even written for a Mega8. :)

http://www.avrbeginners.net/gett...

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

Awesome! I got this working, first with the example from http://www.avrbeginners.net/, but then I wanted to know why my original code was failing (since I compiled it from attiny tutorials I found online). I found that the code posted by srinivasandelta worked. I peeled away everything in his code until I was back to my original code, and the missing piece was:

ldi temp,low(RAMEND) 
out SPL,temp 
ldi temp,high(RAMEND) 
out SPH,temp 

I need to read up on what these lines do, but y'all got me going, thank you both for your help, I'm loving the AVR!

cheers,
-kirk

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

From http://www.avr-asm-tutorial.net/avr_en/TEST2.html:

Quote:
These commands initiate the stack in the build in SRAM. Stack operations
are always necessary when subroutines or interrupts are called.
By calling the subroutine or interrupt handling routine the actual adress
is written to the stack in order to later jump back to the code where the
interrupt or call occurred.

My RCALL, while the SRAM is not initialized would be quite a problem indeed!

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

Quote:
I need to read up on what these lines do
The answer to that was in the first reply by Leon:
Quote:
You haven't initialised the stack pointer.

Regards,
Steve A.

The Board helps those that help themselves.

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

I saw Leon's reply, just wasnt sure how/what I needed to initialize it. I haven't worked in ASM since 6802 days on my 8-bit Atari, and I'm coming to AVR ASM from the arduino, so i'm an avr asm n00b thanks again everyone for your help & patience.

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

Assembler on AVR's are very similar in style to the old 6502 (commodore 64) Much better though.

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

I think its pretty cool that each generation of engineers has new enthusiasts that dig the zen of assembler and learn all the low level fundamentals, and each generation of guitar players has enthusiasts that learn the fundamentals to carry on the tradition for another generation. I suppose there are certain skills that have been lost over the decades... designing geartrains for cash registers, making buggy whips etc.

Imagecraft compiler user

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

"designing geartrains for cash registers, making buggy whips etc."

and... Pot belly stoves, trains, planes, and automobiles, yep, those were the days when America was alive and well. I hearken for those days again although I did just buy CodeVision yesterday so I will be coming into the future soon I hope but I still think a very good foundation in assembler as I am sure you have Bob, helps you with a myriad of solutions to various problems especially when it comes to the how and why a C compiler does what it does or when it does what you think it should not do.

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

Quote:

buggy whips

I guess "buggy" in the US means something different to "buggy" in the UK or else "buggy whip" conjures up visions of all kind of child abusing masochism!

A UK "buggy":

(i.e. "buggy" == "child's pushchair" and "buggy whip" == "who knows!!")

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

Ye olde American buggy whip

as opposed to buggy code, a universal phenomenon

Attachment(s): 

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

Last Edited: Fri. Jun 4, 2010 - 11:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
wasnt sure how/what I needed to initialize it. I haven't worked in ASM since 6802
For the 6802 you do:
        LDS     #STACK

:) as it can do the 16 bit stack pointer in 1 go.

For the AVR you need to load low and high stack 8 bit registers.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

clawson wrote:

(i.e. "buggy" == "child's pushchair" and "buggy whip" == "who knows!!")

I thought the Brit term for the kid-barrow was a "Pram" ???

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

And what part of the buggy do we whip to make it go faster?

Imagecraft compiler user

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

bobgardner wrote:
And what part of the buggy do we whip to make it go faster?

Depends on the horse in question. Some horses require a snap of the whip on the side of the buggy and others require a quick snap to their behind. In that case you might call it a horse whip but times being what they are today that is just not politically correct.

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

leon_heller wrote:
You haven't initialised the stack pointer.

Thank you very much for this answer friend !! Because i had the same problem, i have been downloading program to atmega32 but it haven't been working since morning :( Thanks God i saw your answer and set-up stack, then it works finally!!

Best regards!!