⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 11 preprocessor directives -- valvano.htm

📁 关于如何利用C++在嵌入式系统中应用
💻 HTM
📖 第 1 页 / 共 2 页
字号:
*/<BR>#endif<BR>&nbsp;&nbsp;&nbsp;&nbsp;i=j+1;<BR>#ifdef 
Debug<BR>&nbsp;&nbsp;&nbsp;&nbsp;PORTC&amp;=~0x01; /* PC0 cleared when Sub is 
exited */<BR>#endif<BR>&nbsp;&nbsp;&nbsp;&nbsp;return(i);}<BR>void Program(){ 
int i;<BR>#ifdef Debug<BR>&nbsp;&nbsp;&nbsp;&nbsp;PORTC|=0x02; /* PC1 set when 
Program is entered 
*/<BR>#endif<BR>&nbsp;&nbsp;&nbsp;&nbsp;i=Sub(5);<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(1) 
{ PORTC=2; i=Sub(i);}}<BR>void ProgB(){ int 
i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;i=6;<BR>#ifdef 
Debug<BR>&nbsp;&nbsp;&nbsp;&nbsp;PORTC&amp;=~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>&nbsp;</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 &lt;Filename&gt;</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 &lt;filename&gt; 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>&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;KWIFJ=0x80; // clear 
flag<BR>&nbsp;&nbsp;&nbsp;&nbsp;PutFifo(PORTJ&amp;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>&nbsp;</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>&nbsp;&nbsp;&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;_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>&nbsp;&nbsp;&nbsp;&nbsp;KWIFJ=0x80; 
// clear 
flag<BR>&nbsp;&nbsp;&nbsp;&nbsp;PutFifo(PORTJ&amp;0x7F);}<BR></CODE></P>
<ADDRESS><FONT face="Times New Roman,Times">Listing 11.7: Interrupt service 
routines are specified in Hiware.</FONT></ADDRESS>
<ADDRESS>&nbsp;</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>&nbsp;</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>&nbsp;</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 + -