基于单片机 51 的称重系统设计与实现
#include <reg52.h> #include <intrins.h> #include <string.h> #include "main.h" #include "LCD1602.h" #include "HX711.h" #include "eeprom52.h" typedef unsigned char uchar; typedef unsigned int uint; uchar code table[] = {'Welcome!'}; uchar code ren[] = {'RenShu:'}; uchar code dat[2]; long Weight_Shiwu =0; volatile bit FlagTest = 0; uint GapValue,GapValue1; sbit RS = P2^6; //RS? sbit RW = P2^5; sbit EN = P2^7; //EN?¡¢
int z=0;
sbit k1=P3^0; sbit k2=P3^1; sbit k3=P3^3;
sbit led1=P2^0; sbit led2=P2^1; sbit led3=P2^2; long c_new; uint num,i,c; uint GapValue,GapValue1; void write_eeprom() { SectorErase(0x2000); GapValue1=GapValue&0x00ff; byte_write(0x2000, GapValue1); GapValue1=(GapValue&0xff00)>>8; byte_write(0x2001, GapValue1); byte_write(0x2060, a_a); }
/*°ÑÊý¾Ý´Óµ¥Æ¬»úÄÚ²¿eepromÖжÁ³öÀ´/ void read_eeprom() { GapValue = byte_read(0x2001); GapValue = (GapValue<<8)|byte_read(0x2000); a_a = byte_read(0x2060); }
/¿ª»ú×Ô¼ìeeprom³õʼ»¯***/
void init_eeprom()
{
read_eeprom(); //ÏȶÁ
if(a_a != 1) //еĵ¥Æ¬»ú³õʼµ¥Æ¬»úÄÚÎÊeeprom
{
GapValue = 3500;
a_a = 1;
write_eeprom(); //±£´æÊý¾Ý
}
}
void Timer0_Init()
{
ET0 = 1; //ÔÊÐí¶¨Ê±Æ÷0ÖжÏ
TMOD = 1; //¶¨Ê±Æ÷¹¤×÷·½Ê½Ñ¡Ôñ
TL0 = 0xb0;
TH0 = 0x3c; //¶¨Ê±Æ÷¸³Óè³õÖµ
TR0 = 1; //Æô¶¯¶¨Ê±Æ÷
}
void delay(uint z) { uint x,y; for(x = z; x > 0; x--) for(y = 114; y > 0 ; y--); } void Read_Busy() { uchar busy; P0 = 0xff; RS = 0; RW = 1; do { EN = 1; busy = P0; EN = 0; }while(busy & 0x80); } void Write_Cmd(uchar cmd) { Read_Busy();//??? RS = 0; RW = 0; P0 = cmd; EN = 1; EN = 0; } //??????? void Write_Dat(uchar dat) { Read_Busy(); RS = 1; RW = 0; P0 = dat; EN = 1; EN = 0; } void Init_LCD1602() {
EN=0;
Write_Cmd(0x38); // ??16*2??,5*7??,8?????
Write_Cmd(0x0c);
Write_Cmd(0x06); //???????????1
Write_Cmd(0x01); //????
} void key1() { if(k1==0) { delay(5); if(k1==0) { c++; } while(!k1); } if(k2==0) { delay(5); if(k2==0) { c--; } while(!k2); } } void key2() { if(k3 == 0) { delay(5); if(k3 == 0) { if(c > Weight_Shiwu) { led1 = 1; led2 = 0; led3 = 0; } else if(c < Weight_Shiwu) { led1 = 0; led2 = 1; led3 = 0; } else // c == a { led1 = 0; led2 = 0; led3 = 1; } } while(!k3); } }
void Get_Weighte() { Weight_Shiwu = HX711_Read(); //»ñÈ¡¾»ÖØ
Weight_Shiwu = (unsigned int)((float)((Weight_Shiwu*10)/GapValue)-24847)/1000; //¼ÆËãʵÎïµÄʵ¼ÊÖØÁ¿
}
void main()
{
init_eeprom(); //¿ªÊ¼³õʼ»¯±£´æµÄÊý¾Ý
Init_LCD1602(); //³õʼ»¯LCD1602
EA = 0;
Timer0_Init();
//³õÖÐʼ»¯Íê³É£¬¿ª¶Ï
EA = 1;
c=0;
while(1)
{
Write_Cmd(0x80 | 0x00);//????
for(num=0;num<8;num++)
{
Write_Dat(table[num]);
delay(5);
}
Write_Cmd(0x80 | 0x40);//????
for(i=0;i<7;i++)
{
Write_Dat(ren[i]);
delay(5);
}
Write_Cmd(0x80 | 0x48);//??????0x00,?????????????
key1();
Write_Dat(c/10+0x30);
Write_Dat(c%10+0x30);
if (FlagTest==1)
{
Get_Weighte();
FlagTest = 0;
if(c > Weight_Shiwu)
{
led1 = 1;
led2 = 0;
led3 = 0;
}
else if(c < Weight_Shiwu)
{
led1 = 0;
led2 = 1;
led3 = 0;
}
else // c == a
{
led1 = 0;
led2 = 0;
led3 = 1;
}
}
key2();
}
原文地址: https://www.cveoy.top/t/topic/nCip 著作权归作者所有。请勿转载和采集!