51单片机(ST89C52):利用JDY-16蓝牙模块控制LED小灯亮灭,程序编译没有问题,但为什么功能实现不了?

2019-11-22 14:50发布

<pre style="max-width: 100%;"><code class="cpp hljs" codemark="1"><span class="hljs-comment">//利用蓝牙模块控制单片机上一个LED小灯亮灭</span> <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;reg52.h&gt;</span></span> sbit LED=P2^<span class="hljs-number">0</span>; <span class="hljs-comment">//LED控制IO口</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">chushihua</span><span class="hljs-params">()</span></span>; <span class="hljs-comment">//初始化串口</span> <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">char</span> dat; <span class="hljs-comment">//利用dat接收蓝牙发送的信息</span> <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{ chushihua(); <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) { IF(dat==<span class="hljs-number">1</span>) { LED=<span class="hljs-number">0</span>; } <span class="hljs-keyword">else</span> { LED=<span class="hljs-number">1</span>; } } } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">chushihua</span><span class="hljs-params">()</span> </span>{ SCON=<span class="hljs-number">0x50</span>; TMOD=<span class="hljs-number">0x20</span>; PCON=<span class="hljs-number">0x00</span>; TH1=<span class="hljs-number">0xFD</span>; <span class="hljs-comment">//波特率9600,晶振11.0592M</span> TL1=<span class="hljs-number">0XFD</span>; TR1=<span class="hljs-number">1</span>; ES=<span class="hljs-number">1</span>; EA=<span class="hljs-number">1</span>; } <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">zhongduan</span><span class="hljs-params">(<span class="hljs-keyword">void</span>)</span> interrupt 4 <span class="hljs-keyword">using</span> 1 </span>{ <span class="hljs-keyword">if</span> (RI==<span class="hljs-number">1</span>) <span class="hljs-comment">//传送信息后串口中断</span> { RI=<span class="hljs-number">0</span>; <span class="hljs-comment">//重置串口</span> dat=SBUF; <span class="hljs-comment">//将寄存器信息放入dat中</span> } } </code></pre><p><br></p>
3条回答
邓志军
2019-11-22 22:55 .采纳回答
1,你pc或者手机的应有有没有正确连接到蓝牙,这个可以使用串口工具(ch240)接蓝牙模块,通过串口软件去验证。
2,你发送的是十六进制数据还是字符串,1的十六进制数据就是1,字符串的‘1’的16进制数据是0x31(十进制是49)。
如果发送的直接是十六进制数据那么if(dat==1)是对的,否则if(dat==0x31)或if(dat==49),具体验证数据是什么请参考第一步收到的数据

一周热门 更多>