HX711 51单片机代码
时间: 2023-11-10 13:06:37 浏览: 187
51单片机代码
以下是HX711模块与51单片机的代码示例:
```
#include <reg52.h>
#include <intrins.h>
#define DOUT P3_5
#define SCK P3_6
typedef unsigned char u8;
typedef unsigned int u16;
sbit DOUT=P3^5;
sbit SCK=P3^6;
u8 code table[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay_ms(u16 ms)
{
u16 i,j;
for(i=0;i<ms;i++)
for(j=110;j>0;j--);
}
void Write_Ds18b20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SCK=0;
_nop_();
DOUT=dat&0x01;
dat>>=1;
SCK=1;
_nop_();
SCK=0;
_nop_();
}
}
unsigned long Read_Count(void)
{
unsigned char i;
unsigned long Count=0;
DOUT=1;
SCK=0;
_nop_();
while(DOUT);
for(i=0;i<24;i++)
{
SCK=1;
Count=Count<<1;
_nop_();
SCK=0;
_nop_();
if(DOUT)
Count++;
}
SCK=1;
Count=Count^0x800000;
_nop_();
SCK=0;
_nop_();
return(Count);
}
void main()
{
u8 i;
u8 j;
u8 k;
unsigned long Count;
unsigned long Weight;
u8 buffer[6];
while(1)
{
Count=Read_Count();
if(Count>=0x800000)
Count=Count|0xff000000;
Weight=Count/100;
buffer[0]=table[Weight/1000];
buffer[1]=table[Weight%1000/100];
buffer[2]=table[Weight%100/10];
buffer[3]=table[Weight%10];
for(i=0;i<4;i++)
{
P2=0x00;
for(j=0;j<8;j++)
{
P0=buffer[i];
for(k=0;k<50;k++);
P2|=0x01;
P2=0x00;
}
}
delay_ms(500);
}
}
```
注意:这只是一个简单的示例代码,实际应用中可能需要进行更多的优化和修改。
阅读全文