2

Hi i'm new to AVR assembly language so i was trying to get delay function to create 1 ms , 100us, and 1us delays to do that i need to figure out what to replace nop's with below here (mainboard arduino uno r3 ATmega328 Thank you.

"delay_ms%=:    nop                 ; code to replace  nop  \n"
"delay_100us%=: nop                 ; code to replace  nop  \n"
"delay_1us%=:   nop                 ; code to replace nop  \n"
"               sbiw r30,1          ; decrement ms count (r31:r30)\n"
"               brne delay_ms%=     ; loop to delay_ms while > 0  \n"
"               ret                 ; return from subroutine      \n"

The rest is below.

word millisecs;   
Serial.begin(9600); 
}   
void setup() asm volatile(
"          ldi r16,0x3F    ; r16 = 00111111\n"
"          out 4,r16       ; set pins 8-13 as outputs in DDRB\n" 
::: "r16");
millisecs = 1000;            // 1s blink delay
Serial.begin(9600);
}
void loop() 
{
long starttime = millis();        // make a note of the start time

asm volatile(
// jump to "blink" - ie jump around the delay_ms subroutine
"            rjmp  blink%=        ; relative jump to 'blink' \n"
"          ldi r16,0x3F    ; r16 = 00111111\n"
"          out 4,r16       ; set pins 8-13 as outputs in DDRB\n"
::: "r16");

registers used:
r31 - millisecond count (lo byte)
r30 - millisecond count (hi byte)
r17 - 100 microsecond count
r16 - 1 microsecond count
Overall delay (ms) = r30:r31 * r17 * r16
---------------------------------------------------------------------*/
"delay_ms%=:    nop                 ; code to replace  nop  \n"
"delay_100us%=: nop                 ; code to replace  nop  \n"
"delay_1us%=:   nop                 ; code to replace nop  \n"
"               sbiw r30,1          ; decrement ms count (r31:r30)\n"
"               brne delay_ms%=     ; loop to delay_ms while > 0  \n"
"               ret                 ; return from subroutine      \n"

migrated from stackoverflow.com May 27 '14 at 15:19

This question came from our site for professional and enthusiast programmers.

  • 1
    This is a pure programming question and probably should have remained on stackoverflow, rather than been migrated here. – Chris Stratton May 28 '14 at 3:19
  • its atmega328 so 16mhz but how many clock cycles would that be 16000 for 1 millisecond? – user1653 May 30 '14 at 14:21
3

AVR LibC already has macros that do this, provided F_CPU is set correctly.

// Note: The Arduino IDE gets the value for F_CPU from boards.txt
#define F_CPU xxxxxxxxxx

#include <util/delay.h>

 ...

_delay_ms(1);
_delay_us(100);
_delay_us(1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy