Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming Microcontrollers in C, 2-nd edit (Ted Van Sickle, 2001).pdf
Скачиваний:
306
Добавлен:
12.08.2013
Размер:
7.2 Mб
Скачать

386 Chapter 7 Advanced Topics

You will notice that the file shown in Listing 7-17 is the only file in which the header file hc12.h is included. None of the other code depends in any way on the bit field structures that control all of the peripherals on the chip. There are a few places throughout the code where there are some specific differences between the two programs. The first line of code in the header phone.h is

#define DOS

This line is left intact whenever the program is compiled to run under DOS. When the program is compiled to run on the HC12, this line is removed. Then sequences like the following found in monitor.c

#ifndef DOS

inituart(); /* initializethe uart to 9600 b/s */

#endif

will cause the execution of inituart() to be ignored when compiled for DOS but it will be included when the program is compiled for the HC12. Such inclusions will be found throughout the various functions that are linked to form the program.

Putting It All Together

The above programs were all compiled, linked and tested with the DOS-based compiler provided by MIX Software1. This reliable compiler created satisfactory test code to run on any PC-style computer. When everything was working as desired, the code was moved to the HC12 system. The compiler used in this case was the COSMIC compiler2. This compiler was provided by the same company that provided the compilers used in Chapters 5 and 6. Cosmic provides two software packages that are very useful. The first is called IDEA12. This package is a so-called Integrated Development Environment, IDE. Within the IDE, you can specify things like the default directory, and provide a list of files for a make utility. The make utility will compile all source files that are newer than the corresponding object files. Therefore, as you debug various files in

1Mix Software, 1132 Commerce Drive, Richardson, TX 75081, (972) 783-6001.

2Cosmic Software, 400 W. Cummings Park, Ste. 6000, Woburn, MA 01801-6512, (781) 932-2556 x15.

Putting It All Together 387

your program, you can automatically compile only those files modified since they were last compiled.

There are a couple of additional files needed to complete the program for the phone book. The first is the interrupt vector table. The vector table is stored in nonvolatile memory on this system, and as such, it is best to write a C program that creates this table. This little program is compiled and linked just like any other module in the program. It will be linked as a constant section at the address 0xFFCE. The interrupt vector table program is shown in Listing 7­ 18. There is only one external module to be linked to this particular table. It is _stext(). The function prototype for this function is included at the beginning of the program, and its name is placed in the proper vector location. Recall from our earlier discussion of complicated declarations that the line of code

void (* const _vectab[])()

tells us that _vectab is an array of constant pointers to functions that return the type void. The addresses of the various entries in the array of pointers to functions begin at the memory location 0xFFCE. The remainder of the vectors that follow each have a specific use, and if there were an interrupt service routine needed for the program, its address would be placed in the corresponding location.

/* INTERRUPT VECTORS TABLE 68HC912B32

* Copyright (c) 1997 by COSMIC Software */

void _stext(); /* startup routine */

void (* const _vectab[])() =

{ /* 0xFFCE */

0,

/* Reserved

 

*/

0,

/* BDLC

*/

 

0,

/* ATD

 

*/

0,

/* SCI 1

 

*/

0,

/* SCI 0

 

*/

0,

/* SPI

 

*/

0,

/* Pulse acc input

*/

0,

/* Pulse acc overf

*/

0,

/* Timer overf

 

*/

0,

/* Timer channel 7

*/

388 Chapter 7 Advanced Topics

0,

/* Timer channel 6 */

0,

/* Timer channel 5 */

0,

/* Timer channel 4 */

0,

/* Timer channel 3 */

0,

/* Timer channel 2 */

0,

/* Timer channel 1 */

0,

/* Timer channel 0 */

0,

/* Real time

*/

0,

/* IRQ

*/

0,

/* XIRQ

*/

0,

/* SWI

*/

0,

/* illegal

*/

0,

/* cop fail

*/

0,

/* cop clock fail

*/

_stext

/* RESET

*/

};

 

 

Listing 7-18: M68HC912B32 Vector Table

Listing 7-19 shows the next function that must be included for operation on the M68HC12. This start-up program is named crts.s. The .s extension indicates that the function is an assembly language function. This is the sum total of all assembly code needed for the program discussed in this chapter. Rather than give a line-by-line description of the code, we will see that the initial portion of the program initializes the section named bss to all zeros. This section is where all static and external memory are stored. Then the stack pointer is initialized to the value designated as __stack and then control is passed to the function main(). If main() should return, which it should not, the instruction following main() forms an infinite loop that branches to itself and does nothing.

;C STARTUP FOR MC68HC12

;Copyright (c) 1996 by COSMIC Software

xdef

_exit, __stext

 

xref

_main, __sbss,

__memory, __stack

;

 

 

__stext:

 

 

 

 

Putting It All Together 389

 

 

 

clra

 

; reset the bss

clrb

 

 

ldx

#__sbss

; start of bss

bra

loop

; start loop

zbcl:

 

 

std

2,x+

; clear word

loop:

 

 

cpx

#__memory ; up to the end

blo

zbcl

; and loop

lds

#__stack

; initialize stack pointer

jsr

_main

; execute main

_exit:

 

 

bra

_exit

; stay here

;

 

 

end

 

 

Listing 7-19: Crts.s C Program Start-Up Function

Any linker for an embedded system requires some type of linker command file. The command file for this application is as follows.

#link command file for test program

#Copyright (c) 1996 by COSMIC Software

+seg .text -b 0x8000 -n .text# program start address

+seg .const -a .text # constants follow program +seg .data -b 0x800 # data start address

+seg .eeprom -b 0xd00 -m768 #identify EEPROM block

+def __sbss=@.bss

# start address of bss

crts.o

# startup routine

monitor.o

# applications programs

saveit.o

 

encode.o

 

decode.o

 

reset.o

 

numbdup.o

 

putbcd.o

 

priout.o

 

priafter.o

 

serial.o

 

390Chapter 7 Advanced Topics

get.o

“C:\COSMIC\CX12\lib\libi.h12”

“C:\COSMIC\CX12\lib\libm.h12”

+seg .const -b 0xffce

# vectors start address

vector.o

# interrupt vectors

+def __memory=@.bss

# symbol used by library

+def __stack=0xc00

# stack pointer initial value

Listing 7-20: Linker Command File

This file can be broken into three sections. The first section specifies all of the important memory locations needed for the program. The first instruction identifies the text section as beginning at the hex address 0x8000. That is the beginning of the FLASH EEPROM on this chip. The designation text identifies the executing portion of the program. The next line says that all program constants shall be placed in the text section. Since the text section is placed in internal nonvolatile FLASH, nothing in this section can be changed by the program.

The internal RAM on this part is the 1024 bytes beginning at the hex address 0x800. The ending address of RAM is 0xBFF. The next instruction places data, variables and stack at the starting address 0x800. Jump to the end of this command file. Note that the parameter __stack is given a value of 0xC00. This value is used because the M68HC12 stack pointer points at the top of the stack rather than the next open location on the stack as was seen with the M68HC11 family of parts.

The final segment designation is that of the EEPROM location. This segment has a beginning address of 0xD00 and it contains 768 bytes.

The next section of the file contains the files to be linked. There are twelve applications files to be linked. With the exception of the crts.o file, these files are those created and tested earlier in this chapter. After the application program files, two standard compiler libraries are included. These libraries contain all functions needed to complete the program.

The last section links the vector table discussed above to the correct address in the program, establishes the value for the initial stack pointer, and remembers the address of the end of the bss section for use in the program.

The program was compiled, linked and an srecord of the code created. This code was loaded into an M68EVB912B32 evaluation