Jav作者
4年11个月前IP:河南
899721
1楼

附完整代码。

#include <reg52.h>

#include <intrins.h>


/*I/O口声明*/

sbit LCD1602_RS = P2^6;

sbit LCD1602_RW = P2^5;

sbit LCD1602_EN = P2^7;

sbit HX711_DOUT = P2^1; 

sbit HX711_SCK = P2^0;

sbit key1 = P3^1;

sbit key2 = P3^0;


/*全局变量定义*/

unsigned long Weight_Shiwu = 0;

unsigned long Weight_Maopi = 0;


/*函数声明*/

void Delay(unsigned int n);

void Init_LCD1602();

void LCD1602_write_com(unsigned char com);

void LCD1602_write_word(unsigned char *s);

void LCD1602_write_data(unsigned char dat);

unsigned long HX711_Read(void);

void Keyscan();

void Get_Maopi();

void Get_Weight();


int main()

{ 

Init_LCD1602(); //LCD1602初始化

LCD1602_write_com(0x80); //移动光标到第一行首位

LCD1602_write_word(" Dian Zi Cheng! "); //显示 Dian Zi Cheng! 

LCD1602_write_com(0x80 + 0x40); //移动光标到第二行首位

LCD1602_write_word("Weight :"), //显示Weight :

Delay(1000); //1s延时

while(1)

{

Keyscan();

Get_Weight();

LCD1602_write_com(0x80+0x49); //移动光标到第二行第九位

LCD1602_write_data(Weight_Shiwu%10000/1000 + 0X30);

        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(" g");

}

return 0;

}


/*称实物重*/

void Get_Weight()

{

Weight_Shiwu = HX711_Read();

Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/108);

Weight_Shiwu = Weight_Shiwu - Weight_Maopi;

}


/*延时子函数*/

void Delay(unsigned int n)

{

int i, j;

for(i=0; i<n; i++)

for(j=0; j<110; j++);

}


/*写指令*/

void LCD1602_write_com(unsigned char com)

{

LCD1602_RS = 0;

Delay(10);

LCD1602_EN = 1;

P0 = com;

Delay(10);

LCD1602_EN = 0;

}


/*写数据*/

void LCD1602_write_data(unsigned char dat)

{

LCD1602_RS = 1;

Delay(10);

P0 = dat;

LCD1602_EN = 1;

Delay(10);

LCD1602_EN = 0;

}


/*写连续字符(字符串)*/

void LCD1602_write_word(unsigned char *s)

{

while(*s>0)

{

LCD1602_write_data(*s);

s++;

}

}


/*初始化LCD1602*/

void Init_LCD1602()

{

LCD1602_EN = 0;

LCD1602_RW = 0;

LCD1602_write_com(0x38);

LCD1602_write_com(0x0c);

LCD1602_write_com(0x06);

LCD1602_write_com(0x01);

}


/*获取ad值*/

unsigned long HX711_Read(void)

{

unsigned long count; 

unsigned char i; 

  HX711_DOUT = 1; 

_nop_();

_nop_();

  HX711_SCK = 0; 

  count=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;

_nop_();

_nop_();

HX711_SCK = 0;  

return(count);

}


/*称毛皮重*/

void Get_Maopi()

{

Weight_Maopi = HX711_Read();

Weight_Maopi = (unsigned int)((float)Weight_Maopi/108); 

}


/*按键扫描*/

void Keyscan()

{

if(key1 == 0)

Delay(10);

if(key1 == 0){

Get_Maopi();

while(!key1);

}

if(key2 == 0)

Delay(10);

if(key2 == 0)

{

Weight_Maopi = 0;

while(!key2);

}

}


评论
文号 / 899721

一表人才
名片发私信
学术分 0
总主题 3 帖总回复 43 楼拥有证书:机友
注册于 2021/03/12 10:13:44最后登录 2025/08/05 11:18:03
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:陕西

个人简介

暂未填写
笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的