嵌入式技术论坛
直播中

张玲玲

8年用户 6经验值
擅长:可编程逻辑 模拟技术 嵌入式技术 控制/MCU
私信 关注
[问答]

用C51向hc06蓝牙模块发送字符,手机调试助手一直接收的是乱码

如题,波特率是9600,程序如下,小白不知道哪里有问题,请各位大佬帮忙解决
*****************************************************************
  *******51单片机通过HC-05实现无线通信********(本人用的是hc06)
  
  注:单片机选择为STC89C52RC,程序用于单片机控制HC-05发送数据,
  波特率:9600 停止位:1位 数据位;8位 奇偶校验:无
  字符集选择GBK,UTF-8均可通信
********************************************************************/
#include
#define uchar unsigned char
#define uint unsigned int
  uchar code char_temp[6]={'1','2','3','a','b'};/**
* 串口初始化函数
* 波特率为9600
*/
void UartConfigurationInit()
{
    TMOD=0x20;  //设置定时器1工作方式为方式2   
    TH1=0xfd;     //波特率9600
    TL1=0xfd;  
    TR1=1;      //启动定时器1     
    SM0=0;SM1=1;      //串口方式1         
    REN=1;      //允许接收   
    PCON=0x00;  //关倍频   
    ES=1;       //开串口中断   
    EA=1;       //开总中断
}/**
* 延时函数
* 延时count毫秒
*/
void delay(uint count)     
{
   uint cycle;
   while(count)
   {
     cycle=120;
     while(cycle>0) cycle--;
     count--;
   }
}
/**
* 字符发送函数
*/
void PostChar(uchar character)  
{     
     SBUF=character;   //发送单个字符
     while(!TI);TI=0; //发送完成标志
}
/**
* 字符串发送函数
* 通过调用字符发送函数来实现
*/
void  PostString(uchar *p)  
{  
     while(*p)      //若指针指向的地址为空,则跳出循环
     {  
         PostChar(*p); //指针第一次默认指向首地址
   delay(20);  //延时,作用为提高发送准确度
         p++;  
     }   
}
/**
* 整型转字符型函数
* 将整形数据按位转为字符串后依次发送
*/
void IntConversionChar(uint number)
{
       uint model; //模
       uint numberByte; //十进制数据不同位上的值
     for(model=1000;model>=1;model=model/10) //model的取值根据实际需要选择,选择过大会浪费资源
      {
            numberByte = number/model; //相除取整
   //当取到整数数据的最高位时,将其加上0x30或者48并转为字符型后发送
            if(numberByte >=1 ) PostChar((uchar)(numberByte%10+0x30));
        //if(numberByte >=1 ) PostChar((unsigned char)(numberByte%10+48));
   }
}

/**
* 主函数
*/
void main()
{
  uint number = 0;  //设置动态整型数据初值
  UartConfigurationInit(); //初始化串口
  while(1)      
  {   
     PostString(char_temp); //发送字符串
  delay(1000);
  IntConversionChar(number);   //发送整数
  delay(1000);
  number++;
  }
}<
/div>

回帖(2)

李丽华

2018-12-3 22:29:59

最佳答案

建议仿一下,跟踪一下字符串的数据
1 举报

张玲玲

2018-12-14 13:22:53
已经找到原因了,是因为晶振和波特率不匹配,波特率9600晶振应该选11.0592……小白果然什么都不会
1 举报

更多回帖

×
20
完善资料,
赚取积分