hx711压力传感器89c52数码管显示
时间: 2023-09-12 13:03:32 浏览: 147
好的,你需要连接HX711压力传感器和89C52单片机,并使用数码管显示压力值。以下是一个简单的示例程序,可以作为参考:
```
#include <reg52.h>
#include <stdio.h>
sbit DQ = P3^2;
sbit SCLK = P3^4;
sbit PD_SCK=P1^0;
sbit DOUT=P1^1;
sbit LED=P1^7;
unsigned long Weight_Maopi=0,Weight_Shiwu=0;
float Weight_real=0;
unsigned long get_Weight(void);
void delay_us(unsigned int i)
{
while(i--);
}
void delay_ms(unsigned char i)
{
while(i--)
{
delay_us(114);
}
}
void delay(unsigned int i)
{
while(i--)
{
delay_us(1000);
}
}
void Init_HX711(void)
{
SCLK = 1;
delay_us(1);
SCLK = 0;
delay_us(1);
DQ = 1;
delay_us(1);
}
unsigned long ReadCount(void)
{
unsigned char i;
unsigned long Count;
DQ = 1;
delay_us(1);
SCLK = 0;
delay_us(1);
Count=0;
while(DOUT);
for(i=0;i<24;i++)
{
SCLK = 1;
delay_us(1);
Count=Count<<1;
SCLK = 0;
delay_us(1);
if(DOUT)
{
Count++;
}
}
SCLK = 1;
delay_us(1);
Count=Count^0x800000;
SCLK = 0;
delay_us(1);
return(Count);
}
unsigned long get_Weight(void)
{
unsigned long i = 0;
unsigned long Count = 0;
float Weight = 0;
Count = ReadCount();
for(i=0;i<10;i++)
{
Count += ReadCount();
}
Count = Count / 11;
Weight_Maopi=Count;
Weight=(float)(Weight_Maopi-Weight_Shiwu)/100;
return (unsigned long)(Weight*100);
}
void main(void)
{
unsigned long weight = 0;
unsigned char i;
Init_HX711();
while(1)
{
weight = get_Weight();
printf("Weight: %.2f g\r\n", (float)weight/100);
for(i=0;i<10;i++)
{
P0 = ~(0x01 << i);
delay_ms(5);
}
}
}
```
这是一个简单的程序,需要根据实际硬件连接进行调整。希望能帮到你。
阅读全文