Can someone help with a code snipet for a fixed delay subroutine using DR1 in memory 20h using a 4Mz crystal
Lots of specifications missing.
You haven't told what exact chip you have, so we don't know if it is a 12-clocker or maybe a 6-clocker, a 4-clocker or a 1-clocker.
You haven't specified what unit you want the delay to use - so should each delay "tick" be one millisecond? Or maybe 1 second? Or what?
You haven't specified if the processor may make use of any nice hardware so it can perform the delay while taking into account time lost in interrupt routines or if it should be just a dumb busy-loop counting machine ticks and hoping that the core doesn't get any interrupts to process during the delay.
Are you sure that you have zero own ideas how to implement a delay? Have you made any attempts yourself? Wouldn't you learn more, if you do spend own time on the task?
Wouldn't a single instruction provide a "fixed" delay, even a NOP
Machine instructions are excellent for fixed delays. Especially short delays where the requirement is "must wait at least x ns/us" - such as when pacing some I/O with a slower external device.
When the delay gets long, it's often not so fun with a busy-loop since the processor can't do anything else. Letting a timer do the delay (maybe not the full delay span but at least suitably long sub-delays of maybe 1ms or 10ms) means the "main loop" can continue to update blinking LED concurrently with looking for key presses etc.
If doing longer busy-loops, it's way better to not count machine cycles but looking at a timer or similar - then it doesn't matter if the processor has 20% CPU load in ISR during the delay. The delay loop will "auto-compensate" for machine cycles spent in the ISR. Often other peripherial hardware can also be used to measure time - such as an unused SPI/UART/xx where the baudrate gives a fixed time until the data has been clocked out and the "I'm hungry" bit gets set again.
Madame Westermark,
You are one very smart lady.