功能描述
电子秤是将检测与转换技术、计算机技术、信息处理、数字技术等技术综合一体的现代新型称重仪器。它与我们日常生活紧密结合息息相关。
电子称主要以单片机作为中心控制单元,通过称重传感器进行模数转换单元,在配以键盘、显示电路及强大软件来组成。电子称不但计量准确、快速方便,更重要的自动称重、数字显示,对人们生活的影响越来越大,广受欢迎。
本系统的设计主要从硬件电路设计,软件编程调试,实物焊接调试三部分进行详细阐述。硬件电路主要是基于单片机STC89C52为核心的控制单元实现数据的处理,采用压力传感器对数据进行采集,电子秤专用24位AD转换芯片HX711对传感器采集到的模拟量进行AD转换,转换后的数据送到单片机进行处理显示,数据显示由LCD1602液晶实现,液晶显示效果稳定无闪烁。
完整版 电路图和程序代码 下载地址
https://pan.baidu.com/s/1q64QUwCps34isnbEG7Nz1A?pwd=8888
部分代码展示
#include <reg52.h>
#include <intrins.h>
#include <string.h>
bit bdata flag_key;
#include "main.h"
#include "LCD1602.h"
#include "HX711.h"
#include "keyboard.h"
#include "eeprom52.h"
#define uchar unsigned char
#define uint unsigned int
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
unsigned int qupi=0;
long Weight_Shiwu = 0;
//键盘处理变量
unsigned char keycode;
unsigned char DotPos; //小数点标志及位置
uint GapValue,GapValue1;
unsigned char idata price; //单价,长整型值,单位为分
unsigned char idata money; //总价,长整型值,单位为分
//定义标识
volatile bit FlagTest = 0; //定时测试标志,每0.5秒置位,测完清0
volatile bit FlagKeyPress = 0; //有键按下标志,处理完毕清0
//校准参数
//因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该值可以为小数
//#define GapValue 349
sbit LED=P1^2;
volatile bit ClearWeighFlag = 0; //传感器调零标志位,清除0漂
//把数据保存到单片机内部eeprom中----------------------------------------------
void write_eeprom()
{
SectorErase(0x1000);
GapValue1=GapValue&0x00ff;
byte_write(0x2000, GapValue1);
GapValue1=(GapValue&0xff00)>>8;
byte_write(0x2001, GapValue1);
byte_write(0x2060, a_a);
}
//把数据从单片机内部eeprom中读出来--------------------------------------------
void read_eeprom()
{
GapValue = byte_read(0x2001);
GapValue = (GapValue<<8)|byte_read(0x2000);
a_a = byte_read(0x2060);
}
//开机自检eeprom初始化--------------------------------------------------------
void init_eeprom()
{
read_eeprom(); //先读
if(a_a != 1) //新的单片机初始单片机内问eeprom
{
GapValue = 3000;
a_a = 1;
write_eeprom(); //保存数据
}
}
//显示单价,单位为元,四位整数,两位小数--------------------------------------
void Display_Price()
{
LCD1602_write_com(0x8c);
LCD1602_write_data(price/100 + 0x30);
LCD1602_write_data(price%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(price%10 + 0x30);
}
//显示重量,单位kg,两位整数,三位小数----------------------------------------
void Display_Weight()
{
LCD1602_write_com(0x83);
LCD1602_write_data(Weight_Shiwu/1000 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(Weight_Shiwu%1000/100 + 0x30);
LCD1602_write_data(Weight_Shiwu%100/10 + 0x30);
LCD1602_write_data(Weight_Shiwu%10 + 0x30);
}
//显示总价,单位为元,四位整数,两位小数--------------------------------------
void Display_Money()
{
if (money>9999) //超出显示量程
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_word("---.-");
return;
}
if (money>=1000)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(money/1000 + 0x30);
LCD1602_write_data(money%1000/100 + 0x30);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
else if (money>=100)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_data(money%1000/100 + 0x30);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
else if(money>=10)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+7);
LCD1602_write_data(0x20);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10+ 0x30);
}
else
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+7);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+8);
LCD1602_write_data(0 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
}
//数据初始化-------------------------------------------------------------
void Data_Init()
{
price = 0;
DotPos = 0;
}
//定时器0初始化----------------------------------------------------------
void Timer0_Init()