#include <reg52.h>
#include <intrins.h>
#include <string.h>
bit bdata flag_key;
#include "main.h"
#include "LCD1602.h"
#include "HX711.h"
#include "keyboard.h"
#include "eeprom52.h"
#define uchar unsigned char
#define uint unsigned int
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
unsigned long Weight_Maopi_0 = 0;
unsigned int qupi=0;
long Weight_Shiwu = 0;
unsigned char keycode;
unsigned char DotPos;
uint GapValue,GapValue1;
unsigned char idata price;
unsigned char idata money;
volatile bit FlagTest = 0;
volatile bit FlagKeyPress = 0;
sbit LED=P1^1;
volatile bit ClearWeighFlag = 0;
void write_eeprom()
{
SectorErase(0x1000);
GapValue1=GapValue&0x00ff;
byte_write(0x2000, GapValue1);
GapValue1=(GapValue&0xff00)>>8;
byte_write(0x2001, GapValue1);
byte_write(0x2060, a_a);
}
void read_eeprom()
{
GapValue = byte_read(0x2001);
GapValue = (GapValue<<8)|byte_read(0x2000);
a_a = byte_read(0x2060);
}
void init_eeprom()
{
read_eeprom();
if(a_a != 1)
{
GapValue = 3500;
a_a = 1;
write_eeprom();
}
}
void Display_Price()
{
LCD1602_write_com(0x8c);
LCD1602_write_data(price/100 + 0x30);
LCD1602_write_data(price%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(price%10 + 0x30);
}
void Display_Weight()
{
LCD1602_write_com(0x83);
LCD1602_write_data(Weight_Shiwu/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);
}
void Display_Money()
{
if (money>9999)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_word("---.-");
return;
}
if (money>=1000)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(money/1000 + 0x30);
LCD1602_write_data(money%1000/100 + 0x30);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
else if (money>=100)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_data(money%1000/100 + 0x30);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
else if(money>=10)
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+7);
LCD1602_write_data(0x20);
LCD1602_write_data(money%100/10 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10+ 0x30);
}
else
{
LCD1602_write_com(0x80+0x40+6);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+7);
LCD1602_write_data(0x20);
LCD1602_write_com(0x80+0x40+8);
LCD1602_write_data(0 + 0x30);
LCD1602_write_data('.');
LCD1602_write_data(money%10 + 0x30);
}
}
void Data_Init()
{
price = 0;
DotPos = 0;
}
void Timer0_Init()
{
ET0 = 1;
TMOD = 1;
TL0 = 0xb0;
TH0 = 0x3c;
TR0 = 1;
}
void Timer0_ISR (void) interrupt 1 using 0
{
uchar Counter;
TL0 = 0xb0;
TH0 = 0x3c;
Counter ++;
if (Counter >= 10)
{
FlagTest = 1;
Counter = 0;
}
}
void KeyPress(uchar keycode)
{
switch (keycode)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if (DotPos == 0)
{
if (price<100)
{
price=price*10+keycode*10;
}
}
else if (DotPos==1)
{
price=price+keycode;
DotPos=2;
}
Display_Price();
break;
case 10:
if(qupi==0)
qupi=Weight_Shiwu;
else
qupi=0;
Display_Price();
DotPos = 0;
break;
case 11:
price=0;
DotPos=0;
Display_Price();
break;
case 12:
if(GapValue<10000)
GapValue++;
break;
case 13:
if(GapValue>1)
GapValue--;
break;
case 15:
DotPos = 1;
break;
}
}
void main()
{
init_eeprom();
Init_LCD1602();
EA = 0;
Data_Init();
Timer0_Init();
EA = 1;
LCD1602_write_com(0x80);
LCD1602_write_word(" Welcome To Use ");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("Electronic Scale");
Get_Maopi();
LCD1602_write_com(0x80);
LCD1602_write_word("WE:0.000 PR:00.0");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("MONEY: 0.00 ");
Display_Price();
while(1)
{
if (FlagTest==1)
{
Get_Weight();
FlagTest = 0;
}
keycode = Getkeyboard();
if (keycode<16)
{
KeyPress(keycode);
Buzzer=0;
Delay_ms(100);
Buzzer=1;
while(keycode<16)
{
if(keycode==12||keycode==13)
{
Buzzer=0;
Delay_ms(10);
Buzzer=1;
KeyPress(keycode);
Get_Weight();
flag_key=1;
}
keycode = Getkeyboard();
}
write_eeprom();
}
}
}
void Get_Weight()
{
Weight_Shiwu = HX711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi;
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu*10/GapValue)-qupi;
if(Weight_Shiwu > 5000)
{
Buzzer = !Buzzer;
LED=!LED;
LCD1602_write_com(0x83);
LCD1602_write_word("-.---");
}
else
{
if(Weight_Shiwu==0)
LED=1;
else if(Weight_Shiwu>0)
LED=0;
Buzzer = 1;
Display_Weight();
money = Weight_Shiwu*price/1000;
Display_Money();
}
}
void Get_Maopi()
{
unsigned char clear;
mm: Weight_Maopi_0 = HX711_Read();
for(clear=0;clear<10;clear++)
{
Buzzer=1;
LED=1;
Delay_ms(100);
LED=0;
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 Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
|