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

📄 chapter 9 structures -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<DIR>
<P>&nbsp;</P></DIR></DIR>
<P><FONT face="Times New Roman,Times"><A name=FSMDEFINITION></A>To place a 
structure in ROM, we define it as a global constant. In the following example 
the structure fsm[3] will be allocated and initialized in ROM-space. The 
linked-structure finite syatem machine is a good example of a ROM-based 
structure. For more information about finite state machines see Chapter 2 of the 
book <U>Embedded Microcomputer Systems: Real Time Interfacing</U> by Jonathan 
Valvano published by Brooks-Cole.</FONT></P>
<DIR>
<P><CODE>const&nbsp;struct&nbsp;State{<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;Out;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Output&nbsp;to&nbsp;Port&nbsp;H&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;int&nbsp;Wait;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Time&nbsp;(E&nbsp;cycles)&nbsp;to&nbsp;wait&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;AndMask[4];<BR>&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;EquMask[4];<BR>&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;struct&nbsp;State&nbsp;*Next[4];};&nbsp;&nbsp;/*&nbsp;Next&nbsp;states&nbsp;*/<BR>typedef&nbsp;const&nbsp;struct&nbsp;State&nbsp;StateType;<BR>typedef&nbsp;StateType&nbsp;*&nbsp;StatePtr;<BR>#define&nbsp;stop&nbsp;&amp;fsm[0]<BR>#define&nbsp;turn&nbsp;&amp;fsm[1]<BR>#define&nbsp;bend&nbsp;&amp;fsm[2]<BR>StateType&nbsp;fsm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ADDRESS>Listing 9-2: Example of initializing a structure in ROM</ADDRESS>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=POINTERS></A>Using pointers to 
access structures</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Just like other variables we can use 
pointers to access information stored in a structure. The syntax is illustrated 
in the following examples:</FONT></P>
<DIR>
<P><CODE><BR>void Setp(void){ path 
*ppt;<BR>&nbsp;&nbsp;&nbsp;ppt=&amp;p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
pointer to an existing global 
variable<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L1.x1=5;&nbsp;&nbsp;// black line from 5,6 
to 
10,12<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L1.y1=6;<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L1.x2=10;<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L1.y2=12;<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L1.color=255;<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;L2={5,6,10,12,255};&nbsp;&nbsp;// 
black line from 5,6 to 
10,12<BR>&nbsp;&nbsp;&nbsp;ppt-&gt;direction=-1;<BR>&nbsp;&nbsp;&nbsp;(*ppt).direction=-1;<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-3: Examples of accessing a structure using a 
pointer</ADDRESS>
<P><FONT face="Times New Roman,Times">Notice that the syntax 
<B>ppt-&gt;direction</B> is equivalent to <B>(*ppt).direction</B>. The 
parentheses in this access are required, because along with () and [], the 
operators <B>. </B>and <B>-&gt; </B>have the highest precedence and associate 
from left to right. Therefore <B>*ppt.direction</B> would be a syntax error 
because <B>ppt.direction</B> can not be evaluated.</FONT></P>
<P><FONT face="Times New Roman,Times">As an another example of pointer access 
consider the finite state machine controller for the fsm[3] structure shown 
above. The state machine is illustrated in Figure 9-1, and the program shown in 
Listing 9-4. There is </FONT><A 
href="http://www.ece.utexas.edu/~valvano/embed/chap10/chap10.htm#FSM">an example 
in Chapter 10</A><FONT face="Times New Roman,Times"> that extends this machine 
to implement function pointers.</FONT></P>
<P><IMG height=205 src="Chapter 9 Structures -- Valvano.files/fsm.gif" 
width=387></P>
<ADDRESS>Figure 9-1: State machine</ADDRESS>
<P><A name=FSMPROGRAM></A>&nbsp;</P>
<DIR>
<P><CODE>void&nbsp;control(void){&nbsp;StatePtr&nbsp;Pt;<BR>&nbsp;unsigned&nbsp;char&nbsp;Input;&nbsp;int&nbsp;Endt;&nbsp;unsigned&nbsp;int&nbsp;i;<BR>&nbsp;&nbsp;TSCR 
|=0x80;&nbsp;&nbsp;&nbsp;// 
TEN(enable)<BR>&nbsp;&nbsp;TMSK2=0xA2;&nbsp;&nbsp;&nbsp;&nbsp;// TOI arm, 
TPU(pullup) timer/4 
(500ns)<BR>&nbsp;&nbsp;DDRH=0xFF;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;PortH&nbsp;bits&nbsp;7-0&nbsp;are&nbsp;outputs<BR>&nbsp;&nbsp;DDRJ=0x00;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;PortJ&nbsp;bits&nbsp;7-0&nbsp;are&nbsp;inputs<BR>&nbsp;&nbsp;PUPSJ=0xFF;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Pullups&nbsp;J7-J0<BR>&nbsp;&nbsp;PULEJ=0xFF;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Enable&nbsp;pull&nbsp;up/down&nbsp;PortJ<BR>&nbsp;&nbsp;Pt=stop;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Initial&nbsp;State&nbsp;<BR>&nbsp;&nbsp;while(1){<BR>&nbsp;&nbsp;&nbsp;&nbsp;PORTH=Pt-&gt;Out;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;1)&nbsp;output<BR>&nbsp;&nbsp;&nbsp;&nbsp;Endt=TCNT+Pt-&gt;Wait;&nbsp;&nbsp;&nbsp;//&nbsp;Time&nbsp;(500 
ns 
each)&nbsp;to&nbsp;wait<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(Endt-TCNT&gt;0);&nbsp;&nbsp;&nbsp;//&nbsp;2)&nbsp;wait&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Input=PORTJ;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;3)&nbsp;input&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;for(i=0;i&lt;4;i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if((Input&amp;Pt-&gt;AndMask[i])==Pt-&gt;EquMask[i]){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pt=Pt-&gt;Next[i];&nbsp;//&nbsp;4)&nbsp;next&nbsp;depends&nbsp;on&nbsp;input<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i=4;&nbsp;}}};</CODE></P></DIR>
<ADDRESS>Listing 9-4: Finite state machine controller for MC68HC812A4</ADDRESS>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=FUNCTIONS></A>Passing Structures to 
Functions</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Like any other data type, we can pass 
structures as parameters to functions. Because most structures occupy a large 
number of bytes, it makes more sense to pass the structure by reference rather 
than by value. In the following "call by value" example, the entire 6-byte 
structure is copied on the stack when the function is called.</FONT></P>
<DIR>
<P><CODE>unsigned&nbsp;char&nbsp;Input(port&nbsp;thePort){<BR>&nbsp;&nbsp;&nbsp;return&nbsp;(*thePort.addr);}</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">When we use "call by reference", a pointer 
to the structure is passed when the function is called.</FONT></P>
<DIR>
<P><CODE>typedef&nbsp;const&nbsp;struct&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;mode;&nbsp;&nbsp;//&nbsp;0&nbsp;for&nbsp;I/O,&nbsp;1&nbsp;for&nbsp;in&nbsp;only&nbsp;-1&nbsp;for&nbsp;out&nbsp;only<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;volatile&nbsp;*addr;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;address<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;volatile&nbsp;*ddr;}port;&nbsp;//&nbsp;direction&nbsp;reg<BR>port&nbsp;PortJ={<BR>&nbsp;&nbsp;0,&nbsp;&nbsp;<BR>&nbsp;&nbsp;(unsigned&nbsp;char&nbsp;volatile&nbsp;*)(0x0028),<BR>&nbsp;&nbsp;(unsigned&nbsp;char&nbsp;volatile&nbsp;*)(0x0029)};<BR>int&nbsp;MakeOutput(port&nbsp;*ppt){<BR>&nbsp;&nbsp;if(ppt-&gt;mode==1)&nbsp;return&nbsp;0;&nbsp;//&nbsp;input&nbsp;only<BR>&nbsp;&nbsp;if(ppt-&gt;mode==-1)&nbsp;return&nbsp;1;&nbsp;//&nbsp;OK,&nbsp;output&nbsp;only<BR>&nbsp;&nbsp;(*ppt-&gt;ddr)=0xff;&nbsp;//&nbsp;make&nbsp;output<BR>&nbsp;&nbsp;return&nbsp;1;}<BR>int&nbsp;MakeInput(port&nbsp;*ppt){<BR>&nbsp;&nbsp;if(ppt-&gt;mode==-1)&nbsp;return&nbsp;0;&nbsp;//&nbsp;output&nbsp;only<BR>&nbsp;&nbsp;if(ppt-&gt;mode==1)&nbsp;return&nbsp;1;&nbsp;&nbsp;//&nbsp;OK,&nbsp;input&nbsp;only<BR>&nbsp;&nbsp;(*ppt-&gt;ddr)=0x00;&nbsp;//&nbsp;make&nbsp;input<BR>&nbsp;&nbsp;return&nbsp;1;}<BR>unsigned&nbsp;char&nbsp;Input(port&nbsp;*ppt){<BR>&nbsp;return&nbsp;(*ppt-&gt;addr);}<BR>void&nbsp;Output(port&nbsp;*ppt,&nbsp;unsigned&nbsp;char&nbsp;data){<BR>&nbsp;(*ppt-&gt;addr)=data;<BR>}<BR>void&nbsp;main(void){&nbsp;unsigned&nbsp;char&nbsp;MyData;<BR>&nbsp;&nbsp;MakeInput(&amp;PortJ);<BR>&nbsp;&nbsp;MakeOutput(&amp;PortJ);<BR>&nbsp;&nbsp;Output(&amp;PortJ,0);<BR>&nbsp;&nbsp;MyData=Input(&amp;PortJ);<BR>}</CODE></P></DIR>
<ADDRESS>Listing 9-5: Port access organized with a data structure</ADDRESS>
<P>&nbsp;</P>
<P><B><I><FONT face=Helvetica,Arial><A name=EXTENDEDADDRESS></A>Extended Address 
Data Page Interface to the MC68HC812A4</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">One of the unique features of the 
MC68HC812A4 is its ability to interface large RAM and ROM using the data page 
and program page memory respectively. Up to 1Meg bytes can be configured using 
the data page system. In this example, two 128K by 8 bit RAM chips are 
interfaced to the 6812 using 18 address pins (A17-A0). The 628128 is a 128K by 8 
bit static RAM. When the paged memory is enabled Port G will contain address 
lines A21-A16 (although the data page system can only use up to A19). The 
built-in address decoder CSD must be used with the data page system. The details 
of this interface can be found in Chapter 9 of the book <U>Embedded 
Microcomputer Systems: Real Time Interfacing</U> by Jonathan Valvano published 
by Brooks Cole.</FONT></P>
<P><IMG height=240 src="Chapter 9 Structures -- Valvano.files/Image41.gif" 
width=427></P>
<ADDRESS>Figure 9-2: Extended data memory interface for the 
MC68HC812A4</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">We divide the software into initialization 
and access. The initialization software performs the usual steps:</FONT></P>
<DIR>
<P><FONT face="Times New Roman,Times">enable E, LSTRB, CSD, R/W 
outputs</FONT><FONT face=Monaco><BR></FONT><FONT 
face="Times New Roman,Times">clear bit 4 in the CSCTL1 to set up CSD for the 
$7000-$7FFF range</FONT><FONT face=Monaco><BR></FONT><FONT 
face="Times New Roman,Times">select 1 cycle stretch on CSD</FONT></P></DIR>
<P><FONT face="Times New Roman,Times">To enable the data page system we must 
also</FONT></P>
<DIR>
<P><FONT face="Times New Roman,Times">set bit 7 in WINDEF to enable the Data 
Page Window</FONT><FONT face=Monaco><BR></FONT><FONT 
face="Times New Roman,Times">set bits 1,0 in MXAR to enable memory expansion 
pins A17-A16</FONT></P>
<P><CODE>void 
RAMinit(void){<BR>&nbsp;&nbsp;&nbsp;MODE=0x7B&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
special expanded wide 
mode<BR>&nbsp;&nbsp;&nbsp;PEAR=0x2C;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
enable E, R/W, 
LSTRB<BR>&nbsp;&nbsp;&nbsp;WINDEF=WINDEF|0x80;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
enable 
DPAGE<BR>&nbsp;&nbsp;&nbsp;MXAR=0x03;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
enable A17, A16 on Port 
G<BR>&nbsp;&nbsp;&nbsp;CSCTL0=CSCTL0|0x10;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
enable 
CSD<BR>&nbsp;&nbsp;&nbsp;CSCTL1=CSCTL1&amp;0xEF;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
CSD $7000 to 
$7FFF<BR>&nbsp;&nbsp;&nbsp;CSSTR0=(CSSTR0&amp;0xFC)|0x01;}&nbsp;&nbsp;&nbsp;// 1 
cycle stretch on CSD</CODE></P></DIR>
<ADDRESS>Listing 9-6: Extended memory initialization on the 
MC68HC812A4</ADDRESS>
<P><FONT face="Times New Roman,Times">Let A17-A0 be the desired 256K RAM 
location. To access that location requires two steps</FONT></P>
<DIR>
<P><FONT face="Times New Roman,Times">set the most significant addresses A17-A12 
into DPAGE register</FONT><FONT face=Monaco><BR></FONT><FONT 
face="Times New Roman,Times">access $7000 to $7FFF, with the least significant 
addresses A11-A0 </FONT></P>
<P><CODE>struct addr20<BR>{&nbsp;&nbsp;unsigned char msb;&nbsp;&nbsp;// bits 
19-12, only 17-12 used here<BR>&nbsp;&nbsp;&nbsp;unsigned int 
lsw;&nbsp;&nbsp;&nbsp;// bits 11-0<BR>};<BR>typedef struct addr20 
addr20Type;<BR>char ReadMem(addr20Type addr){ char 
*pt;<BR>&nbsp;&nbsp;&nbsp;DPAGE=addr.msb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// set 
addr bits 19-12, only 17-12 used<BR>&nbsp;&nbsp;&nbsp;pt=(char 
*)(0x7000+addr.lsw);&nbsp;&nbsp;&nbsp;// set address bits 
11-0<BR>&nbsp;&nbsp;&nbsp;return 
*pt;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// read 
access<BR>void WriteMem(addr20Type addr, char data){ char 
*pt;<BR>&nbsp;&nbsp;&nbsp;DPAGE=addr.msb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
set addr bits 19-12, only 17-12 used<BR>&nbsp;&nbsp;&nbsp;pt=(char 
*)(0x7000+addr.lsw);&nbsp;&nbsp;// set address bits 
11-0<BR>&nbsp;&nbsp;&nbsp;*pt=data;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
write access</CODE></P></DIR>
<ADDRESS>Listing 9-7: Extended memory access on the MC68HC812A4</ADDRESS>
<P>&nbsp;</P>
<P><FONT face="Times New Roman,Times">When MXAR is active, the MC68HC812A4 will 
convert all 16 addresses to the extended 22 bit addresses. When an access is 
outside the range of any active page window (EPAGE, DPAGE or PPAGE), the upper 6 
bits are 1. In the following table it is assumed that only the DPAGE is active, 
the DPAGE register contains DP7-DP0, and MXAR is 0x3F (activating all 22 address 
bits)</FONT></P>
<DIV align=right>
<P>
<TABLE cellSpacing=0 width=615 border=0>
  <TBODY>
  <TR>
    <TD vAlign=top width="12%"><FONT 
      face="Times New Roman,Times">internal</FONT></TD>
    <TD vAlign=top width="7%"><FONT face="Times New Roman,Times">A21</FONT></TD>
    <TD vAlign=top width="7%"><FONT face="Times New Roman,Times">A20</FONT></TD>
    <TD vAlign=top width="8%"><FONT face="Times New Roman,Times">A19</FONT></TD>
    <TD vAlign=top width="7%"><FONT face="Times New Roman,Times">A18</FONT></TD>
    <TD vAlign=top width="8%"><FONT face="Times New Roman,Times">A17</FONT></TD>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -