智能车制作

标题: 问几个关于C语言编程的问题 请高手帮帮忙 [打印本页]

作者: yilin    时间: 2008-11-12 03:38
标题: 问几个关于C语言编程的问题 请高手帮帮忙
<><FONT size=4>#include &lt;hidef.h&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* common defines and macros */<BR>//#include &lt;mc9s12dg128.h&gt;&nbsp;&nbsp;&nbsp;&nbsp; /* derivative information */</FONT></P><P><FONT size=4>#define PORTB&nbsp; (*((volatile unsigned char *)(0x0001)))//<BR>#define DDRB&nbsp;&nbsp; (*((volatile unsigned char *)(0x0003)))<BR>#define TSCR1&nbsp; (*((volatile unsigned char *)(0x0046)))<BR>#define TSCR2&nbsp; (*((volatile unsigned char *)(0x004D)))<BR>#define TSCR3&nbsp; (*((volatile unsigned char *)(0x004F)))<BR>#define TCNT&nbsp;&nbsp; (*((volatile unsigned short *)(0x0044)))<BR>int counter;<BR>void TimeOverflow(void){<BR>&nbsp; <BR>&nbsp; while (TCNT!=0x0000);<BR>&nbsp; while (TCNT==0x0000);<BR>&nbsp; counter++;<BR>&nbsp; if (counter==8) PORTB=0x7e;<BR>&nbsp; if (counter==7) PORTB=0xbd;<BR>&nbsp; if (counter==6) PORTB=0xdb;<BR>&nbsp; if (counter==5) PORTB=0xe7;<BR>&nbsp; if (counter==4) PORTB=0xe7;<BR>&nbsp; if (counter==3) PORTB=0xdb;<BR>&nbsp; if (counter==2) PORTB=0xbd;<BR>&nbsp; if (counter==1) PORTB=0x7e;<BR>}<BR>void main(void) {<BR>&nbsp; /* put your own code here */<BR>&nbsp; EnableInterrupts;<BR>&nbsp; TSCR1=0x80;<BR>&nbsp; TSCR2=0x03;<BR>&nbsp; counter=0;<BR>&nbsp; DDRB=0xff;<BR>&nbsp; PORTB=0xff;<BR>&nbsp; for(;;) {<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; TimeOverflow();<BR>&nbsp;&nbsp;&nbsp; if (counter&gt;=8){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter=0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TSCR2=0x05;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp; }<BR>}</FONT></P><P><FONT size=4></FONT>&nbsp;</P><P><FONT size=4>我想问一下#define PORTB&nbsp; (*((volatile unsigned char *)(0x0001)))&nbsp;&nbsp; 这条语句中“(*((volatile unsigned char *)(0x0001)))&nbsp;”什么意思?我可以这样写吗?#define PORTB&nbsp;&nbsp;0x0001&nbsp; </P><P><BR></P></FONT>
作者: whut_wj    时间: 2008-11-12 05:31
标题: Re:问几个关于C语言编程的问题 请高手帮帮忙
<><FONT size=4>(*((volatile unsigned char *)(0x0001)))</FONT></P><P><FONT size=4>unsigned char *是指针定义</FONT></P><P><FONT size=4>volatile 是不易变的,IDE不会自动优化.</FONT></P><P><FONT size=4>(*((volatile unsigned char *)是指针的内容.</FONT></P><P><FONT size=4>不能写成这样: #define PORTB&nbsp;&nbsp;0x0001,好好领会一下吧.&nbsp; </FONT></P><P><FONT size=4></FONT>&nbsp;</P>
作者: yilin    时间: 2008-11-18 05:15
标题: Re:问几个关于C语言编程的问题 请高手帮帮忙
<>在C语言中不是可以吗?能给详细讲解一下吗?谢谢&nbsp; !!!&nbsp;</P>
作者: XELLRO    时间: 2008-11-26 19:42
标题: Re:问几个关于C语言编程的问题 请高手帮帮忙
<>肯定不行!</P><P><FONT size=4>#define PORTB&nbsp;&nbsp;0x0001 的话,编译器直接就将“0x0001”替换"PORTB"了。 这样,在赋值的时候,显然是错误的。</FONT></P><P><FONT size=4>#define PORTB&nbsp; (*((volatile unsigned char *)(0x0001)))&nbsp;&nbsp; ,是对PORTB 的地址定义,因为硬件地址已经映射在了RAM里面。 (*((volatile unsigned char *)(0x0001)))&nbsp;&nbsp;就是将物理地址“0x0001”转化为RAM里对应的地址单元。这个过程就是一个指针定位的过程。 </FONT></P><P><FONT size=4>这样再给PORTB=0xff 赋值时就正确了。</FONT></P><P>&nbsp;</P>
作者: zelong    时间: 2009-3-11 17:51
c是的好好学啊




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2