基于51单片机lcd1602直接实时显示HX711模块称重代码
时间: 2023-07-30 10:06:05 浏览: 136
基于HX711的称重模块stm32程序.zip_HX711 STM32_HX711模块 STM32_hx711外接stm32_h
5星 · 资源好评率100%
以下是基于51单片机和HX711模块的称重代码,实时将重量显示在LCD1602上:
```
#include <reg51.h>
#include <intrins.h>
#define LCD_DB P0
sbit LCD_RS=P3^6;
sbit LCD_RW=P3^5;
sbit LCD_EN=P3^4;
sbit DOUT=P2^7; // HX711模块的数据输出引脚
sbit PD_SCK=P2^6; // HX711模块的时钟引脚
void Delay1ms(unsigned int);
void InitLcd1602();
void WriteCommand(unsigned char);
void WriteData(unsigned char);
void LcdDisplay(unsigned char,unsigned char,unsigned char);
void main()
{
unsigned long weight = 0;
unsigned char i = 0;
unsigned char j = 0;
unsigned char buff[16];
InitLcd1602(); // 初始化LCD1602
while(1)
{
// 读HX711模块的数据
while(DOUT);
for(i=0;i<24;i++)
{
PD_SCK=1;
weight=weight<<1;
PD_SCK=0;
if(DOUT) weight++;
}
for(j=0;j<1;j++)
{
PD_SCK=1;
PD_SCK=0;
}
// 将重量显示在LCD1602上
sprintf(buff,"%lu",weight);
LcdDisplay(2,1,'w');
LcdDisplay(2,2,'e');
LcdDisplay(2,3,'i');
LcdDisplay(2,4,'g');
LcdDisplay(2,5,'h');
LcdDisplay(2,6,'t');
LcdDisplay(2,7,':');
LcdDisplay(2,8,' ');
LcdDisplay(2,9,buff[0]);
LcdDisplay(2,10,buff[1]);
LcdDisplay(2,11,buff[2]);
LcdDisplay(2,12,buff[3]);
LcdDisplay(2,13,buff[4]);
LcdDisplay(2,14,buff[5]);
LcdDisplay(2,15,'g');
LcdDisplay(2,16,0);
weight = 0; // 清空重量
}
}
void Delay1ms(unsigned int i)
{
unsigned int j;
for(;i>0;i--)
{
for(j=110;j>0;j--);
}
}
void InitLcd1602()
{
WriteCommand(0x38); // 初始化LCD1602
WriteCommand(0x0c);
WriteCommand(0x06);
WriteCommand(0x01);
Delay1ms(10);
}
void WriteCommand(unsigned char c)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DB = c;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
}
void WriteData(unsigned char c)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DB = c;
LCD_EN = 1;
_nop_();
_nop_();
LCD_EN = 0;
}
void LcdDisplay(unsigned char y,unsigned char x,unsigned char c)
{
unsigned char address;
if(y!=1)
{
address=0xc0+x-1;
}
else
{
address=0x80+x-1;
}
WriteCommand(address);
WriteData(c);
}
```
注意:以上代码仅供参考,实际应用中需要根据具体硬件配置和需求进行修改。
阅读全文