Hardware Reference
In-Depth Information
Step 3
Wait for 2 ms.
Step 4
Pull the PT5 pin to low.
Step 5
Wait for 2 ms.
Step 6
lpcnt1
lpcnt1 2 1;
Step 7
If (lpcnt1 ? 0), go to step 2; else, continue to the next step.
After the second iteration, the only thing left is how to generate the 2-ms and 1-ms time
delays. These time delays can be generated by calling the existing delay subroutines.
The subroutine is the mechanism that allows the same sequence of code to be reused
(called) from many places of the program. This is the start of the concept of software reuse .
In addition, the subroutines created for certain programs can also be called (reused) in other
programs. To make subroutines more reusable, the following principles must be followed:
Avoid using global memory to hold variables that are supposed to be local to the
subroutine. Stack is the best place for holding local variables.
Save CPU registers used in the subroutine unless they are used to pass incoming
parameters or to return computation results. By doing this, the caller does not need
to be concerned about what registers to save before calling the subroutine.
Describe clearly how to pass incoming parameters and how to return results using
appropriate comments.
The HCS12 provides instructions bsr , jsr , and call for making subroutine calls. The
instructions bsr and jsr will save the return address in the stack before jumping to the subrou-
tine. The call instruction will save the contents of the PPAGE register in the stack in addition
to the return address. The call instruction is provided to call subroutines located in expanded
memory. All subroutines should have rts (rtc for subroutines in expanded memory) as the last
instruction. The rts instruction will pop the return address onto the PC register from the stack,
and program control will be returned to the point that called the subroutine. The rtc instruction
should be used by subroutines that are in expanded memory. The rtc instruction will restore
the PPAGE value pushed onto the stack in addition to the return address.
Parameters can be passed in registers, program memory, the stack, or the global memory.
The result computed by the subroutine can be returned in CPU registers, the stack, or the
global memory. Local variables must be allocated in the stack so that they are not accessible to
the caller and other program units. Local variables come into being only when the subroutine
is being executed. The HCS12 provides instructions to facilitate the access to variables in the
stack. The leas instruction is most effective for local variable allocation and deallocation.
The D-Bug12 monitor provides many functions to support I/O programming on demo
boards that include the D-Bug12 monitor. The printf subroutine is a very useful subroutine be-
cause it provides complicated output data formatting.
The HCS12 has many signal pins. These signal pins are divided into I/O ports. An I/O port
consists of a set of signal pins and the registers required for the I/O operation. Since an I/O pin
can be used for input and output, the user must configure the direction of the I/O pin before
using it in data transfer. This is achieved by writing an appropriate value into a data direction
register. Most signal pins serve multiple functions. A signal pin can be used for general-purpose
I/O when it is not being used by any peripheral module.
 
Search WWH ::




Custom Search