数模转换元件为HX711
程序:
#include <reg51.h>
#include <intrins.h>
sbit HX711_DOUT=P2^1;
sbit HX711_SCK=P2^0;
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
long Weight_Shiwu = 0;
unsigned char flag = 0;
bit Flag_ERROR = 0;
void Delay_ms(unsigned int);
#define GapValue 400
sbit KEY1=P3^2;
sbit LCD1602_RS=P2^6;
sbit LCD1602_RW=P2^5;
sbit LCD1602_EN=P2^7;
unsigned char Num = 0;
bit Flag_Receive_OK = 0;
void Uart_Init(void);
void Send_Word(unsigned char *p);
void Get_Weight();
void Scan_Key();
void Get_Maopi();
void Send_ASCII(unsigned char c);
void LCD1602_delay_ms(unsigned int n);
void LCD1602_write_com(unsigned char com);
void LCD1602_write_data(unsigned char dat);
void LCD1602_write_word(unsigned char *s);
void Init_LCD1602();
void Delay__hx711_us(void);
unsigned long HX711_Read(void);
void main()
{
Uart_Init();
Send_Word("Welcome to use!\n");
Init_LCD1602();
LCD1602_write_com(0x80);
LCD1602_write_word(" 2021-1-4 ");
Delay_ms(1000); //延时,等待传感器稳定
Get_Maopi(); //称毛皮重量
while(1)
{
EA = 0;
Get_Weight();
EA = 1;
Scan_Key();
if( Flag_ERROR == 1)
{
Send_Word("ERROR\n");
LCD1602_write_com(0x80+0x40);
LCD1602_write_word("ERROR ");
}
else
{
Send_ASCII(Weight_Shiwu/1000 + 0X30);
Send_ASCII(Weight_Shiwu%1000/100 + 0X30);
Send_ASCII(Weight_Shiwu%100/10 + 0X30);
Send_ASCII(Weight_Shiwu%10 + 0X30);
Send_Word(" MPa\n");
LCD1602_write_com(0x80+0x40);
LCD1602_write_data(Weight_Shiwu/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(" MPa");
}
}
}
//扫描按键
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;
}
}
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void LCD1602_delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
//写指令
void LCD1602_write_com(unsigned char com)
{
LCD1602_RS = 0;
LCD1602_delay_ms(1);
LCD1602_EN = 1;
P0 = com;
LCD1602_delay_ms(1);
LCD1602_EN = 0;
}
//写数据
void LCD1602_write_data(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_delay_ms(1);
P0 = dat;
LCD1602_EN = 1;
LCD1602_delay_ms(1);
LCD1602_EN = 0;
}
//连续写字符
void LCD1602_write_word(unsigned char *s)
{
while(*s>0)
{
LCD1602_write_data(*s);
s++;
}
}
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); //清屏指令
}
void Uart_Init(void)
{
SCON = 0X50; //UART方式1;8位UART
REN = 0; //不允许串行口接收数据
PCON = 0x00; //SMOD=0;波特率不加倍
TMOD = 0x21; //T1方式2,用于产生波特率
TH1 = 0xFD; //装初值
TL1 = 0xFD;
TR1 = 1; //启动定时器1
EA = 1; //打开全局中断控制
ES = 0; //串行口中断
}
void Send_Word(unsigned char *p) //发送字符串
{
while(*p)
{
SBUF=*(p++);
while(TI==0)
{
;
;
;
};
TI=0;
}
}
void Send_ASCII(unsigned char c) //发送一个字符
{
TI=0;
SBUF=c;
while(TI==0);
TI=0;
}
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
//读取HX711
unsigned long HX711_Read()
{
unsigned long count;
unsigned char i;
HX711_DOUT=1;
Delay__hx711_us();
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;
Delay__hx711_us();
HX711_SCK=0;
return(count);
}
|