功能介绍:
0.本系统采用STC89C52作为单片机
1.采用LCD1602液晶显示当前时间和检测重量
2.按键可更改时间和设置超重报警阈值
3.采用HEX711和称重支架传感器获取物体重量信息,传入给单片机后再处理,一旦超重后,蜂鸣器报警
4.时钟芯片采用DS1302
主程序:
#include
#include "main.h"
enum MODE_DF{NORMAL, SET_ALARM}dispMode;
bit setFlag = 0;
unsigned char setIndex=0;
unsigned int alarmWeight = 5000;
unsigned char alarmWeightBuf;
unsigned long initialWeight = 0; //单位g
float objectWeight = 0; //单位g
bit dispFlag; //液晶刷新标志
void main()
{
dispMode = NORMAL;
// 定时器初始化
Timer0_Init();
DS1302_Init();
// 开机显示
LCD_Init();
LCD_DispStr(0, 0, "Electronic Scale");
LCD_DispStr(0, 1, "is initializing!");
DelayS(2);
initialWeight = HX711_GetInitialWeight();
LCD_Clear();
while(1)
{
if (dispFlag == 1 && setFlag == 0)
{
dispFlag = 0;
DS1302_ReadTime();
if (dispMode == NORMAL)
{
DispWeight();
DispTime(setIndex);
}
else if (dispMode == SET_ALARM)
{
DispAlarm();
}
}
KeyProcess();
}
}
void DispWeight()
{
unsigned char dispRow[16];
static unsigned char i=0;
if (i == 5) //计算5次测量的平均值
{
objectWeight = objectWeight / 5 - initialWeight;
if (objectWeight < 0)
{
objectWeight = 0;
}
objectWeight = (objectWeight * 10 / GAPVALUE);
if (objectWeight > alarmWeight)
{
BUZZER = 0;
}
else
{
BUZZER = 1;
}
sprintf(dispRow, "Weight: %6.3fkg", objectWeight / 1000);
LCD_DispStr(0, 1, dispRow);
i = 0;
objectWeight = 0;
}
else
{
TR0 = 0;
objectWeight = objectWeight + HX711_Read();
TR0 = 1;
i++;
}
}
void DispTime(unsigned char setIndex)
{
unsigned char dispRow[16];
switch (timeBufDec[7])
{
case 0: sprintf(dispRow, "%02d/%02d 7 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 1: sprintf(dispRow, "%02d/%02d 7 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 2: sprintf(dispRow, "%02d/%02d 1 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 3: sprintf(dispRow, "%02d/%02d 2 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 4: sprintf(dispRow, "%02d/%02d 3 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 5: sprintf(dispRow, "%02d/%02d 4 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 6: sprintf(dispRow, "%02d/%02d 5 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
case 7: sprintf(dispRow, "%02d/%02d 6 %02d:%02d:%02d", (int)timeBufDec[2], (int)timeBufDec[3], (int)timeBufDec[4], (int)timeBufDec[5], (int)timeBufDec[6]); break;
default: ;
}
LCD_DispStr(0, 0, dispRow);
switch (setIndex)
{
case 1: LCD_LocateXY(1 , 0); break;
case 2: LCD_LocateXY(4 , 0); break;
case 3: LCD_LocateXY(6 , 0); break;
case 4: LCD_LocateXY(9 , 0); break;
case 5: LCD_LocateXY(12, 0); break;
case 6: LCD_LocateXY(15, 0); break;
default: ;
}
}
void DispAlarm()
{
unsigned char dispRow[16];
sprintf(dispRow, "Alarm : %6.3fkg", (float)alarmWeight / 1000);
LCD_DispStr(0, 0, dispRow);
}
void KeyProcess()
{
if (!KEY_UP) //按键加
{
DelayMs(180);
if (!KEY_UP)
{
if (dispMode == NORMAL && setFlag == 1)
{
switch (setIndex)
{
case 1:
{
timeBufDec[2]++;
if (timeBufDec[2] >= 13)
{
timeBufDec[2] = 1;
}
break;
}
case 2:
{
timeBufDec[3]++;
if (timeBufDec[3] >= YDay(timeBufDec[1], timeBufDec[2]) + 1)
{
timeBufDec[3] = 1;
}
break;
}
case 3:
{
timeBufDec[7]++;
if (timeBufDec[7] > 7)
{
timeBufDec[7] = 1;
}
break;
}
case 4:
{
timeBufDec[4]++;
if (timeBufDec[4] >= 24)
{
timeBufDec[4] = 0;
}
break;
}
case 5:
{
timeBufDec[5]++;
if (timeBufDec[5] >= 60)
{
timeBufDec[5] = 0;
}
break;
}
case 6:
{
timeBufDec[6]++;
if (timeBufDec[6] >= 60)
{
timeBufDec[6] = 0;
}
break;
}
default:;
}
DispTime(setIndex);
}
if (dispMode == SET_ALARM)
{
alarmWeight = alarmWeight + 10;
if (alarmWeight > 5000)
{
alarmWeight = 5000;
}
}
}
//while (!KEY_UP);
}
if (!KEY_DOWN) //按键减
{
DelayMs(180);
if (!KEY_DOWN)
{
if (dispMode == NORMAL && setFlag == 1)
{
switch (setIndex)
{
case 1:
{
timeBufDec[2]--;
if (timeBufDec[2] < 1)
{
timeBufDec[2] = 12;
}
break;
}
case 2:
{
timeBufDec[3]--;
if (timeBufDec[3] < 1)
{
timeBufDec[3] = YDay(timeBufDec[1], timeBufDec[2]);
}
break;
}
case 3:
{
timeBufDec[7]--;
if (timeBufDec[7] < 1)
{
timeBufDec[7] = 7;
}
break;
}
case 4:
{
if (timeBufDec[4] == 0)
{
timeBufDec[4] = 24;
}
timeBufDec[4]--;
break;
}
case 5:
{
if (timeBufDec[5] == 0)
{
timeBufDec[5] = 60;
}
timeBufDec[5]--;
break;
}
case 6:
{
if (timeBufDec[6] == 0)
{
timeBufDec[6] = 60;
}
timeBufDec[6]--;
break;
}
default:;
}
DispTime(setIndex);
}
if (dispMode == SET_ALARM)
{
alarmWeight = alarmWeight - 10;
if (alarmWeight <= 0)
{
alarmWeight = 0;
}
}
}
//while (!KEY_DOWN);
}
if (!KEY_SET)
{
DelayMs(20);
if (!KEY_SET)
{
if (dispMode == NORMAL)
{
if (setFlag == 0)
{
setFlag = 1; //进入时间设置
setIndex = 1;
}
else
{
setIndex++;
if (setIndex >= 7)
{
setFlag = 0; //退出时间设置
setIndex = 0;
}
}
DispTime(setIndex);
}
}
仿真演示视频:
https://www.bilibili.com/video/BV16U4y1m7bc/
实物演示视频:
https://www.bilibili.com/video/BV13v4y1A7Wj/