TA的每日心情 | 衰 2018-11-8 22:22 |
---|
签到天数: 19 天 连续签到: 1 天 [LV.4]偶尔看看III
版主
- 积分
- 211
|
本帖最后由 heishijie 于 2018-9-19 12:52 编辑
需要配件:
5v继电器模块
12v电磁阀(12v独立电源)
HX711模块
51单片机
5v电源线
压力模块
-------------------------------------------------------------------以下是单片机程序源码
#include "main.h"
#include "LCD1602.h"
#include "HX711.h"
#define uchar unsigned char //无符号字符型 宏定义 变量范围0~255
#define uint unsigned int //无符号整型 宏定义 变量范围0~65535
#include "eeprom52.h"
//定义变量
unsigned char KEY_NUM = 0; //用来存放按键按下的键值
unsigned long HX711_Buffer = 0; //用来存放HX711读取出来的数据
unsigned long Weight_Maopi = 0; //用来存放毛皮数据
long Weight_Shiwu = 0; //用来存放实物重量
unsigned char Max_Value; //用来存放设置最大值
unsigned char state = 0; //用来存放设置状态
unsigned char Blink_Speed = 0;
#define Blink_Speed_Max 6 //该值可以改变设置指针闪烁频率
uint GapValue= 106.5; //传感器曲率
unsigned char num1,num2,times; //num1 和 num2 是存储在单片机eeprom内部的。
sbit JDQ=P3^0; //变量JDQ等于继电器p3^0
//报警值,单位是g
#define AlarmValue 40000
/******************把数据保存到单片机内部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);
}
/******************把数据从单片机内部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;
}
/**************开机自检eeprom初始化*****************/
void init_eeprom()
{
read_eeprom(); //先读
if(a_a != 40) //新的单片机初始单片机内问eeprom
{
a_a =40;
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<40) { Max_Value++;}
write_eeprom(); //保存数据
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(); //保存数据
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( 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(); //保存数据
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(); //保存数据
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);
}
}
}
//****************************************************
//主函数
//****************************************************
void main()
{
Init_LCD1602(); //初始化LCD1602
init_eeprom(); //读eeprom数据
LCD1602_write_com(0x80); //设置LCD1602指针
LCD1602_write_word("Welcome to use!");
Get_Maopi();
Get_Maopi();
Delay_ms(2000); //延时2s
Get_Maopi();
Get_Maopi(); //称毛皮重量 //多次测量有利于HX711稳定
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();
//显示当前重量
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");
//超限报警
if(Weight_Shiwu/1000 >= Max_Value || Weight_Shiwu >= AlarmValue) //超过设置最大值或者传感器本身量程最大值报警
{
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); //计算实物的实际重量
}
else
{
Weight_Shiwu = 0;
}
}
//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
//****************************************************
//MS延时函数(12M晶振下测试)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
//****************************************************
//蜂鸣器程序
//****************************************************
void Buzzer_Di()
{
Buzzer = 0;
Delay_ms(10);
Buzzer = 1;
Delay_ms(10);
}
--------------------------------------------------------------------------------------------------------------------
代码我都给你们了·········研究了7天的51单片机哎········给自己厂里升级一些装备······等有时间给你多整几个好用的东西~·······
哎.............感觉自己学太多东西脑子好乱~··············还是安逸点·~~··提示本论坛朋友·······专注一个东西研究就可以了~···
多多支持叫兽······多搞搞论坛vip会员········
我给你一些好的文章或者资源·······
|
|