Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

My first AVR program

Status
Not open for further replies.

PA3040

Advanced Member level 3
Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Visit site
Activity points
6,936
Dear All,

This is my first AVR Program
Code:
.include <m32def.inc>

Start:		jmp		main

main:		call	sys_init
			call	blink_led

blink_led:	ldi		R16,0b11111111
			out		PORTB,R16
			ldi		R16,0b00000000
			out		PORTB,R16
			jmp		blink_led

			
sys_init:	ldi		R16,0b11111111
			out		DDRB,R16
			ret

This program is build succeeded. then I debug it is using AVR studio-6
1. First it pointed start
2. then jump to main ( call sys_init)
3.Then jumped to sys_init subroutine
4.finally it met ret command
5 then it returned to start instead of call blink_led

Please advice
Thanks in advance
 

You need a delay routine to see the led effect!!!
(**broken link removed**)
"
main:call sys_init
call blink_led
blink_led: ldi R16,0b11111111
out PORTB,R16
call delay
ldi R16,0b00000000
out PORTB,R16
call delay
jmp blink_led
"
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear nagkiller,

Thanks for the reply

Actually my problem is not LED visibility issue
It does not debugging correctly as mention in my post one
Please help
Thanks in advance
 

Hi Bigdog
Wish you the happy new year
I am using ATMEL studio 6 soft debugger ( software debugger )
Please help
Thanks in advance
 

I am using ATMEL studio 6 soft debugger ( software debugger )

Happy New Year to you as well.

So you're simulating your code rather than debugging it on physical hardware.

A hardware debugger, like JTAGICE mkII, JTAGICE3 or AVROne is required to debug code on physical hardware using AVRstudio 6.


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdogguru,
Thanks for the reply

Extremely sorry
Yes Problem is existing with Simulating
Please advice
 

The following is a fairly good tutorial concerning the use of the simulator in AVR studio:

**broken link removed**

How to use AVR Studio for Assembler Programming


Work through the example project provided in the above document, if you still have issues with your code post them in this thread and I'll try and resolve them.


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdogguru,
Please see the code
Code:
.include <m32def.inc>

Start:		jmp		main

main:		call	sys_init
			call	blink_led

blink_led:	ldi		R16,0b11111111
			out		PORTB,R16
			ldi		R16,0b00000000
			out		PORTB,R16
			jmp		blink_led

			
sys_init:	ldi		R16,0b11111111
			out		DDRB,R16
			ret
 

When you simulate your code, are you single stepping through or setting breakpoints?

Are you not seeing any changes at the PORTB level?


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdogguru

Please see the image to see the problem
It return to the same location from call return
avr1.JPG
please advice


avr1.JPG
 
Last edited:

The issue is due to the lack of device initialization code, primarily there is no stack initialization.

When the ret instruction is executed, there is no return point pushed on the stack, hence the code execution simply jumps back to the beginning of the code segment.


Code:
; Conversion times:
; The whole routine requires 228 clock cycles maximum (converting $FF), and 79 clock cycles minimum (converting $00).
; At 4 MHz the times are 56.75 microseconds resp. 17.75 microseconds.
; Definitions:
; Registers
.DEF rmp = R16 ; used as multi-purpose register
; AVR type: Tested for type AT90S8515, only required for stack setting, routines work fine with other AT90S-types also
.NOLIST
.INCLUDE "8515def.inc"
.LIST
; Start of test program
; Just writes a number to R1 and starts the conversion routine, for test purposes only
.CSEG
.ORG $0000
rjmp main
main:
[COLOR="#FF0000"]ldi rmp,HIGH(RAMEND) ; Set the stack
out SPH,rmp
ldi rmp,LOW(RAMEND)
out SPL,rmp[/COLOR]
ldi rmp,$FF ; Convert $FF
mov R1,rmp
rcall fpconv8 ; call the conversion routine
no_end: ; unlimited loop, when done
rjmp no_end
; Conversion routine wrapper, calls the different conversion steps
fpconv8:
rcall fpconv8m ; multiplicate by 502
rcall fpconv8r ; round and divide by 256
rcall fpconv8a ; convert to ASCII string
ldi rmp,'.' ; set decimal char
mov R6,rmp
ret ; all done
; Subroutine multiplication by 502
fpconv8m:
clr R4 ; set the multiplicant to 502
ldi rmp,$01
mov R3,rmp
ldi rmp,$F6
mov R2,rmp
clr R7 ; clear the result
clr R6
clr R5
fpconv8m1:
or R1,R1 ; check if the number is all zeros
brne fpconv8m2 ; still one's, go on convert
ret ; ready, return back
fpconv8m2:
lsr R1 ; shift number to the right (div by 2)
brcc fpconv8m3 ; if the lowest bit was 0, then skip adding
add R5,R2 ; add the number in R6:R5:R4:R3 to the result
adc R6,R3
adc R7,R4
fpconv8m3:
lsl R2 ; multiply R4:R3:R2 by 2
rol R3
rol R4
rjmp fpconv8m1 ; repeat for next bit
; Round the value in R7:R6 with the value in bit 7 of R5
fpconv8r:
clr rmp ; put zero to rmp
lsl R5 ; rotate bit 7 to carry
adc R6,rmp ; add LSB with carry
adc R7,rmp ; add MSB with carry
mov R2,R7 ; copy the value to R2:R1 (divide by 256)
mov R1,R6
ret
; Convert the word in R2:R1 to an ASCII string in R5:R6:R7:R8
fpconv8a:
clr R4 ; Set the decimal divider value to 100
ldi rmp,100
mov R3,rmp
rcall fpconv8d ; get ASCII digit by repeated subtraction
mov R5,rmp ; set hundreds string char
ldi rmp,10 ; Set the decimal divider value to 10
mov R3,rmp
rcall fpconv8d ; get the next ASCII digit
mov R7,rmp ; set tens string char
ldi rmp,'0' ; convert the rest to an ASCII char
add rmp,R1
mov R8,rmp ; set ones string char
ret

Beginners Introduction to the Assembly Language of ATMEL AVR Microprocessors

When programming in C the required device initialization is included in what is commonly referred to as Startup Code, when programming in assembly you are required to complete the device initialization before using the stack and other elements which need to be initialized before use.

Try the following modifications:

Code:
.include <m32def.inc>

.CSEG
.ORG $0000

Start:		ldi 	R16,HIGH(RAMEND) ; Set the stack
		out 	SPH,R16
		ldi 	R16,LOW(RAMEND)
		out 	SPL,R16


main:		rcall	sys_init
		call	blink_led

blink_led:	ldi	R16,0b11111111
		out	PORTB,R16
		ldi	R16,0b00000000
		out	PORTB,R16
		jmp	blink_led

			
sys_init:	ldi	R16,0b11111111
		out	DDRB,R16
		ret


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Bigdog,
Now it is working tanks for help

I am going through the beginners introduction the tutorials that you provided in the above post of you
what does the meaning the paragraph six of page 5 under ( Test it) don't try to program a mega machine to start with
Please advice
 

what does the meaning the paragraph six of page 5 under ( Test it) don't try to program a mega machine to start with

The term mega-machine refers to a large program or application, in other words take small steps when learning a new programming language.

Don't try and write the firmware for that satellite you've been planning to design your first time. :lol:

I also found the following AVR Assembly Language sites:

The AVR Assembler Site

AVR ASSEMBLER TUTOR

AVR-Assembler-Tutorial

AVRbeginners.net!


BigDog
 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear bigdog
It is really nice tutorials

My next step is to study the fuse bit
Before read the tutorial abut fuse bit. can you guide me some from you experience.

can we set fuse bit like pic ( __config 3f39)
Please advice
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top