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"
Ignacio Vazquez-Abrams
17.7k1 gold badge28 silver badges32 bronze badges
asked May 24, 2014 at 10:42
user3669884user3669884
-
1This is a pure programming question and probably should have remained on stackoverflow, rather than been migrated here.Chris Stratton– Chris Stratton2014年05月28日 03:19:04 +00:00Commented May 28, 2014 at 3:19
-
its atmega328 so 16mhz but how many clock cycles would that be 16000 for 1 millisecond?user1653– user16532014年05月30日 14:21:52 +00:00Commented May 30, 2014 at 14:21
1 Answer 1
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);
answered May 27, 2014 at 20:27