| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1174 人关注过本帖
标题:C语言代码错误,求改正
只看楼主 加入收藏
Q45786
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-5-23
收藏
 问题点数:0 回复次数:0 
C语言代码错误,求改正
此段代码是超声波测距语音播报,编译无错,但是调试的时候,播报不是所有音频都播报一遍就是只重复播报一个音频。
语音播报模块用的是DFPlayer Mini
求改正,希望能正确播报测量的距离
PS:音频在SD卡根目录下,顺序为0-9,十,百,厘米,测量距离

//#include<reg52.h>
#include <intrins.h>
#include "STK6037.h"
#define uchar unsigned char
#define uint unsigned int
uint num=0;
uint timer=0;
unsigned char table[9]={0,0,0,0,0,0,0,0,0};//显示的数值
bit flag;           //温度变量
uint temp;
uint time=0;
unsigned long S=0;
bit flagg=0;
//   1602引脚定义
sbit RW=P1^1;
sbit RS=P1^0;  
sbit E=P1^2;   

//报警引脚定义
sbit feng=P1^5;
sbit led=P2^0;

//按键控制
sbit key1=P3^4;
sbit key2=P3^5;

sbit ds=P3^2;  /温度引脚
//超声波位定义
sbit Trig = P2^1;
sbit Echo = P2^2;
int numm=0,nnum=0;

void delayms(unsigned int ms)
{
        unsigned char i=100,j;
        for(;ms;ms--)
        {
                while(--i)
                {
                        j=10;
                        while(--j);
                }
        }
}

void delay2(uchar p)
        {
          uchar l,k;
           for(l=p;l>0;l--)
              for(k=110;k>0;k--);
        }

void delay(uchar z)
{
    while(z--);
}

//温度初始化函数                       
void ds_reste()         
{
        ds=1;
        delay(5);
        ds=0;
        delay(80);
        ds=1;
        delay(14);
        if(ds==0)                 //判断ds总线
        flag=1;
        else
        flag=0;
        delay(20);
}

//读一位字节
bit ds_read_byte()        
{
         bit dat;
         ds=0;               
         _nop_();
         _nop_();
         ds=1;
         _nop_();
         dat=ds;         
         delay(10);
         return dat;
}

//读一位字节
uchar ds_read()        
{
        uchar i,j,k;
      for(i=0;i<8;i++)
        {
        j=ds_read_byte();
        k=(j<<7)|(k>>1);
        }
         return k;
}

//写入数据
void ds_write(uchar dat)
{
         uchar i;
         for(i=0;i<8;i++)
    {
         ds=0;
         _nop_();
         ds=dat&0x01;
         delay(6);
         ds=1;
         dat=dat>>1;
        }
         delay(6);
}

//读取温度
uint read_temp()         
{
        uchar a,b;
        ds_reste();
        ds_write(0xcc);
        ds_write(0xbe);
        a=ds_read();
        b=ds_read();
        temp=b;
        temp=temp<<8;
        temp=temp|a;
        temp=temp*0.0625*10+0.5;                 
        return temp;
}

//写入命令1602
void write_com(uchar com)           
{
        RS=0;
        RW=0;
        E=0;
        P0=com;           
        delay2(5);
        E=1;
        delay2(5);
        E=0;
}

//写入数据1602
void write_data(uchar date)        
{
        RS=1;
        RW=0;
        E=0;
        P0=date;               
        delay2(5);
        E=1;
        delay2(5);
        E=0;
}

//1602初始化显示
void init()                                 
{
    RS=0;
        write_com(0x38);
         write_com(0x0c);
         write_com(0x06);
        write_com(0x01);
        write_com(0x80);
}

//温度在1602上显示出来
void display(uint tempp)           
{  
         write_com(0x80+0x0b);
     table[0]=tempp/100;
         write_data(table[0]+0x30);         
         delay2(10);
  
         write_com(0x80+0x0c);
     table[1]=tempp%100/10;
         write_data(table[1]+0x30);
         delay2(10);        

         write_com(0x80+0x0d);
         write_data('.');                           
         delay2(10);
   
         write_com(0x80+0x0e);
     table[2]=tempp%100%10;
         write_data(table[2]+0x30);
         delay2(10);                                                         

         write_com(0x80+0x0f);
         write_data('C');                                    
         delay2(10);

         write_com(0x80+0x09);
         write_data('T');                                    
         delay2(10);

         write_com(0x80+0x0a);
         write_data(':');                                 
         delay2(10);

}

/超声波测距
void Conut(void)
        {  
         time=TH0*256+TL0;
          TH0=0;
         TL0=0;
  ES=0;
         S=(time*1.7)/100+1;                //算出来测距值单位是cm
 
         if((S>500)||S<10)           //超出测量范围显示***
         {         
          //flagg=0;
          write_com(0x80+0x40);
          write_data('c');                           
        //  delay2(10);
         
          write_com(0x80+0x41);
          write_data('j');                           
          //delay2(10);         

          write_com(0x80+0x42);
          write_data(':');                           
         // delay2(10);


          write_com(0x80+0x43);
          write_data('*');                           
        //  delay2(10);

          write_com(0x80+0x44);
          write_data('*');                           
          delay2(10);
          write_com(0x80+0x45);
          write_data('*');                           
         // delay2(10);

          write_com(0x80+0x46);
          write_data('M');                           
        //  delay2(10);        //显示M
          write_com(0x80+0x47);
          write_data('M');                           
        //  delay2(10);        //显示M
         }
      else
         {
 
           write_com(0x80+0x40);
          write_data('c');                           
        //  delay2(10);

          write_com(0x80+0x41);
          write_data('j');                           
        //  delay2(10);

          write_com(0x80+0x42);
          write_data(':');                           
         // delay2(10);
         
          write_com(0x80+0x43);
 table[3]=S/100;
          write_data(table[3]+0x30);  //  s/100                        
        //  delay2(10);

          write_com(0x80+0x44);
 table[4]=S%100/10;
          write_data(table[4]+0x30);  // s%100/10                        
        //  delay2(10);

          write_com(0x80+0x45);
  table[5]=S%100%10;
          write_data(table[5]+0x30); //  s                        
        //  delay2(10);

          write_com(0x80+0x46);
          write_data('C');                           
          //delay2(10);
         
          write_com(0x80+0x47);
          write_data('M');                           
         // delay2(10);
  
  
         //}
        }
}
//语音播报
void  yuyinbobao()
{   
  delay2(10);
   Uart_SendCMD(0x03 , 0 , 0x01);
  delay2(500);
  if(table[3]==0)
  {
  
   duqu(table[4]);
    delay2(100);
 Uart_SendCMD(0x03 , 0 , 0x0c);
  delay2(100);
  duqu(table[5]);
    delay2(100);
 Uart_SendCMD(0x03 , 0 , 0x01);
  delay2(100);
  }
  else if(table[3]!=0)
  {
  // delay2(50);
   duqu(table[3]);
    delay2(100);
 Uart_SendCMD(0x03 , 0 , 0x0d);
  delay2(100);   
   duqu(table[4]);
    delay2(100);
 Uart_SendCMD(0x03 , 0 , 0x0c);
  delay2(100);
   duqu(table[5]);
   delay2(100);
  }
     Uart_SendCMD(0x03 , 0 , 0x0e);
  delay2(100);
}
//显示按键输入值
void display1()
{
          write_com(0x80+0x00);
          write_data('S');                           
         // delay2(10);

          write_com(0x80+0x01);
          write_data('z');                           
          //delay2(10);

          write_com(0x80+0x02);
          write_data(':');                           
         // delay2(10);            
            
          write_com(0x80+0x03);
       table[6]=num/100;
          write_data(table[6]+0x30);    //  num/100                     
          //delay2(10);

          write_com(0x80+0x04);
       table[7]=num%100/10;
          write_data(table[7]+0x30); //  num%100/10                        
        //  delay2(10);

          write_com(0x80+0x05);
    table[8]=num%100%10;
          write_data(table[8]+0x30);   // num                       
          //delay2(10);

          write_com(0x80+0x06);
          write_data('C');                           
          //delay2(10);

          write_com(0x80+0x07);
          write_data('M');                           
        //  delay2(10);
}
//按键控制
void KEY()
{
   if(key1==0)
   {
             delay2(10);         
          if(key1==0)
          {         
              num+=10;
              if(num==500)num=0;
          }
           while(!key1);
           delay2(10);
         //  while(!key1);
   }

      if(key2==0)
   {
             delay2(10);
          if(key2==0)
          {         
             num-=10;
             if(num==0)num=0;
          }
           while(!key2);
           delay2(10);
          // while(!key2);
   }
}

/蜂鸣器报警
void BEEP()
{
   if(num<S)
   {
           
         
          feng=0;
          led=0;
          delay2(80);
   feng=1;
     delay2(3);
   }
  
   else
   {
   feng=1;
   led=1;
   }
}


void  StartModule()          //启动模块
  {
  
         
          _nop_(); _nop_(); _nop_();
          Trig=1;         //启动一次模块
          _nop_(); _nop_(); _nop_();
          _nop_(); _nop_(); _nop_();
          _nop_(); _nop_(); _nop_();
          _nop_(); _nop_(); _nop_();
          _nop_(); _nop_(); _nop_();
         
          Trig=0;  
           
  }

void tempchang(void)
{
           ds_reste();      
        delay2(1);
        ds_write(0xcc);      
        ds_write(0x44);      
}
 void T1_time()  interrupt 3   //T1中断用来扫描数码管和计800MS启动模块
  {
 TH1=0xf8;
 TL1=0x30;
// Display();
 // xian2();
 timer++;
 if(timer>=400)
 {
  timer=0;
  Trig=1;                 //800MS  启动一次模块
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  Trig=0;
 }
    KEY();  
  }
 
 void T0_time() interrupt 1   //T0中断用来计数器溢出,超过测距范围
  {
     TR0=0;
 
     EA=0;
  TH0=0;
         TL0=0;
  time=TH0*256+TL0;
  
  }
void main()
{        
    feng=1;
    led=1;
    TMOD=0x11;    //设T0为方式1,GATE=1
TH0=0;
TL0=0;         
TH1=0xf8;    //2MS定时
TL1=0x30;
ET0=1;             //允许T0中断
ET1=1;    //允许T1中断Ï
TR1=1;    //开启定时器
EA=1;    //开启总中断
         init();                          //1602的初始化                  
     //Serial_init() ;
         while(1)
         {
   // Serial_init() ;
             display1();        //显示按键值                           
            // KEY();                         //按键  
            
             BEEP();                //蜂鸣器
            //StartModule();
             while(!Echo);          //当RX为零时等待
             TR0=1;                         //开启计数
             while(Echo);    //当RX为1时计数并等待
             TR0=0;                     //关闭计数      
             Conut();             //距离显示
// TR1=1;
// ES=1;
//  yuyinbobao();
              delayms(10);   
             tempchang();    //温度开始转换
             display(read_temp());//温度显示
   delayms(50);
}         
}
搜索更多相关主题的帖子: num table 显示 void while 
2019-05-23 17:58
快速回复:C语言代码错误,求改正
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053862 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved