ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 3114
Скачиваний: 0
122 9 HC11 Development Board
Here is the list file generated by ASHC11 for this fragment:
=0000 |
1 |
DATA |
|||
0100 |
=0100 |
2 |
ORG |
$100 |
|
0100 |
3 |
VAR1 |
DS |
1 |
|
0101 |
4 |
BUF |
DS |
10 |
|
010b |
6 |
VAR2 |
DS |
1 |
For the variable VAR1, one byte is reserved, at the current address in the DATA section. The variable BUF1 takes the next 10 bytes between the addresses $101 and $10A, and VAR2 will take the memory location at the address $010B.
A similar process occurs for the CODE section. The assembler automatically determines how many bytes of code each instruction need, and increments the pointer accordingly.
Step 3. Creating Symbolic Names for Resources
The EQU (EQUation) directive associates a numeric value to a symbolic name like in this example:
REGBASE |
EQU |
$1000 |
SCDR |
EQU |
REGBASE+$002F |
In this mode, all the resources of the microcontroller can have symbolic names assigned. These definitions can be saved in distinct files that can be invoked later, using the INCLUDE directive. The accompanying CD includes several definition files for the HC11 microcontrollers. They are named 68HC11E9.DEF, 68HC11F1.DEF, 68HC11KA.DEF.
STEP 4. USING MACROS
A MACRO is a block of code delimited by the directives MACRO and ENDM, associated with the label NAME:
NAME |
MACRO <param>, <param> |
.... |
|
ENDM |
When the assembler encounters a previously defined MACRO name in the source file, it automatically inserts the whole block of code associated with that MACRO name. The examples below are intended to illustrate the utility of this feature:
PSHD |
MACRO |
PULD |
MACRO |
|
PSHA |
PULB |
|||
PSHB |
PULA |
|||
ENDM |
ENDM |
|||
9.4 Description of the Software Components |
123 |
The next MACROs create the possibility of conditional program jumps anywhere in the 64 K addressable space.
JEQ |
MACRO |
?dest |
JNE |
MACRO |
?dest |
|
LOCAL |
@A |
LOCAL |
@A |
|||
BNE |
@A |
BEQ |
@A |
|||
JMP |
?dest |
JMP |
?dest |
|||
@A |
EQU |
* |
@A |
EQU |
* |
|
ENDM |
ENDM |
|||||
Please note the following distinctive features of these MACROs:
1.MACROs allow the use of parameters, in this case the destination address of the jump.
2.Local labels are allowed. This avoids “duplicate symbol” errors, when the macro is used more than once in a software module. Note that not all assemblers allow local labels in MACROs.
3.The line @A EQU *
indicates that the symbolic label @A receives the current value of the pointer of the CODE section.
An interesting application of this feature is shown in the following example:
VECTOR_SCI MACRO
LOCAL @SCI_SVC CODE
@SCI_SVC SET * ORG $FFD6
DW @SCI_SVC
ORG @SCI_SVC ENDM
This MACRO saves the current address in the CODE section in the variable @SCI_SVC, then stores this value to the addresses $FFD6-$FFD7, which contain the interrupt vector for the SCI system. After this operation the pointer of the section is restored to the value saved in the variable @SCI_SVC. Similar MACROs define the other interrupt vectors. See the file AS11.MAC on the accompanying CD, for the whole set of MACROs used in this book.
Step 5. Defining a General Structure for Software Applications
Consider the example found in the file STEP5.ASM:
124 |
9 HC11 Development Board |
|||
TITLE |
MAIN MODULE |
|||
INCLUDE |
68HC11F1.DEF |
;definitions |
||
INCLUDE |
AS11.MAC |
;macro definitions |
||
INCLUDE |
MAP.ASM |
;memory map |
||
CODE |
||||
VECTOR_RESET |
||||
RESET |
EQU |
* |
||
INCLUDE |
INIT.ASM |
;initialization |
||
MLOOP |
EQU |
* |
;start of main loop |
|
INCLUDE |
TIMER.ASM |
;timer routines |
||
INCLUDE |
SCI.ASM |
|||
*............................
*other user modules come here
JMP |
MLOOP |
;the program is an |
;infinite loop |
INCLUDE GENLIB.ASM
;general purpose ;routines
INCLUDE TABLES.ASM ;ROM tables END
The first line instructs the assembler to include in the source file the file 68HC11F1.DEF, which contains symbolic definitions for the resources of the microcontroller.
The second line invokes the library of MACRO definitions AS11.MAC.
The file MAP.ASM, invoked in the third line, contains the origins of the DATA and CODE sections. It is advisable to define here all the variables used by the program. Here is an example of structure for MAP.ASM.
TITLE |
Memory map & global variables |
||
DATA |
|||
ORG |
RAMBASE |
;RAMBASE is defined in |
|
;68HC11F1.DEF |
|||
VAR1 |
DS |
1 |
;some variables |
VAR2 |
DS |
1 |
|
VAR3 |
DS |
1 |
|
CODE |
|||
ORG |
ROMBASE |
;ROMBASE is defined in |
|
;68HC11F1.DEF |
|||
END |
|||
Note that no actual executable code has been generated so far. The purpose of all these include files is to simplify and to organize the dialog with the assembler.
The MACRO named VECTOR_RESET defines the entry point in the main program after RESET. The first piece of code executed after RESET is the initialization
9.4 Description of the Software Components |
125 |
sequence for registers associated with the hardware subsystems and for RAM variables.
The example below presents some typical initializations: the stack pointer, data direction register for an I/O port, and some variables:
TITLE |
INITIALIZATION SEQUENCE |
||
LDS |
#RAMEND |
;Init SP |
|
LDAA |
#$05 |
;Enable CSPROG |
|
STAA |
CSCTL |
;for a |
32 k memory |
LDAA |
#$3C |
||
STAA |
DDRD |
;PORTD |
2-5 for output |
CLR |
VAR1 |
;other |
initializations |
*.......
END
An important detail on this initialization sequence concerns the ability of 68HC11F1 to generate four selection signals, CSPROG, CSGEN, CSIO1, CSIO2, for external memory or I/O devices
These signals are software controlled by means of four special registers: CSCTL, CSGADR, CSGSIZ and CSSTRH.
The CSPROG line that selects the external program memory is controlled by the CSCTL register. The structure of CSCTL register is as follows:
CSCTL |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
IO1EN |
IO1PL |
IO2EN |
IO2PL |
GCSPR |
PCSEN |
PSIZA |
PSIZB |
|
RESET |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
•IO1EN – Enable CSIO1.
When set to 1, CSIO1 is enabled. When cleared to 0, CSIO1 is disabled and the associated pin, PORTG bit 5, is usable as a general-purpose I/O pin.
•IO1PL – Controls the polarity of CSIO1. When this bit is 0, CSIO1 is active LOW; when set to 1, CSIO1 is active HIGH.
IO2EN and IO2PL act similarly on CSIO2.
•GCSPR – General-Purpose Chip Select Priority
GCSPR =0 CSPROG has priority compared to CSGEN GCSPR = 1 CSGEN has priority compared to CSPROG
•PCSEN – Program Chip Select Enable
When the microcontroller operates in expanded mode (i.e. with external bus) this bit is automatically set out of RESET.
PSIZA and PSIZB. These bits relate the selection signal with the size of the external memory, as shown in Table 9.1.
This explains why, in the initialization sequence, CSCTL is written with $05. This means that an external 32 K ROM is used, while the other three chip selects are disabled and the pins of PORTG are usable as general-purpose I/O pins.
126 9 HC11 Development Board
Table 9.1. The effect of programming the bits [PSIZA:PSIZB] in CSCTL
PSIZA |
PSIZB |
Size (Kbytes) |
Address range |
0 |
0 |
64 |
$0000–$FFFF |
0 |
1 |
32 |
$8000–$FFFF |
1 |
0 |
16 |
$C000–$FFFF |
1 |
1 |
8 |
$E000–$FFFF |
After the initialization sequence, the program enters an infinite loop. In the example presented (STEP5.ASM) the program loop includes the modules TIMER.ASM and SCI.ASM, while GENLIB.ASM and TABLES.ASM remain outside the loop. See the examples in the next paragraph for a better understanding on how to organize your software.
Step 6. Creating Reusable Software Modules
This paragraph presents a detailed solution to create a set of software timers. A software timer is basically a predefined variable that can be initialized by the program. Once initialized these predefined “timers” are automatically decremented at precise time intervals. Thus, the timer will reach zero after a time T = N × Kt , where N is the initialization value and Kt is the time quantum between two successive decrements. The time quantum is generated using the TOC2 (Timer Output Compare 2) interrupt, which is generated every 4 milliseconds. Based on this interrupt, precise 100 ms, 1-second, and 1-minute intervals are generated.
When a time quantum expires, the associated set of variables (software timers) is scanned, and if their value is greater than zero, they are decremented.
The software timers are defined in MAP.ASM, which becomes:
TITLE Memory map & global variables
DATA |
|||
ORG |
RAMBASE |
||
TIRQ |
DS |
1 |
;flag set by TOC2 ISR |
T4MS0 |
DS |
1 |
;4 ms timers |
T4MS1 |
DS |
1 |
|
T4MS2 |
DS |
1 |
|
T4MS3 |
DS |
1 |
|
T4MS4 |
DS |
1 |
|
T4MS5 |
DS |
1 |
|
T4MS6 |
DS |
1 |
|
T4MS7 |
DS |
1 |
|
T100MS0 |
DS |
1 |
;100 ms timers |
T100MS1 |
DS |
1 |
|
T100MS2 |
DS |
1 |
|
T100MS3 |
DS |
1 |
|
T100MS4 |
DS |
1 |