36351 发表于 2006-9-13 05:27:44

[求助]LED计数问题!!!

问题如楼下:

36351 发表于 2006-9-15 06:52:37

<p>那应该是p0.0亮阿</p><p>为什么是p0.1亮?</p>

36351 发表于 2006-9-15 07:35:02

<p>所以如果加了那句,就是如果按键一直没有松开,程序就一直会在</p><p>while(p0_0==0)[空语句];</p><p>里面的空语句执行?</p><p>不会出去??</p>

36351 发表于 2006-9-13 05:37:17

<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">#include &lt;AT89X51.H&gt;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><p><font face="Times New Roman">&nbsp;</font></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">unsigned char count;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><p><font face="Times New Roman">&nbsp;</font></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">void delay10ms(void)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp; </span>unsigned char i,j;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp; </span>for(i=20;i&gt;0;i--)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp; </span>for(j=248;j&gt;0;j--);<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">}<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><p><font face="Times New Roman">&nbsp;</font></p></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">void main(void)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp; </span>while(1)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(P0_0==0)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>delay10ms();<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(P0_0==0)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>count++;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>if(count==16)<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>{<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>count=0;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>P0=~count;<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>while(P0_0==0);<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>}<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; </span>}<p></p></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 12pt; mso-line-height-rule: exactly;"><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman">}</font></span></p><span lang="EN-US" style="FONT-SIZE: 9pt;"><font face="Times New Roman"><p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><font size="3"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;"></span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><font size="4"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">功能:每按下一次开关KB</span><span lang="EN-US">1</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">,计数值加</span><span lang="EN-US">1</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">,通过</span><span lang="EN-US">AT89S52</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">单片机的</span><span lang="EN-US">P1</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">端口的</span><span lang="EN-US">P0.0</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">到</span><span lang="EN-US">P0.3</span><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">显示出其的二进制计数值。</span></font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;"><font size="4"></font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;"><font size="4">因为我们的学习板的设计是:键盘和LED是连在一起的。</font></span></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><font size="4"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: &quot;Times New Roman&quot;; mso-hansi-font-family: &quot;Times New Roman&quot;;">所以我上面的程序使用了p0.0作为按键。那么第一次按下KB1(p0.0)时候应该是LED1(也是p0.0)</span>亮。</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><font size="4">但是我实际看到的现象是他是从led2开始计数的。也就是第一次按KB1,LED2会马上亮。</font></p><p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; mso-para-margin-left: 2.0gd;"><font size="4">为什么会这样?</font></p></p></font></span>

36351 发表于 2006-9-13 05:39:30

<p>还有一个问题:</p><p>程序尾部有这样一个语句:</p><p><font face="Times New Roman">while(P0_0==0);</font></p><p><font face="Times New Roman">好像没有什么用啊!!</font></p><p><font face="Times New Roman">作什么的呢?</font></p>

cpubbs 发表于 2006-9-13 19:09:42

<p><font face="Times New Roman">while(P0_0==0);</font></p><p><font face="Times New Roman">这个是等待键松开!</font></p>

cpubbs 发表于 2006-9-13 19:14:50

<font face="Times New Roman">第一次按count变为1,但是你这里P0=~count;按位取反一次,这时候P0等于1按位取反,是0xfe了,应该除按键1外的所有引键会变成为高电平了!</font>
页: [1]
查看完整版本: [求助]LED计数问题!!!