標(biāo)題:
基于單片機(jī)+HX711的20kg電子秤的相關(guān)程序
[打印本頁(yè)]
作者:
萌新瑟瑟
時(shí)間:
2018-12-3 23:20
標(biāo)題:
基于單片機(jī)+HX711的20kg電子秤的相關(guān)程序
51單片機(jī)設(shè)計(jì)的電子秤
20kg電子秤程序+上線報(bào)警設(shè)置(加繼電器)
單片機(jī)源程序如下:
/************************************************
功能:
按鍵K1為去皮功能,K2和K3是修改報(bào)警參數(shù)值。K2加,K3減。當(dāng)所測(cè)重量大于設(shè)置的報(bào)警值時(shí),P3.0驅(qū)動(dòng)繼電器工作,繼電器可以接報(bào)警裝置等。
校正方法:如果所測(cè)重量偏小。則按住K3鍵不放,再按下K1鍵。 直到顯示重量為正確重量時(shí)再放開K1和K3按鍵。
如果所測(cè)重量偏大。則按住K2鍵不放,再按下K1鍵。 直到顯示重量為正確重量時(shí)再放開K1和K2按鍵。。 校正系數(shù)具有斷電保存功能。
*************************************************/
#include "main.h"
#include "LCD1602.h"
#include "HX711.h"
#define uchar unsigned char //無符號(hào)字符型 宏定義 變量范圍0~255
#define uint unsigned int //無符號(hào)整型 宏定義 變量范圍0~65535
#include "eeprom52.h"
//定義變量
unsigned char KEY_NUM = 0; //用來存放按鍵按下的鍵值
unsigned long HX711_Buffer = 0; //用來存放HX711讀取出來的數(shù)據(jù)
unsigned long Weight_Maopi = 0; //用來存放毛皮數(shù)據(jù)
long Weight_Shiwu = 0; //用來存放實(shí)物重量
unsigned char Max_Value; //用來存放設(shè)置最大值
unsigned char state = 0; //用來存放設(shè)置狀態(tài)
unsigned char Blink_Speed = 0;
#define Blink_Speed_Max 6 //該值可以改變?cè)O(shè)置指針閃爍頻率
uint GapValue= 104; //傳感器曲率
unsigned char num1,num2,times; //num1 和 num2 是存儲(chǔ)在單片機(jī)eeprom內(nèi)部的。
sbit JDQ=P3^0;
//報(bào)警值,單位是g
#define AlarmValue 20000
/******************把數(shù)據(jù)保存到單片機(jī)內(nèi)部eeprom中******************/
void write_eeprom()
{
num1=GapValue/256;
num2=GapValue%256;
SectorErase(0x2000);
byte_write(0x2001,Max_Value);
byte_write(0x2002, num1);
byte_write(0x2003, num2);
byte_write(0x2060, a_a);
}
/******************把數(shù)據(jù)從單片機(jī)內(nèi)部eeprom中讀出來*****************/
void read_eeprom()
{
Max_Value = byte_read(0x2001);
num1 = byte_read(0x2002);
num2 = byte_read(0x2003);
a_a = byte_read(0x2060);
GapValue= num1*256+num2;
}
/**************開機(jī)自檢eeprom初始化*****************/
void init_eeprom()
{
read_eeprom(); //先讀
if(a_a != 20) //新的單片機(jī)初始單片機(jī)內(nèi)問eeprom
{
a_a =20;
Max_Value = 10;
GapValue= 106;
write_eeprom();
}
}
//掃描按鍵
void Scan_Key()
{
if(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 )
{
Delay_ms(5);
if(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 )
{
while(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 );
Get_Maopi();
Buzzer_Di();
}
}
if(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 )
{
Delay_ms(5);
if(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 )
{
while(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 );
if(Max_Value<20) { Max_Value++;}
write_eeprom(); //保存數(shù)據(jù)
Buzzer_Di();
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("JDQ=");
LCD1602_write_data(Max_Value/100 + 0x30);
LCD1602_write_data(Max_Value%100/10 + 0x30);
LCD1602_write_data(Max_Value%10 + 0x30);
}
}
if(KEY3 == 0 && KEY1 ==1 && KEY2 ==1 )
{
Delay_ms(5);
if(KEY3 == 0 && KEY1 ==1 && KEY2 ==1)
{
while(KEY3 == 0 && KEY1 ==1 && KEY2 ==1);
if(Max_Value>1) { Max_Value--; }
write_eeprom(); //保存數(shù)據(jù)
Buzzer_Di();
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("JDQ=");
LCD1602_write_data(Max_Value/100 + 0x30);
LCD1602_write_data(Max_Value%100/10 + 0x30);
LCD1602_write_data(Max_Value%10 + 0x30);
}
}
//=======================對(duì)實(shí)物進(jìn)行重量校正================================
if( KEY1 == 0 && KEY2 ==0)
{
Delay_ms(25);
if( KEY1 == 0 && KEY2 ==0)
{
times=0;
Buzzer =0;
Delay_ms(25);
Buzzer =1;
if(GapValue<999) { GapValue++; }
write_eeprom(); //保存數(shù)據(jù)
LCD1602_write_com(0x80+0x40+11);
LCD1602_write_word("K=");
LCD1602_write_data(GapValue/100 + 0x30);
LCD1602_write_data(GapValue%100/10 + 0x30);
LCD1602_write_data(GapValue%10 + 0x30);
}
}
if(KEY1 == 0 && KEY3 ==0)
{
Delay_ms(25);
if(KEY1 == 0 && KEY3 ==0)
{
times=0;
Buzzer =0;
Delay_ms(25);
Buzzer =1;
if(GapValue>1) { GapValue--; }
write_eeprom(); //保存數(shù)據(jù)
LCD1602_write_com(0x80+0x40+11);
LCD1602_write_word("K=");
LCD1602_write_data(GapValue/100 + 0x30);
LCD1602_write_data(GapValue%100/10 + 0x30);
LCD1602_write_data(GapValue%10 + 0x30);
}
}
}
//****************************************************
//主函數(shù)
//****************************************************
void main()
{
Init_LCD1602(); //初始化LCD1602
init_eeprom(); //讀eeprom數(shù)據(jù)
LCD1602_write_com(0x80); //設(shè)置LCD1602指針
LCD1602_write_word("Welcome to use!");
Get_Maopi();
Get_Maopi();
Delay_ms(2000); //延時(shí)2s
Get_Maopi();
Get_Maopi(); //稱毛皮重量 //多次測(cè)量有利于HX711穩(wěn)定
LCD1602_write_com(0x01); //清屏
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("JDQ=");
LCD1602_write_data(Max_Value/100 + 0x30);
LCD1602_write_data(Max_Value%100/10 + 0x30);
LCD1602_write_data(Max_Value%10 + 0x30);
LCD1602_write_word("Kg");
Buzzer =0;
Delay_ms(25);
Buzzer =1;
while(1)
{
Scan_Key();
Get_Weight();
//顯示當(dāng)前重量
LCD1602_write_com(0x80);
LCD1602_write_word("Weight=");
LCD1602_write_data(Weight_Shiwu/10000 + 0x30);
LCD1602_write_data(Weight_Shiwu%10000/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);
LCD1602_write_word("Kg");
//超限報(bào)警
if(Weight_Shiwu/1000 >= Max_Value || Weight_Shiwu >= AlarmValue) //超過設(shè)置最大值或者傳感器本身量程最大值報(bào)警
{
JDQ = 0; Buzzer=0;
}
else
{
JDQ = 1; Buzzer=1;
}
times++;
if(times>=50)
{
times=0;
LCD1602_write_com(0x80+0x40+11);
LCD1602_write_word(" ");
}
}
}
//****************************************************
//稱重
//****************************************************
void Get_Weight()
{
Weight_Shiwu = HX711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取凈重
if(Weight_Shiwu >= 0)
{
Weight_Shiwu = (unsigned long)((float)Weight_Shiwu/GapValue); //計(jì)算實(shí)物的實(shí)際重量
}
else
{
Weight_Shiwu = 0;
}
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
附件2:20kg的壓力傳感器例程.rar
(79.21 KB, 下載次數(shù): 67)
2018-12-3 23:19 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
admin
時(shí)間:
2018-12-4 04:04
補(bǔ)全原理圖或者詳細(xì)說明一下電路連接即可獲得100+黑幣
作者:
玉簫戲鳳鸞
時(shí)間:
2023-3-26 22:02
大哥,補(bǔ)全原理圖或者詳細(xì)說明一下電路連接唄
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1