Write a subroutine that creates a 1ms delay by decrementing the X register in a loop for a processor frequencies of 8MHz, 16MHz & 24MHz. Since the program uses the X register, save it to the stack at the beginning of the routine and restore it from the stack at the end of the routine.
Be sure to download the attached PDF file if you are looking at a more in depth explanation of the math used to calculate the delays
You might want to utilize the stack to preserve your 'X' value when using multiple sub routines. Otherwise your data in each sub routine is basically destroyed when you leave the routine.
Since the 'X' register is a 16 bit register, we could alternatively use just one decrementing loop on one decremented variable.
Be sure to keep in mind that when we jump to a subroutine, the return address is dumped onto the stack. So if we push something onto the stack we need to be sure to retrieve it so that the "program counter" address will be back on top of the stack so that JSR can retrieve the proper value.
********************************************************* * Assignment #3: Time delay loops * * Matthew B. Pare, September 22, 2008, EE2662.001 * ********************************************************* ************************************ * Definitions * ************************************ MHz EQU $0008 ;Define MHz value for Processor DELAY EQU $00FA ;Define Delay For MHz Processor ************************************ * Start (ORIGIN) of Code in Memory * ************************************ ORG $800 ;Origin CLR $3C ;Turn Off Watch Dog Timer ******************************************************** * Time Delay Subroutine with nested Loops 1ms for 8MHz * ******************************************************** TimDel: PSHY ;Push Index Register Y Onto Stack LDY #MHz ;Load Y Register with DELAY LoopX: PSHX ;Push Index Register X Onto Stack LDX #DELAY ;Load X Register with DELAY DecX: DEX ;Decrement X Register BNE DecX ;Branch if DELAY limit has not been reached DEY ;Decrement Y Register BNE LoopX ;Branch if DELAY limit has not been reached PULX ;Pull Index Register X From Stack PULY ;Pull Index Register Y From Stack RTS ;Return from DELAY Subroutine
| Attachment | Size |
|---|---|
| Delay Loop Math Explained.pdf | 14.67 KB |
Comments
Post new comment