程序有点小疑惑,望发烧友们指点迷津。

2019-11-25 15:35发布

<font face="微软雅黑"><font style="font-size:18px">想让单片机的一个LED闪烁500ms熄灭500ms,也达到目的了,可是对于程序的某个小地方有些不解,问题写在程序里面了。</font></font><br><pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">"reg52.h"</span></span> <span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> uint unsigned int</span> <span class="hljs-keyword">typedef</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> uchar; sbit led=P2^<span class="hljs-number">1</span>; uint i; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">binter</span><span class="hljs-params">()</span></span>; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{ binter(); i=<span class="hljs-number">0</span>; <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) { IF(TR0==<span class="hljs-number">1</span>) i++; <span class="hljs-comment">//(这个if语句删除后再在中断里加i++,载入单片机后能达到预期500ms,led变换一次,可是保留if语句就不行)</span> <span class="hljs-keyword">if</span>(i==<span class="hljs-number">10</span>) { i=<span class="hljs-number">0</span>; led=~led; } } } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">binter</span><span class="hljs-params">()</span> </span>{ EA=<span class="hljs-number">1</span>; ET0=<span class="hljs-number">1</span>; TR0=<span class="hljs-number">1</span>; TH0=(<span class="hljs-number">65536</span><span class="hljs-number">-50000</span>)/<span class="hljs-number">256</span>; TL0=(<span class="hljs-number">65536</span><span class="hljs-number">-50000</span>)%<span class="hljs-number">256</span>; TMOD=<span class="hljs-number">0x01</span>; } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">t50ms</span><span class="hljs-params">()</span> interrupt 1 </span>{ TH0=(<span class="hljs-number">65536</span><span class="hljs-number">-50000</span>)/<span class="hljs-number">256</span>; TL0=(<span class="hljs-number">65536</span><span class="hljs-number">-50000</span>)%<span class="hljs-number">256</span>; <span class="hljs-comment">//i++;</span> } </code></pre><p><br></p>
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。