使用51单片机制作一个电子秤,所需器件:全桥传感器,hx711芯片,数码管,at89c52.
其中一定要注意传感器和hx711芯片的接法,我是用A通道,DT接P3.0,SCK接P3.1。
部分代码如下,有点乱,懒得整理了。
我的传感器有点问题,加上上面的光碟计算为初始重量0000.
制作出来的实物图:
单片机源程序如下:
- #include "main.h"
- #include "HX711.h"
- /////共阴数码管段选//////////////////////////////////////////////
- unsigned char table[22]={0x3F,0x06,0x5B,0x4F,0x66,
- 0x6D,0x7D,0x07,0x7F,0x6F,
- 0x77,0x7C,0x39,0x5E,0x79,0x71,
- 0x40,0x38,0x76,0x00,0xff,0x37};//'-',L,H,灭,全亮,n 16-21
- float m__1=16.752;
- unsigned long HX711_Buffer = 0;
- unsigned int Weight_Maopi = 0,Weight_Shiwu = 0;
- char Price_Count = 0;
- unsigned char KEY_NUM = 0,j=0;
- unsigned char Price_Buffer[3] = {0x00,0x00,0x00};
- unsigned long Money = 0;
- bit Flag_OK = 0;
- unsigned int a=0;
- unsigned int b=0;
- unsigned int c=0;
- unsigned int d=0;
- void delay(unsigned int ms)
- {
- unsigned char x;
- for(ms;ms>0;ms--)
- for(x=112;x>0;x--);
- }
- void display(unsigned int zl)//显示重量 单位g
- {
-
- w1=1;w2=0;w3=0;w4=0;
- P0=table[zl/1000];
- delay(20);
- w1=0; //第1位
-
- w1=0;w2=1;w3=0;w4=0;
- P0=table[zl%1000/100];
- delay(20);//第2位
- w2=0;
- w1=0;w2=0;w3=1;w4=0;
- P0=table[zl%100/10];
- delay(20);//第3位
- w3=0;
-
- w1=0;w2=0;w3=0;w4=1;
- P0=table[zl%10];
- delay(20);
- w4=0;//第4位
- }
- void Get_Maopi()
- {
- HX711_Buffer = HX711_Read();
- Weight_Maopi = HX711_Buffer/100;
- }
- //****************************************************
- //主函数
- //****************************************************
- void main()
- {
- Get_Maopi(); //称毛皮重量
-
- while(1)
- {
- if( Flag_OK == 0)
- {
- Get_Weight();
- for(j=0;j<100;j++)display(Weight_Shiwu);//显示重量 单位g
-
- }
- if(key1==0) //
- {
- while(!a)
- {a=Weight_Shiwu;}
- for(j=0;j<100;j++)display(a);
- }
- if(key2==0) //
- {
- while(!b)
- {b=Weight_Shiwu;}
- for(j=0;j<100;j++)display(b);
- }
- if(key3==0) //
- {
- while(!c)
- {c=Weight_Shiwu;}
- for(j=0;j<100;j++)display(c);
- }
- if(key4==0) //
- {
- while(!d)
- {d=Weight_Shiwu;}
- for(j=0;j<100;j++)display(d);
- }
- }
- }
- //****************************************************
- //称重
- //****************************************************
- void Get_Weight()
- {
- HX711_Buffer = HX711_Read();
- HX711_Buffer = HX711_Buffer/100;
- if(HX711_Buffer > Weight_Maopi)
- {
- Weight_Shiwu = HX711_Buffer;
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
-
- Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/m__1+0.05); //计算实物的实际重量
- //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的4.30这个除数。
- //当发现测试出来的重量偏大时,增加该数值。
- //如果测试出来的重量偏小时,减小改数值。
- //该数值一般在4.0-5.0之间。因传感器不同而定。
- //+0.05是为了四舍五入百分位
- Buzzer = 1; //关闭警报
- }
- // if(Weight_Shiwu>1000)Buzzer=0;
- //
- // if(Weight_Shiwu < 2)Weight_Shiwu=0;
- //
- // else if(HX711_Buffer < Weight_Maopi - 30)
- // {
- // Buzzer = 0; //负重量报警
- // }
- // else if(HX711_Buffer > Weight_Maopi + 24970) //大于5Kg的最大量程,报警
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
蓝色 电子称程序 - 副本.zip
(37.62 KB, 下载次数: 330)
|