找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2331|回复: 1
收起左侧

分享一个电子秤(基于HX711+12864+STC89C51)

[复制链接]
ID:384477 发表于 2018-8-24 10:02 | 显示全部楼层 |阅读模式
本帖最后由 xiaohancf 于 2018-8-24 10:07 编辑

先贴程序main.c:
#include "main.h"
#include "LCD12864.h"
#include "HX711.h"

unsigned long HX711_Buffer = 0;
unsigned int Weight_Maopi = 0,Weight_Shiwu = 0;

//****************************************************
//主函数
//****************************************************
void main()
{
    LCD12864_Reset();                                //初始化液晶
    LCD12864_HAIZI_SET();                            //设置为普通模式

    LCD12864_NoWaitIdle_COM_Write(0x80);                        //指针设置
    LCD12864_write_word("※※※※※※※※");
    LCD12864_NoWaitIdle_COM_Write(0x90);                        //指针设置
    LCD12864_write_word("※※欢迎使用※※");            

    Delay_ms(1000);         //延时1s,等待传感器稳定

    Get_Maopi();                //称毛皮重量
    LCD12864_NoWaitIdle_COM_Write(0x01);            //清空

    while(1)
    {
        Get_Weight();            //称重

        if(  Weight_Shiwu > 10000 )
        {
        LCD12864_NoWaitIdle_COM_Write(0x80);                        //指针设置
        LCD12864_write_word("※※※超重※※※");        
        }
        else if( Weight_Shiwu < 10000)
        {
        //显示当前重量
        LCD12864_NoWaitIdle_COM_Write(0x80);
        LCD12864_write_word("重量:");
        if( Weight_Shiwu/10000 != 0)
        {
            LCD12864_Data_Write(Weight_Shiwu/10000 + 0x30);
        }
        else
        {
            LCD12864_Data_Write(' ');   
        }
        LCD12864_Data_Write(Weight_Shiwu%10000/1000 + 0x30);
        LCD12864_Data_Write('.');
        LCD12864_Data_Write(Weight_Shiwu%1000/100 + 0x30);
        LCD12864_Data_Write(Weight_Shiwu%100/10 + 0x30);
        LCD12864_Data_Write(Weight_Shiwu%10 + 0x30);
        LCD12864_write_word(" Kg");

        LCD12864_NoWaitIdle_COM_Write(0x90);
        LCD12864_write_word("重量:");
        if( Weight_Shiwu*2/10000 != 0)
        {
            LCD12864_Data_Write(Weight_Shiwu*2/10000 + 0x30);
        }
        else
        {
            LCD12864_Data_Write(' ');   
        }
        LCD12864_Data_Write(Weight_Shiwu*2%10000/1000 + 0x30);
        LCD12864_Data_Write('.');
        LCD12864_Data_Write(Weight_Shiwu*2%1000/100 + 0x30);
        LCD12864_Data_Write(Weight_Shiwu*2%100/10 + 0x30);
        LCD12864_Data_Write(Weight_Shiwu*2%10 + 0x30);
        LCD12864_write_word(" ");

        }

    }
}

//****************************************************
//称重
//****************************************************
void Get_Weight()
{
    HX711_Buffer = HX711_Read();
    HX711_Buffer = HX711_Buffer/100;
    if(HX711_Buffer > Weight_Maopi)            
    {
        Weight_Shiwu = HX711_Buffer;
        Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                //获取实物的AD采样数值。

        Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/2.09+0.05);     //计算实物的实际重量
                                                                        //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的2.15这个除数。
                                                                        //当发现测试出来的重量偏大时,增加该数值。
                                                                        //如果测试出来的重量偏小时,减小改数值。
                                                                        //该数值一般在2.15附近调整之间。因传感器不同而定。
                                                                        //+0.05是为了四舍五入百分位        
    Weight_Shiwu=Weight_Shiwu/2;   

    }
}

//****************************************************
//获取毛皮重量
//****************************************************
void Get_Maopi()
{
    unsigned char i = 0;
    unsigned int Temp_Weight = 0;

    Weight_Maopi = 0;

    for( i = 0 ; i < 10 ; i++)
    {
        HX711_Buffer = HX711_Read();
        Temp_Weight = HX711_Buffer/100;

        if( Temp_Weight > Weight_Maopi)
        {
            Weight_Maopi = Temp_Weight;     
        }
    }        
}

//****************************************************
//MS延时函数(12M晶振下测试)
//****************************************************
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>

//函数或者变量声明
extern void Delay_ms(unsigned int n);
extern void Get_Maopi();
extern void Get_Weight();


#endif


LCD12864.h:
#ifndef __LCD12864_H__
#define __LCD12864_H__

#include <reg52.h>
#include <intrins.h>

//引脚定义
sbit LCD12864_RS_PORT  = P1^0;  
sbit LCD12864_RW_PORT = P1^1;
sbit LCD12864_E_PORT    = P1^2;
#define LCD12864_DA_PORT P2


//函数或者变量声明
extern void LCD12864_WaitIdle();
extern void LCD12864_COM_Write( unsigned char com_da);
extern void LCD12864_NoWaitIdle_COM_Write(unsigned char com_da);
extern void LCD12864_Data_Write(unsigned char da);
extern void lcd_delay_ms(unsigned char x);
extern void LCD12864_Reset();
extern void LCD12864_write_word(unsigned char *s);
//extern void LCD12864_PHOTO_SET();
extern void LCD12864_HAIZI_SET();
//extern void LCD12864_HAIZI_WRITE(unsigned char xpos,unsigned char ypos,unsigned char daH,unsigned char daL);
//extern void LCD12864_PHOTO_WRITE(unsigned char *img);


#endif


HX711.h:
#ifndef __HX711_H__
#define __HX711_H__


#include <reg52.h>
#include <intrins.h>

//IO设置
sbit HX711_DOUT=P3^4;
sbit HX711_SCK=P3^5;

//函数或者变量声明
extern void Delay__hx711_us(void);
extern unsigned long HX711_Read(void);

#endif


LCD12864.c:
#include "LCD12864.h"

//********************************************************************
//LCD12864  忙 信号检测
//********************************************************************
void LCD12864_WaitIdle()

{
    unsigned char i;
    LCD12864_DA_PORT = 0xff;
    LCD12864_RS_PORT = 0;
    LCD12864_RW_PORT = 1;
    LCD12864_E_PORT = 1;
    while((LCD12864_DA_PORT&0x80)==1); /*等待BF 不为1*/
    LCD12864_E_PORT = 0;
    for(i=0;i<5;i++);
}


//********************************************************************
//检测忙信号写入命令字    com_da 为待写入的命令字
//********************************************************************
void LCD12864_COM_Write( unsigned char com_da)   
{
    LCD12864_WaitIdle();
    LCD12864_RS_PORT = 0;
    LCD12864_RW_PORT = 0;
    LCD12864_DA_PORT = com_da;
    LCD12864_E_PORT = 1;
    _nop_();
    _nop_();   
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD12864_E_PORT = 0;   
}

//********************************************************************
//不检测忙信号写入命令字        com_da 为待写入的命令字
//********************************************************************
void LCD12864_NoWaitIdle_COM_Write(unsigned char com_da)   
{
    LCD12864_RS_PORT = 0;
    LCD12864_RW_PORT = 0;
    LCD12864_DA_PORT = com_da;
    LCD12864_E_PORT = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD12864_E_PORT = 0;   
}

//********************************************************************
//数据写入        da  为待写入的8位数据
//********************************************************************
void LCD12864_Data_Write(unsigned char da)

{
    LCD12864_WaitIdle(); /*检测忙信号*/
    LCD12864_RS_PORT = 1;
    LCD12864_RW_PORT = 0;
    LCD12864_DA_PORT = da;
    LCD12864_E_PORT = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD12864_E_PORT = 0;   
}


//*************************************************************************************
//写连续字符函数
//*************************************************************************************
void LCD12864_write_word(unsigned char *s)
{
    while(*s>0)
    {
        LCD12864_Data_Write(*s);
        s++;
    }
}

//********************************************************************
//1MS为单位的延时程序,不准确
//********************************************************************
void lcd_delay_ms(unsigned char x)
{
    unsigned char j;
    while(x--){
        for(j=0;j<125;j++)
            {;}
        }   
}


//********************************************************************
//LCD12864初始化
//********************************************************************
void LCD12864_Reset()
{
    lcd_delay_ms(100); /*适当延时待LCD自动复位完成*/
    LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通讯*/
    lcd_delay_ms(10);
    LCD12864_NoWaitIdle_COM_Write(0x30); /*使用8位并口通讯*/
    lcd_delay_ms(10);
    LCD12864_NoWaitIdle_COM_Write(0x0c); /*显示开及光标设置*/
    lcd_delay_ms(10);
    LCD12864_NoWaitIdle_COM_Write(0x01); /*显示清屏*/
    lcd_delay_ms(30);
    LCD12864_NoWaitIdle_COM_Write(0x06); /*DDRAM的地址计数器(AC)加1*/
    lcd_delay_ms(30);   
}


//void LCD12864_PHOTO_SET()
//{
//    LCD12864_COM_Write(0x36);
//    lcd_delay_ms(10);
//    LCD12864_COM_Write(0x36);
//    lcd_delay_ms(10);
//}
//
void LCD12864_HAIZI_SET()
{
    LCD12864_COM_Write(0x30);
    lcd_delay_ms(10);
    LCD12864_COM_Write(0x30);
    lcd_delay_ms(10);
}
//
//
//void LCD12864_HAIZI_WRITE(unsigned char xpos,unsigned char ypos,unsigned char daH,unsigned char daL)
////ST7920 汉字字符写入
////参数说明:     xpos 待写入的X位置
////ypos 待写入的Y位置
////daH 待写入的汉字的高八位 daL待写入的汉字的低八位
//{
//    unsigned char xy_pos;
//    if((xpos>=8)||(ypos>=4) ) return; /*X位置超出显示范围退出*/
//    if(ypos==0)         xy_pos = 0x80 + xpos;
//    else if(ypos==1) xy_pos = 0x90 + xpos; /*计算转换地址*/
//    else if(ypos==2) xy_pos = 0x88 + xpos;
//    else if(ypos==3) xy_pos = 0x98 + xpos;
//    LCD12864_COM_Write(xy_pos); /*写地址*/
//    lcd_delay_ms(1);
//    LCD12864_Data_Write(daH); /*写高八位数据*/
//    lcd_delay_ms(1);   
//    LCD12864_Data_Write(daL); /*写低八位数据*/   
//    lcd_delay_ms(1);
//}
//
//
//void LCD12864_PHOTO_WRITE(unsigned char *img)
//{
//    unsigned char x,y,i,j;
//    unsigned int k=0;
//    y=0x80; /*设置起始 绘图区的 Y地址坐标*/
//    x=0x80; /*设置起始 绘图区的 X地址坐标*/
//    for(i=0;i<32;i++){  /*写上半部*/
//        LCD12864_COM_Write(y);
//        LCD12864_COM_Write(x);
//        for(j=0;j<16;j++){
//            LCD12864_Data_Write(img[k]);
//            k++;
//            }
//        y++;
//        }
//   
//    y=0x80; /*设置起始 绘图区的 Y地址坐标*/
//    x=0x88; /*设置起始 绘图区的 X地址坐标*/
//    for(i=0;i<32;i++){  /*写下半部*/
//        LCD12864_COM_Write(y);
//        LCD12864_COM_Write(x);
//        for(j=0;j<16;j++){
//            LCD12864_Data_Write(img[k]);
//            k++;
//            }
//        y++;
//        }
//   
//}


HX711.c:
#include "HX711.h"


//****************************************************
//延时函数
//****************************************************
void Delay__hx711_us(void)
{
    _nop_();
    _nop_();
}

//****************************************************
//读取HX711
//****************************************************
unsigned long HX711_Read(void)    //增益128
{
    unsigned long count;
    unsigned char i;
      HX711_DOUT=1;
    Delay__hx711_us();
      HX711_SCK=0;
      count=0;
      while(HX711_DOUT);
      for(i=0;i<24;i++)
    {
          HX711_SCK=1;
          count=count<<1;
        HX711_SCK=0;
          if(HX711_DOUT)
            count++;
    }
     HX711_SCK=1;
    Delay__hx711_us();
    HX711_SCK=0;  
    return(count);
}



电路图

电路图

12864中文.doc

2.61 MB, 下载次数: 25, 下载积分: 黑币 -5

附上12864说明

回复

使用道具 举报

ID:150860 发表于 2018-8-25 23:10 | 显示全部楼层
楼主最好把液晶屏显示的也发一张图更有吸引力
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表