0

I'm using an Atmega328P micro controller and need a time wasting loop in assembly. I want to be able to waste time for 500ms (or any desired time) but can't find a reasonable way to do so.

I have tried something like:

TimeWastingLoop:
    CPI R19, 0xFF ; compare register with immediate
    JMP MAIN
    NOP NOP NOP   ; Do nothing... 
    INC R19       ; increment r19 (started @ 0x00)
    JMP TimeWastingLoop

I will end up putting A LOT of NOPs so I'm thinking there must be a better way.... Any ideas

fifamaniac04
  • 2,343
  • 12
  • 49
  • 72
  • Better way is to use hardware timer and set the flag in the interrupt (while the main code is to wait the flag). Another way is calculate how much `nop`'s required to perform for defined CPU frequency (nop CPU time consumption should be in the datasheet). Then instead of compare register with 0xFF compare it with calculated value (500 ms / nop-time) – mblw Apr 26 '14 at 17:46
  • 1
    Hm, am I mis-reading that you're comparing and un-conditionally jumping to main right away? Will the NOP's even be executed? (honest question, my atmega assembler was way back) – Joachim Isaksson Apr 26 '14 at 17:47
  • I'll go back and double check the compare but the idea with the compare is that the JMP will only be executed if they are equal... – fifamaniac04 Apr 26 '14 at 17:54
  • I can't use interrupts, weird requirement, I know... – fifamaniac04 Apr 26 '14 at 17:55
  • I _think_ you'll need to use something like `CPI R19, 0xFF / BRBS 1, MAIN / NOP NOP NOP / ...` in that case. – Joachim Isaksson Apr 26 '14 at 18:00
  • 1
    You need 3 nested loops (with empty body), two inner loops are full-scale from 0 to xFF, the outer loop is adjustable to match desired interval 500ms. – Egor Skriptunoff Apr 26 '14 at 19:47

0 Answers0