#include<reg51.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit HX711_DOUT=P2^1;
sbit HX711_SCK=P2^0;
sbit lcdrs=P2^5;
sbit lcdrw=P2^4;
sbit lcden=P2^3;
sbit KEY1 = P3^2;
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
long Weight_Shiwu = 0;
unsigned char flag = 0;
bit Flag_ERROR = 0;
sbit speak= P3^5;
#define GapValue 430
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
delay(1);
HX711_SCK=0;
count=0;
EA = 1;
while(HX711_DOUT);
EA = 0;
for(i=0;i<24;i++)
{
HX711_SCK=1;
count=count<<1;
HX711_SCK=0;
if(HX711_DOUT)
count++;
}
HX711_SCK=1;
count=count^0x800000;//第25个脉冲下降沿来时,转换数据
delay(1);;
HX711_SCK=0;
return(count);
}
void write_com(uchar com) //写指令
{
lcdrs=0; //处理指令
P0=com; //输入命令
delay(5);
lcden=1; //使能
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1; //写数据
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void LCD_init()
{
lcdrw=0;
lcden=0;
write_com(0x38); // 显示模式设置
write_com(0x0e); // 显示光标,光标不闪烁
write_com(0x06); // 地址指针加1,光标右移
write_com(0x01); // 清屏
write_com(0x80+0x10); //数据指标地址(首地址为0x80)
}
/*在LCD上显示字符
输入参数:显示数据的地址 *dat,
x 液晶显示在第x行,x=1或2
*/
void LCD_display(uchar *dat, uchar x)
{
uchar num;
if(x==1)
{
for(num=0;num<16;num++) //1602第一行写入16个字符
{
write_com(0x80+num); //Ox80为第一行首地址,num为DDRAM地址偏移量
write_data(*dat);
dat++; //数组指针加1
delay(20);
}
}
// write_com(1);
if(x==2)
{
for(num=0;num<16;num++) //1602第二行写入16个字符
{
write_com(0x80+0x40+num); //0x80+0x40为第二行首地址,num为DDRAM地址偏移量
write_data(*dat);
dat++; //数组指针加1
delay(20);
}
}
}
void display_welcome(void) //欢迎界面显示
{
uchar table1[]={" CME13007 "};
uchar table2[]={" Welcome to use "};
LCD_display(table1,1); //
LCD_display(table2,2); //
delay(200);
}
void display_error(void)
{
uchar table1[]={" CME13007 "};
uchar table2[]={" error "};
LCD_display(table1,1); //
LCD_display(table2,2); //
delay(200);
}
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
void Scan_Key()
{
if(KEY1 == 0)
{
Delay_ms(5);
if(KEY1 == 0)
{
while(KEY1 == 0);
Get_Maopi(); //去皮
}
}
}
//****************************************************
//称重
//****************************************************
void Get_Weight()
{
Weight_Shiwu = HX711_Read();
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取净重
if(Weight_Shiwu > 0)
{
Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/GapValue); //计算实物的实际重量
if(Weight_Shiwu > 5000) //超重报警
{
Flag_ERROR = 1;
}
else
{
Flag_ERROR = 0;
}
}
else
{
Weight_Shiwu = 0;
// Flag_ERROR = 1; //负重报警
}
}
//****************************************************
//MS延时函数(12M晶振下测试)
//****************************************************
void display_weight(void)
{
uchar table1[]={" the weight is: "};
uchar table2[]={" . g "};
table2[0]=Weight_Shiwu/1000+'0';
table2[2]=Weight_Shiwu/100%10+'0';
table2[3]=Weight_Shiwu/10%10+'0';
table2[4]=Weight_Shiwu%10+'0';
LCD_display(table1,1);
LCD_display(table2,2);
delay(200);
}
void main()
{
LCD_init(); //液晶屏初始化
display_welcome(); //欢迎界面
Delay_ms(1000);
while(1)
{
Get_maopi();
EA=0;
Get_Weight();
EA=1;
Scan_Key();
if(Flag_ERROR ==1)
{ display_error();
speak=0;}
else
{
speak=1;
display_weight();
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>