Creating a delay in Arduino Uno using assembly language without using a timer

I was just starting to study microcontrollers, and I could not understand how we can introduce delays in the code without using timers. My board has a 16MHZ watch. Let's say I want to enter a 5 ms delay before checking if a button is pressed. How can I determine how many instructions I need to follow to get a 5 ms delay and how will I program it? Is there a standardized way to do this? This seems like a standard thing, but I can't figure out how to do it.

I program using assembly language on the Atmega 328 Arduino uno.

+1
source share
1 answer

Typically, you figure out how many time cycles you need to write, and then write a cycle. Consult your spreadsheet to determine how many cycles your cycle takes and calculate how many iterations you need.

       ldi r16, x ; 1 cycle
loop:  nop        ; 1 cycle
       dec r16    ; 1 cycle
       brne loop1 ; 2 cycles when jumping, 1 otherwise

Depending on the value x, this cycle will take cycles x * 4. With a 16 megahertz board 1 ms, 16,000 cycles, so 5 ms will be 80,000 cycles. What is larger than this 8-bit loop can be controlled, so we need to make a 16-bit counter.

       ldi r16, x ; 1 cycle
       ldi r17, y ; 1 cycle
loop:  nop        ; 1 cycle
       dec r16    ; 1 cycle
       brne skip  ; 2 cycles when jumping, 1 otherwise
       dec r17    ; 1 cycle
skip:  brne loop  ; 2 cycles when jumping, 1 otherwise

, 6 . , 6 , , r16 . 2 , brne 1 , 1 . , 79999 , 13333 . , x=low(13333)=21 y=high(13333)=52 nop.

, , . , . , .

+6

Source: https://habr.com/ru/post/1543675/


All Articles