代码:
main.c
#include "main.h"
#include "HX711.h"
#include "uart.h"
#include "LCD1602.h"
unsigned long HX711_Buffer = 0;
unsigned long Weight_Maopi = 0;
long Weight_Shiwu = 0;
unsigned char flag = 0;
bit Flag_ERROR = 0;
sbit speak= P1^7;
#define GapValue 400
void main()
{
Uart_Init();
Send_Word("Welcome to use!\n");
Send_Word("Made by Beetle Electronic Technology!\n");
Init_LCD1602();
LCD1602_write_com(0x80);
LCD1602_write_word("Welcome to use!");
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 ");
speak=0;
}
else
{
speak=1;
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(" g\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(" g");
}
}
}
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;
// Flag_ERROR = 1;
}
}
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++);
}
main.h
#ifndef __MAIN_H__
#define __MAIN_H__
#include <reg52.h>
sbit KEY1 = P3^1;
extern void Delay_ms(unsigned int n);
extern void Get_Maopi();
extern void Get_Weight();
extern void Scan_Key();
#endif
HX711.c
#include "HX711.h"
void Delay__hx711_us(void)
{
_nop_();
_nop_();
}
unsigned long HX711_Read(void) //ÔöÒæ128
{
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);
}
HX711.h
#ifndef __HX711_H__
#define __HX711_H__
#include <reg52.h>
#include <intrins.h>
sbit HX711_DOUT=P2^1;
sbit HX711_SCK=P2^0;
extern void Delay__hx711_us(void);
extern unsigned long HX711_Read(void);
#endif
LCD1602.c
#include "LCD1602.h"
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;
LCD1602_PORT = com;
LCD1602_delay_ms(1);
LCD1602_EN = 0;
}
void LCD1602_write_data(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_delay_ms(1);
LCD1602_PORT = 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);
}
LCD1602.h
#ifndef __LCD1602_H__
#define __LCD1602_H__
#include <reg52.h>
#define LCD1602_PORT P0
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;
extern void LCD1602_delay_ms(unsigned int n);
extern void LCD1602_write_com(unsigned char com);
extern void LCD1602_write_data(unsigned char dat);
extern void LCD1602_write_word(unsigned char *s);
extern void Init_LCD1602();
#endif
uart.c
#include "uart.h"
unsigned char Num = 0;
bit Flag_Receive_OK = 0;
void Uart_Init()
{
SCON = 0X50;
REN = 0;
PCON = 0x00;
TMOD = 0x21;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 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;
}
uart.h
#ifndef __UART_H__
#define __UART_H__
#include <reg52.h>
extern void Uart_Init();
extern void Send_Word(unsigned char *p);
extern void Send_ASCII(unsigned char c);
extern unsigned char RX_Buffer[30];
extern unsigned char Num;
extern bit Flag_Receive_OK;
#endif