📄 chapter 11 preprocessor directives -- valvano.htm
字号:
*/<BR>#endif<BR> i=j+1;<BR>#ifdef
Debug<BR> PORTC&=~0x01; /* PC0 cleared when Sub is
exited */<BR>#endif<BR> return(i);}<BR>void Program(){
int i;<BR>#ifdef Debug<BR> PORTC|=0x02; /* PC1 set when
Program is entered
*/<BR>#endif<BR> i=Sub(5);<BR> while(1)
{ PORTC=2; i=Sub(i);}}<BR>void ProgB(){ int
i;<BR> i=6;<BR>#ifdef
Debug<BR> PORTC&=~0x02; /* PC1 cleared when Sub is
exited */<BR>#endif<BR>}</CODE><FONT
face="Times New Roman,Times"><BR></FONT></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.3: Conditional
compilation can help in removing all debugging code</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">For more information about debugging see
Chapter 2 of Valvano's </FONT><U>Embedded Microcomputer Systems: Real Time
Interfacing.</U></P>
<P> </P>
<P><B><FONT face="Times New Roman,Times"><A name=INCLUDE></A></FONT><I><FONT
face=Helvetica,Arial>Including Other Source Files</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The preprocessor also recognizes
directives to include source code from other files. The two
directives</FONT></P>
<DIR>
<DIR>
<P><CODE>#include "Filename"</CODE></P>
<P><CODE>#include <Filename></CODE></P></DIR></DIR>
<P><FONT face="Times New Roman,Times">cause a designated file to be read as
input to the compiler. The difference between these two directives is where the
compiler looks for the file. The <filename> version will search for the
file in the standard include directory, while the "filename" version will search
for the file in the same directory as the original source file. The preprocessor
replaces these directives with the contents of the designated files. When the
files are exhausted, normal processing resumes. </FONT></P>
<P><FONT face="Times New Roman,Times">Filename follows the normal MS-DOS file
specification format, including drive, path, filename, and extension.
</FONT></P>
<P><FONT face="Times New Roman,Times">In </FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#PRIVATE">Chapter
10</A><FONT face="Times New Roman,Times">, an example using #include was
presented that implemented a feature similar to encapsulated objects of C++,
including private and public functions.</FONT></P>
<P> </P>
<P><FONT face="Times New Roman,Times"><B><A
name=PRAGMA></A></B></FONT><B><I><FONT face=Helvetica,Arial>Interrupt
software</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">The ICC11/ICC12 preprocessor also
recognizes three pragma directives that we will use to develop interrupt
software. We use the </FONT><CODE>interrupt_handler</CODE><FONT
face="Times New Roman,Times"> pragma to specify a function as an interrupt
handler. The compiler will then use the rti instruction rather than the rts
instruction to return from </FONT><CODE>ExtHan</CODE><FONT
face="Times New Roman,Times">.</FONT></P>
<P><CODE>#pragma interrupt_handler ExtHan()<BR>void ExtHan(void){
<BR> KWIFJ=0x80; // clear
flag<BR> PutFifo(PORTJ&0x7F);}<BR></CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.4: Interrupt service
routines are specified using a pragma in ICC11/ICC12.</FONT></ADDRESS>
<ADDRESS> </ADDRESS>
<P><FONT face="Times New Roman,Times">We use the
</FONT><CODE>abs_address</CODE><FONT face="Times New Roman,Times"> and
</FONT><CODE>end_abs_address</CODE><FONT face="Times New Roman,Times"> pragmas
to define the interrupt vector.</FONT></P>
<P><CODE>#pragma abs_address:0xffdo<BR>void (*KeyWakupJ_interrupt_vector[])() =
{<BR> ExtHan}; /* 812 KeyWakeUpJ */<BR><BR>#pragma
end_abs_address</CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.5: Pragmas allow us to
define interrupt vectors in ICC11/ICC12.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">We also set the reset vector using the
</FONT><CODE>abs_address</CODE><FONT face="Times New Roman,Times"> and
</FONT><CODE>end_abs_address</CODE><FONT face="Times New Roman,Times">
pragmas.</FONT></P>
<P><CODE>extern void _start(); /* entry point in crt12.s */<BR>#pragma
abs_address:0xfffe<BR>void (*Reset_interrupt_vectors[])() =
{<BR> _start }; /* fffe RESET, entry point into ICC12
*/<BR>#pragma end_abs_address<BR></CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.6: Pragmas allow us to
define the reset vector in ICC11/ICC12.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times">We will not use pragmas to develop
interrupt software with the Hiware compiler. We use the
</FONT><CODE>interrupt</CODE><FONT face="Times New Roman,Times"> key word to
specify a function as an interrupt handler. The Hiware compiler will then use
the rti instruction rather than the rts instruction to return from
</FONT><CODE>ExtHan</CODE><FONT face="Times New Roman,Times">. We start counting
the interrupt number from reset. Some of the interrupt numbers used by Hiware
for the MC68HC812A4 are shown in the following table.</FONT></P>
<P>
<TABLE cellSpacing=0 width=390 border=0>
<TBODY>
<TR>
<TD vAlign=top width="17%">number</TD>
<TD vAlign=top width="83%">source</TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">24</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">Key wakeup
H</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">23</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">Key wakeup
J</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">20</FONT></TD>
<TD vAlign=top width="83%"><FONT
face="Times New Roman,Times">SCI0</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">16</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">timer
overflow</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">15</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">timer
channel 7</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">8</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">timer
channel 0</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">6</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">Key wakeup
D</FONT></TD></TR>
<TR>
<TD vAlign=top width="17%"><FONT face="Times New Roman,Times">4</FONT></TD>
<TD vAlign=top width="83%"><FONT face="Times New Roman,Times">SWI software
interrupt</FONT></TD></TR>
<TR>
<TD width="17%"><FONT face="Times New Roman,Times">0</FONT></TD>
<TD width="83%"><FONT
face="Times New Roman,Times">reset</FONT></TD></TR></TBODY></TABLE></P>
<P><I>Table 11-1: Interrupt numbers for the MC68HC812A4 used by Hiware</I></P>
<P><FONT face="Times New Roman,Times">Hiware will automatically set the
interrupt vector for KeyWakeup J to point to the ExtHan routine.</FONT></P>
<P><CODE>void interrupt 23 ExtHan(void){ <BR> KWIFJ=0x80;
// clear
flag<BR> PutFifo(PORTJ&0x7F);}<BR></CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.7: Interrupt service
routines are specified in Hiware.</FONT></ADDRESS>
<ADDRESS> </ADDRESS>
<P><FONT face="Times New Roman,Times">We use the prm linker file to define the
reset vector.</FONT></P>
<P><CODE>LINK keywake.abs<BR>NAMES keywake.o start12s.o ansis.lib
END<BR>SECTIONS <BR>MY_RAM = READ_WRITE 0x0800 TO 0x0AFF;<BR>MY_ROM = READ_ONLY
0xF000 TO 0xFF00;<BR>MY_STK = READ_WRITE 0x0B00 TO
0x0BFF;<BR>PLACEMENT<BR>DEFAULT_ROM INTO MY_ROM;<BR>DEFAULT_RAM INTO
MY_RAM;<BR>SSTACK INTO MY_STK;<BR>END<BR>/* set reset vector to function
_Startup defined in startup code start12.c */<BR>VECTOR ADDRESS 0xFFFE
_Startup<BR></CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.8: The last line of the
PRM linker file defines the reset vector in Hiware.</FONT></ADDRESS>
<P><FONT face="Times New Roman,Times"><BR> </FONT></P>
<P><FONT face="Times New Roman,Times">For more information about interrupts see
Chapter 4 of Valvano's </FONT><U>Embedded Microcomputer Systems: Real Time
Interfacing.</U></P>
<P> </P>
<P>Go to <A
href="http://www.ece.utexas.edu/~valvano/embed/chap12/chap12.htm">Chapter 12 on
Assembly Language</A> <FONT face="Times New Roman,Times">Return to <A
href="http://www.ece.utexas.edu/~valvano/embed/toc1.htm">Table of
Contents</A></FONT></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -