Arduino time delay to Pic

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,593
I am currently converting a routine that is written for Arduino to Picmicro'
Can anyone tell me what time is represented in the following couple of delays.
IOW, the delay time they represent.?
Can it be assumed that the Arduino runs at a typical clk rate, 16Mhz?
I don't see any indication in the pgm.
I can produce an equivalent PIC TD time using the golovchenko site tool.

Delays in question:
delay_us:
LDI R21, 55
l5: DEC R21
BRNE l5
RET
;---------------------------------------------------------------
delay_ms:

LDI R21, 255
l7 : LDI R22, 255
l8 : LDI R23, 20
l9 : DEC R23
BRNE l9
DEC R22
BRNE l8
DEC R21


Edit: I believe the above was written for Arduino Nano which indicates 16Mhz clk.
 
Last edited:

Jony130

Joined Feb 17, 2009
5,594
delay_us:
LDI R21, 55 (1 cycle)
l5: DEC R21 (1 cycle)
BRNE l5 (2 cycle)
RET ( 4 cycle)
This code will take 165 cycles (3 * 55) plus 4 cycles for RET instruction.
So the delay will be around 10μs.

delay_ms:

LDI R21, 255 (1 cycle)
l7 : LDI R22, 255 (1 cycle)
l8 : LDI R23, 20 (1 cycle)
l9 : DEC R23 (1 cycle)
BRNE l9 (2 cycle)
DEC R22 (1 cycle)
BRNE l8 (1 cycle)
DEC R21 (1 cycle)
Are you sure you pasted the entire code?
In this case, will have 3 cycles * 20 = 60 cycles + (60 +3) * 255 = 16 065 + 2 = 16 067
And this gives 1ms of dealy.
 

Ian0

Joined Aug 7, 2020
13,112
Once cycle for the decrement, two cycles for the branch, so 3 cycles at 62.5ns repeated 55 times is 10.3us.
Add 1 cycle for the load and 4 cycles for the RET (plus another 3 cycles for the CALL that calls the subroutine) and you get 10.8us
They are all listed here.
Should there be another line in delay_ms that says BRNE l7 ?
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,593
Good catch, here is corrected.

delay_ms:
LDI R21, 255
l7 :LDI R22, 255
l8 :LDI R23, 20
l9 DEC R23
BRNE l9
DEC R22
BRNE l8
DEC R21
BRNE l7
RET
 

Jony130

Joined Feb 17, 2009
5,594
Well, now it will be a long delay. About 16067 * 255 = 4 097 085 (256ms).
Are you sure you need this in your program?
 

Jony130

Joined Feb 17, 2009
5,594
I looked in the assembly code and you do not have to worry too much about the delay.
The first delay ( delay_us: = 10μs) is used to generate the CLK signal for TM1637.
And the second one ( delay_ms: = 250ms ) is used in the "main" loop in the "demo" program. So you don't even have to use this delay (delay_ms) in your program.
Or you can change it to the value you want.
 
Last edited:

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,593
I looked in the assembly code and you do not have to worry too much about the delay.
The first delay ( delay_us: = 10μs) is used to genera the CLK signal for TM1637.
And the second one ( delay_ms: = 250ms ) is used in the "main" loop in the "demo" program. So you don't even have to use this delay (delay_ms) in your program.
Or you can chenge it to the value you want.
Thanks !
I was starting to come to the same conclusion, thanks for verifying it. :cool:
 
Top