带语音播报的51单片机电子体重秤设计(源码+原理图+资料)

目录

1、概要

2、所用到的物料

3、实物照片

4、原理图

​5、模块使用说明

6、程序

1、概要

1、实现0~150KG的量程称量;
2、实时显示称量结果在1602上;
3、最小误差为0.01KG;
4、称量体重或物体时待结果稳定后,实现语音播报结果;
5、语音播报可手动关闭或打开;
6、由于传感器一致性差异,体重计算的系数可手动调节。

2、所用到的物料

1、STC89C52单片机
2、1602液晶
3、JQ8900-16P语音模块
4、HX711称重模块
5、洞洞板、下载线、晶振、电容、电阻、按键、排针等

3、实物照片

4、原理图

5、模块使用说明

        JQ8900-16P 选用的是 SOC 方案,集成了一个 16 位的 MCU,以及一个专门针对音频解码的ADSP,采用硬解码的方式,更加保证了系统的稳定性和音质。小巧尺寸更加满足嵌入其它产品的需求。
        SPI-flash 更换语音内容
        此芯片最大的优势在于能够灵活的更换 SPI-flash 内的语音内容,省去了传统语音芯片需要安装上位机更换语音的麻烦,SPI FLASH 直接模拟成 U 盘,跟拷贝 U 盘一样,非常方便。使得产品研发和生产变得便捷简单。一线串口控制模式、RX232 串口控制可选,为研发提供更多的选择性。 USB 数据线连接芯片,插上电脑,电脑会自动识别。

6、程序

完整代码点击顶部链接下载

#include <reg52.H>
#define uchar unsigned char
#define uint unsigned int
#define PX_1602 P1

sbit RS =P3^2;               
sbit RW =P3^3;
sbit E  =P3^4;

/*sbit RS =P1^3;               
sbit RW =P1^4;
sbit E  =P1^5;*/

sbit kset =P2^0;
sbit kup  =P2^2;
sbit kdown=P2^1;

sbit ADDO = P2^4;
sbit ADSK = P2^3;

sbit sda =P3^5;
sbit busy=P3^6;

uchar code str1[]={"Test weight:         "};
uchar code str2[]={"   00.0Kg  Voice"};
uchar code str11[]={"Welcome to use"};
uchar code str22[]={"Password:"};
uchar code set1[]={"Voice"};
uchar code set2[10]={"      "};
unsigned long buf=0,maopi=0,shiwu=0,shiwu2=0,shiwu_buf=0,gu=84866;
uchar  key,TSS,flag=0;
uint t;
int set_num,xishu=38;
double kg;

void ddelay(uint cnt)
{
        while(cnt--);
}

void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
           for(y=110;y>0;y--);        
}

void Delay1us (uint T )
{
    while ( T-- );
}

void SendData (uchar addr)
{
    uchar i;

    EA = 0;

    sda = 1;
    Delay1us ( 1000 );
    sda = 0;
    Delay1us ( 420 );

    for ( i = 0; i < 8; i++ )
    {
        sda = 1;
        if ( addr & 0x01 )
        {
            Delay1us ( 300 );
            sda = 0;
            Delay1us ( 100 );
        }
        else      
        {
            Delay1us ( 100 );
            sda = 0;
            Delay1us ( 300 );
        }
        addr >>= 1;
    }
    sda = 1;
    EA = 1;//????
}
               
void WriteCMD(uchar command)               
{
        RS = 0;
        RW = 0;
        ddelay(20);
        E = 1;
        ddelay(20);
        PX_1602 = command;
        ddelay(30);
        E = 0;
        ddelay(50);
}

void WriteDAT(uchar dat)               
{
        RS = 1;
        RW = 0;
        ddelay(20);
        E = 1;
        ddelay(20);
        PX_1602 = dat;
        ddelay(30);
        E = 0;
        ddelay(50);
}

void init_1602()                  
{
        uchar num;
        E=0;
        WriteCMD(0x38);
        WriteCMD(0x38);         
        WriteCMD(0x0c);        
        WriteCMD(0x06);      
        WriteCMD(0x01);        
        WriteCMD(0x80);      

  for(num=0;num<16;num++)
        {
                WriteDAT(str1[num]);
                //if(num==4){WriteCMD(0x80+4);WriteDAT(0xd0+15);}        
                delay(20);
  }
        
        WriteCMD(0x80+0x40);
        for(num=0;num<16;num++)
        {
                WriteDAT(str2[num]);
                delay(20);
          }
        delay(200);                                  //yan shi 100us(12Mjingzheng)
}

void password()
{
        uchar num,kk=1;
        uchar ps1,ps2,ps3,ps4,ps5,ps6,ps7;
        E=1;
        WriteCMD(0x38);
        WriteCMD(0x38);         
        WriteCMD(0x0c);        
        WriteCMD(0x06);      
        WriteCMD(0x01);        
        WriteCMD(0x80);

  for(num=0;num<14;num++)
        {
                WriteDAT(str11[num]);
                delay(20);
  }        
        WriteCMD(0x80+0x40);
        for(num=0;num<9;num++)
        {
                WriteDAT(str22[num]);
                delay(20);
          }
        delay(200);                                  //yan shi 100us(12Mjingzheng)
        WriteCMD(0x80+0x49);
        WriteCMD(0x0f);
        while(1)
        {
                if(kset==0)
                {
                        delay(10);
                        if(kset==0)
                        {
                                kk++;
                                while(kset==0);
                                WriteCMD(0x0f);
                                switch (kk)
                                {
                                        case 1 : WriteCMD(0x80+0x49); break;
                                        case 2 : WriteCMD(0x80+0x4a); break;
                                        case 3 : WriteCMD(0x80+0x4b); break;
                                        case 4 : WriteCMD(0x80+0x4c); break;
                                        case 5 : WriteCMD(0x80+0x4d); break;
                                        case 6 : WriteCMD(0x80+0x4e); break;
                                        case 7 : WriteCMD(0x80+0x4f); break;
                                        case 8 : kk=1;WriteCMD(0x80+0x49); break;
                                }
                        }
                }
                if(kk==1)
                {
                        if(ps1==2&&ps2==0&&ps3==1&&ps4==8&&ps5==6&&ps6==6&&ps7==6)break;
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        if(kdown==0)break;
                                        ps1++;if(ps1>=10)ps1=0;
                                        WriteCMD(0x80+0x49);
                                        WriteDAT(0x30+ps1);
                                        WriteCMD(0x80+0x49);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(kup==0)break;
                                        if(ps1>0)ps1--;
                                        else ps1=9;
                                        WriteCMD(0x80+0x49);
                                        WriteDAT(0x30+ps1);
                                        WriteCMD(0x80+0x49);
                                        WriteCMD(0x0e);
                                }
                        }
                }
                if(kk==2)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps2++;if(ps2>=10)ps2=0;
                                        WriteCMD(0x80+0x4a);
                                        WriteDAT(0x30+ps2);
                                        WriteCMD(0x80+0x4a);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps2>0)ps2--;
                                        else ps2=9;
                                        WriteCMD(0x80+0x4a);
                                        WriteDAT(0x30+ps2);
                                        WriteCMD(0x80+0x4a);
                                        WriteCMD(0x0e);
                                }
                        }
                }
                if(kk==3)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps3++;if(ps3>=10)ps3=0;
                                        WriteCMD(0x80+0x4b);
                                        WriteDAT(0x30+ps3);
                                        WriteCMD(0x80+0x4b);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps3>0)ps3--;
                                        else ps3=9;
                                        WriteCMD(0x80+0x4b);
                                        WriteDAT(0x30+ps3);
                                        WriteCMD(0x80+0x4b);
                                        WriteCMD(0x0e);
                                }
                        }
                }                        
                if(kk==4)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps4++;if(ps4>=10)ps4=0;
                                        WriteCMD(0x80+0x4c);
                                        WriteDAT(0x30+ps4);
                                        WriteCMD(0x80+0x4c);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps4>0)ps4--;
                                        else ps4=9;
                                        WriteCMD(0x80+0x4c);
                                        WriteDAT(0x30+ps4);
                                        WriteCMD(0x80+0x4c);
                                        WriteCMD(0x0e);
                                }
                        }
                }
                if(kk==5)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps5++;if(ps5>=10)ps5=0;
                                        WriteCMD(0x80+0x4d);
                                        WriteDAT(0x30+ps5);
                                        WriteCMD(0x80+0x4d);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps5>0)ps5--;
                                        else ps5=9;
                                        WriteCMD(0x80+0x4d);
                                        WriteDAT(0x30+ps5);
                                        WriteCMD(0x80+0x4d);
                                        WriteCMD(0x0e);
                                }
                        }
                }
                if(kk==6)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps6++;if(ps6>=10)ps6=0;
                                        WriteCMD(0x80+0x4e);
                                        WriteDAT(0x30+ps6);
                                        WriteCMD(0x80+0x4e);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps6>0)ps6--;
                                        else ps6=9;
                                        WriteCMD(0x80+0x4e);
                                        WriteDAT(0x30+ps6);
                                        WriteCMD(0x80+0x4e);
                                        WriteCMD(0x0e);
                                }
                        }
                }
                if(kk==7)
                {
                        if(kup==0)
                        {
                                delay(100);
                                if(kup==0)
                                {
                                        ps7++;if(ps7>=10)ps7=0;
                                        WriteCMD(0x80+0x4f);
                                        WriteDAT(0x30+ps7);
                                        WriteCMD(0x80+0x4f);
                                        WriteCMD(0x0e);
                                }
                        }
                        if(kdown==0)
                        {
                                delay(100);
                                if(kdown==0)
                                {
                                        if(ps7>0)ps7--;
                                        else ps7=9;
                                        WriteCMD(0x80+0x4f);
                                        WriteDAT(0x30+ps7);
                                        WriteCMD(0x80+0x4f);
                                        WriteCMD(0x0e);
                                }
                        }
                }                                                                                
        }
}

/*void write_kg(unsigned long date)
{
        unsigned long shi,ge,bai,qian;
        if(date>9999)date=9999;
        qian=date/1000;
        bai=date%1000/100;
        shi=date%100/10;
        ge=date%10;
        WriteCMD(0x80+0x42);
        WriteDAT(0x30+qian);
        WriteDAT(0x30+bai);
        WriteDAT(0x30+shi);
        WriteDAT(0x30+ge);
}*/

void write_kgg(unsigned long date)
{
        unsigned long shi,ge,bai;
        if(date>99999)date=99999;
        (unsigned long)date=(float)date/100;

        bai=date/100;
        shi=date%100/10;
        ge=date%10;        
        WriteCMD(0x80+0x43);        
        WriteDAT(0x30+bai);
        WriteDAT(0x30+shi);
        WriteCMD(0x80+0x46);        
        WriteDAT(0x30+ge);        
}

void write_n(uint date)
{
        uchar shi,ge,bai;
        bai=date/100;
        shi=date%100/10;
        ge=date%10;
        WriteCMD(0x8d);
        WriteDAT(0x30+bai);
        WriteDAT(0x30+shi);
        WriteDAT(0x30+ge);
}

void write_xishu(uint date)
{
        uchar shi,ge;
        shi=date/10;
        ge=date%10;
        WriteCMD(0x80+0x40);
        WriteDAT(0x30+shi);
        WriteDAT(0x30+ge);
}

void delay_us(uint z)
{
        while(z--);
}

/*unsigned long get_kg(void)
{  
        unsigned long Count;  
        unsigned char i;  
        ADDO=1;
        delay_us(2);        
        ADSK=0;       //使能AD(PD_SCK 置低)  
        Count=0;  
        while(ADDO);    //AD转换未结束则等待,否则开始读取  
        for (i=0;i<24;i++)  
        {   
                ADSK=1;     //PD_SCK 置高(发送脉冲)   
                Count=Count<<1; //下降沿来时变量Count左移一位,右侧补零   
                ADSK=0;     //PD_SCK 置低   
                if(ADDO)
                Count++;  
        }  
        ADSK=1;  
        Count=Count^0x800000;//第25个脉冲下降沿来时,转换数据
        delay_us(2);  
        ADSK=0;  

        shiwu=Count/100-gu;
        //shiwu=(unsigned int)((float)shiwu/4.22+0.05);
        return(shiwu);
}*/

unsigned long hx711_read(void)
{
        unsigned long Count;  
        unsigned char i;  
        ADDO=1;
        delay_us(2);
        ADSK=0;       //使能AD(PD_SCK 置低)  
        Count=0;  
        while(ADDO);    //AD转换未结束则等待,否则开始读取  
        for (i=0;i<24;i++)  
        {   
                ADSK=1;     //PD_SCK 置高(发送脉冲)   
                Count=Count<<1; //下降沿来时变量Count左移一位,右侧补零   
                ADSK=0;     //PD_SCK 置低   
                if(ADDO)
                Count++;  
        }  
        ADSK=1;  
        Count=Count^0x800000;//第25个脉冲下降沿来时,转换数据  
        delay_us(2);
        ADSK=0;  
        return(Count);

}

void get_maopi()
{
        buf=hx711_read();
        maopi=buf/100;        
}

void gei_weight()
{
//        uint n;
        buf=hx711_read();
        buf=buf/100;
        if(buf>maopi)
        {
                shiwu=buf-maopi;
                shiwu=(unsigned long)((double)shiwu*xishu/10.0);
                //shiwu=(unsigned int)((float)shiwu/3.0+0.05);            //需要矫正4.22数值        
        }
        if(shiwu>99000||buf<maopi-30)
        {
                write_n(999);
                while(1);
        }
        //shiwu-=(gu/10);        
}

void scankey()
{
        uchar num;
        if(kset==0)
        {
                delay(10);
                if(kset==0)
                {
                        key++;
                        while(kset==0);
                        write_xishu(xishu);
                        switch (key)
                        {
                                case 1 : WriteCMD(0x80+0x4f);WriteCMD(0x0f); break;
                                case 2 : WriteCMD(0x80+0x41); break;
                                case 3 : WriteCMD(0x80+0x40);                        
                                        for(num=0;num<5;num++)
                                        {
                                                WriteDAT(set2[num]);                                       
                                                delay(20);
                                        }key=0;        WriteCMD(0x0c);break;
                        }
                }
        }
                        
        if(key==1)
        {
                if(kup==0)
                {
                        delay(10);
                        if(kup==0)
                        {
                                set_num++;if(set_num>=2)set_num=0;
                                while(!kup);
                                WriteCMD(0x80+0x4b);
                                for(num=0;num<5;num++)
                                {
                                        if(set_num==0)
                                        {WriteDAT(set1[num]);flag=1;}
                                        if(set_num==1)
                                        {WriteDAT(set2[num]);flag=0;}                                                                                
                                        delay(20);
                                }
                                WriteCMD(0x80+0x4f);
                                WriteCMD(0x0f);
                        }
                }
                if(kdown==0)
                {
                        delay(10);
                        if(kdown==0)
                        {
                                set_num--;if(set_num<0)set_num=1;
                                while(!kdown);
                                WriteCMD(0x80+0x4b);
                                for(num=0;num<5;num++)
                                {
                                        if(set_num==0)
                                        {WriteDAT(set1[num]);flag=1;}
                                        if(set_num==1)
                                        {WriteDAT(set2[num]);flag=0;}                                                
                                        delay(20);
                                }
                                WriteCMD(0x80+0x4f);
                                WriteCMD(0x0f);
                        }
                }
        }
        if(key==2)
        {
                if(kup==0)
                {
                        delay(10);
                        if(kup==0)
                        {
                                xishu++;if(xishu>=99)xishu=0;
                                write_xishu(xishu);
                                WriteCMD(0x80+0x41);
                                WriteCMD(0x0f);
                        }
                }
                if(kdown==0)
                {
                        delay(10);
                        if(kdown==0)
                        {
                                xishu--;if(xishu<0)xishu=99;
                                write_xishu(xishu);
                                WriteCMD(0x80+0x41);
                                WriteCMD(0x0f);
                        }
                }
        }               
}

void bobao()
{
        uint buff;
        uchar ge,shi,bai;

上一篇:[STM32] Keil MDK 新建工程编译不通过(warning: #2803-D和Error: L6218E)解决方法备忘


下一篇:微信小程序多图列表页面性能问题为什么会出现?如何解决?