去年省比赛准备51模块程序之一.
以上部分截自于厂家数据手册。
#include <STC12C5A60S2.H>
#include"lcd12864.h"
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
long Weight_Shiwu = 0;
long wsbeep=200; //系统设定值为200g
uint chazhi=20; //重量正常值在180-220之间
sbit HX711_SCK=P1^0;
sbit HX711_DOUT=P1^1;
#define GapValue 4500 //灵敏度
void Delay__hx711_us()
{
_nop_();
_nop_();
}
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
Delay__hx711_us();
HX711_SCK=0;
count=0;
while(HX711_DOUT);
for(i=0;i<24;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
Delay__hx711_us();
HX711_SCK=0;
return(count);
}
void Display_Weight()
{
wcode(0x80);
// lcd_wdat(Weight_Shiwu/10000);
wrnum(Weight_Shiwu%10000/1000);
wdata('.');
wrnum(Weight_Shiwu%1000/100);
wrnum(Weight_Shiwu%100/10);
wrnum(Weight_Shiwu%10);
wdata('k');
wdata('g');
}
void Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu=HX711_Buffer;
// Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取净重
if(Weight_Shiwu > 0)
{
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu*10/GapValue)-8530; //计算实物的实际重量
//
// if(Weight_Shiwu > 3000) //超重报警
// {
// Flag_ERROR = 1;
// }
// else
// {
// Flag_ERROR = 0;
// }
// }
// else
// {
// Weight_Shiwu = 0;
// // Flag_ERROR = 1; //负重报警
// }
}
delay(5);
Display_Weight();
}
//****************************************************
//获取毛皮重量
//****************************************************
//void Get_Maopi()
//{
// unsigned char clear;
//mm: Weight_Maopi_0 = HX711_Read();
// for(clear=0;clear<10;clear++)
// {
// Buzzer=1;
// LED=0;
// Delay_ms(100);
// LED=1;
// Delay_ms(100);
// }
// Weight_Maopi = HX711_Read();
// if(Weight_Maopi/GapValue!=Weight_Maopi_0/GapValue)
// goto mm;
// Buzzer=0;
// Delay_ms(500);
// Buzzer=1;
//}
void main()
{
initlcd();
while(1)
{
Get_Weight();
}
}