#include "main.h"
#include "LCD12864.h"
#include "HX711.h"
unsigned long HX711_Buffer = 0;
unsigned int Weight_Maopi = 0,Weight_Shiwu = 0;
//****************************************************
//主函数
//****************************************************
void main()
{
LCD12864_Reset(); //初始化液晶
LCD12864_HAIZI_SET(); //设置为普通模式
LCD12864_NoWaitIdle_COM_Write(0x80); //指针设置
LCD12864_write_word("※※欢迎使用※※");
Delay_ms(1000); //延时1s,等待传感器稳定
Get_Maopi(); //称毛皮重量
LCD12864_NoWaitIdle_COM_Write(0x01); //清空
while(1)
{
Get_Weight(); //称重
if( Weight_Shiwu > 10000 )
{
LCD12864_NoWaitIdle_COM_Write(0x80); //指针设置
LCD12864_write_word("※※※超重※※※");
}
else if( Weight_Shiwu < 10000)
{
//显示当前重量
LCD12864_NoWaitIdle_COM_Write(0x80);
LCD12864_write_word("重量:");
if( Weight_Shiwu/10000 != 0)
{
LCD12864_Data_Write(Weight_Shiwu/10000 + 0x30);
}
else
{
LCD12864_Data_Write(' ');
}
LCD12864_Data_Write(Weight_Shiwu%10000/1000 + 0x30);
LCD12864_Data_Write('.');
LCD12864_Data_Write(Weight_Shiwu%1000/100 + 0x30);
LCD12864_Data_Write(Weight_Shiwu%100/10 + 0x30);
LCD12864_Data_Write(Weight_Shiwu%10 + 0x30);
LCD12864_write_word(" Kg");
LCD12864_NoWaitIdle_COM_Write(0x90);
LCD12864_write_word("重量:");
if( Weight_Shiwu*2/10000 != 0)
{
LCD12864_Data_Write(Weight_Shiwu*2/10000 + 0x30);
}
else
{
LCD12864_Data_Write(' ');
}
LCD12864_Data_Write(Weight_Shiwu*2%10000/1000 + 0x30);
LCD12864_Data_Write('.');
LCD12864_Data_Write(Weight_Shiwu*2%1000/100 + 0x30);
LCD12864_Data_Write(Weight_Shiwu*2%100/10 + 0x30);
LCD12864_Data_Write(Weight_Shiwu*2%10 + 0x30);
LCD12864_write_word(" ");
}
}
}
//****************************************************
//称重
//****************************************************
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/2.09+0.05); //计算实物的实际重量
//因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的2.15这个除数。
//当发现测试出来的重量偏大时,增加该数值。
//如果测试出来的重量偏小时,减小改数值。
//该数值一般在2.15附近调整之间。因传感器不同而定。
//+0.05是为了四舍五入百分位
Weight_Shiwu=Weight_Shiwu/2;
}
}
//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
unsigned char i = 0;
unsigned int Temp_Weight = 0;
Weight_Maopi = 0;
for( i = 0 ; i < 10 ; i++)
{
HX711_Buffer = HX711_Read();
Temp_Weight = HX711_Buffer/100;
if( Temp_Weight > Weight_Maopi)
{
Weight_Maopi = Temp_Weight;
}
}
}
//****************************************************
//MS延时函数(12M晶振下测试)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
} |