Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
wek
PostPosted: Apr 26, 2012 - 09:15 AM
Raving lunatic


Joined: Dec 16, 2005
Posts: 3089
Location: Bratislava, Slovakia

Stepping through the code in a simulator (such as in AVRStudio) may be very instructive, too; although it may take some effort to set it up.

Johan (or others): is there any truly step-by-step guide for this?

JW
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
beautiful_day
PostPosted: Apr 27, 2012 - 02:33 AM
Newbie


Joined: Oct 21, 2011
Posts: 19


Thanks, your reply was very helpful. But I still have some doubts.

1. r26,r27 is X-low/high byte register (so is it true that r24,25 are also low/high byte; r22,r23 - low/high & so on..) [sorry, if it sounds silly but that's what I feel from the code]

2. Couldn't understand the values given to r24...r27. But if I want to fill RAM with 0xAA from 0x21ff-0x200 then is this code correct?

Code:

.section .init0,"ax",@progbits

LDI r27;          CLEAR X HIGH BYTE
LDI r26, 0x21FF;  SET X LOW BYTE TO 0X21FF LOCATION
LDI r25;          CLEAR HIGH BYTE
LDI r24, 0X200;   SET LOW BYTE TO 0X200 LOCATION
LDI r16, 0XAA;    VALUE TO BE STORED

LOOP:
ST X, r16;
SBIW r26,1;    DECREMENT LOCATION BY 1
CP r26,r24;    COMPARE LOW BYTES
CPC r27, r25;  COMPARE HIGH BYTE WITH CARRY
BRNE LOOP;     BRANCH IF r27 & r25 ARE NOT EQUAL


Can I write:
Code:
ST X, r16;
SBIW r26,1;    DECREMENT LOCATION BY 1


as

Code:
ST X-, r16;


3. r16 register is used to set the value 0xAA (just like we use a variable in C), instead we can use r0, r1..or r15. Am I correct?


Thanks once again



JohanEkdahl wrote:
Herte are some cluse:



Last edited by beautiful_day on Apr 27, 2012 - 05:28 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
JohanEkdahl
PostPosted: Apr 27, 2012 - 05:05 AM
10k+ Postman


Joined: Mar 27, 2002
Posts: 18576
Location: Lund, Sweden

Quote:

that's what I feel from the code

You can feel all you like. If you want facts then download the AVR Instruction Set document and use it.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
beautiful_day
PostPosted: Apr 27, 2012 - 05:26 AM
Newbie


Joined: Oct 21, 2011
Posts: 19


Hey, it's not like that (you guys are great, you catch every single word Smile).I downloaded the manual and read it...but I couldn't find or may be couldn't get it into my head correctly, that's why I said that. The manual is still open, am compiling the code and trying to solve errors & warning..if you don't believe..here are some of them after compiling the exact code written above(by me):

Code:
../RamInit.S: Assembler messages:
../RamInit.S:3: Error: `,' required
../RamInit.S:3: Error: garbage at end of line
../RamInit.S:4: Warning: constant out of 8-bit range: 8703
../RamInit.S:5: Error: `,' required
../RamInit.S:5: Error: garbage at end of line
../RamInit.S:6: Warning: constant out of 8-bit range: 512
 
 View user's profile Send private message  
Reply with quote Back to top
beautiful_day
PostPosted: Apr 27, 2012 - 06:07 AM
Newbie


Joined: Oct 21, 2011
Posts: 19


At least I got rid of the messages with this:

Code:
.section .init0,"ax",@progbits

clr r27;          CLEAR X HIGH BYTE
LDS r26, 0x21FF;  SET X LOW BYTE TO 0X21FF LOCATION
clr r25;          CLEAR HIGH BYTE
LDS r24, 0x200;   SET LOW BYTE TO 0X200 LOCATION
LDI r16, 0XAA;    VALUE TO BE STORED
 
 View user's profile Send private message  
Reply with quote Back to top
beautiful_day
PostPosted: Apr 27, 2012 - 08:22 AM
Newbie


Joined: Oct 21, 2011
Posts: 19


Hello,

I am debugging the program and watching the memory & registers. The program does fill 0xAA but at the wrong places (from 78-60). I don't understand why? The start & end address that I've given is 0x200-0x21FF respectively.

Somebody please point out my mistake...
 
 View user's profile Send private message  
Reply with quote Back to top
js
PostPosted: Apr 27, 2012 - 08:35 AM
10k+ Postman


Joined: Mar 28, 2001
Posts: 20387
Location: Sydney, Australia (Gum trees, Koalas and Kangaroos, No Edelweiss)

Code:
LDS r26, 0x21FF;  SET X LOW BYTE TO 0X21FF LOCATION
You CANNOT put a 16 bit value into a 8 bit register, that is loading r26 FROM address 0x21FF
Code:
LDS r24, 0x200;   SET LOW BYTE TO 0X200 LOCATION
same thing here

_________________
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
SprinterSB
PostPosted: Apr 27, 2012 - 08:43 AM
Posting Freak


Joined: Dec 21, 2006
Posts: 1483
Location: Saar-Lor-Lux

You can use the code from here:
http://www.rn-wissen.de/index.php/Speic ... -Verbrauch

Code:
#include <avr/io.h> // RAMEND

#define MASK 0xaa

// !!! Never call this function, it is part of .init-Code
void __attribute__ ((naked, used, section(".init3"))) init_mem (void);
void init_mem (void)
{
   //  Use inline assembler so it works even with optimization turned off
   __asm volatile (
      "ldi r30, lo8 (__heap_start)"  "\n\t"
      "ldi r31, hi8 (__heap_start)"  "\n\t"
      "ldi r24, %0"                  "\n\t"
      "ldi r25, hi8 (%1)"            "\n"
      "0:"                           "\n\t"
      "st  Z+,  r24"                 "\n\t"
      "cpi r30, lo8 (%1)"            "\n\t"
      "cpc r31, r25"                 "\n\t"
      "brlo 0b"
         :
         : "n" (MASK), "n" (RAMEND+1)
   );
}
If you want a different start, use a different start address. The example above uses __heap_start which starts after static data.

For the start of RAM, you can use __data_start which is defined in the linker script, just like __heap_start.

I'd discourage to write this in C for several obvious reasons like, e.g. not overriding the return address of the worker.
 
 View user's profile Send private message Visit poster's website 
Reply with quote Back to top
beautiful_day
PostPosted: Apr 27, 2012 - 11:18 PM
Newbie


Joined: Oct 21, 2011
Posts: 19


js wrote:
Code:
LDS r26, 0x21FF;  SET X LOW BYTE TO 0X21FF LOCATION
You CANNOT put a 16 bit value into a 8 bit register, that is loading r26 FROM address 0x21FF
Code:
LDS r24, 0x200;   SET LOW BYTE TO 0X200 LOCATION
same thing here


Thanks so much, it was very helpful (also understood why the values for R24...R27 were like that). Got the desired output.

Thanks everybody!
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits