<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"><reg52.h></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>
2,你发送的是十六进制数据还是字符串,1的十六进制数据就是1,字符串的‘1’的16进制数据是0x31(十进制是49)。
如果发送的直接是十六进制数据那么if(dat==1)是对的,否则if(dat==0x31)或if(dat==49),具体验证数据是什么请参考第一步收到的数据
一周热门 更多>