標(biāo)題: Arduino電子秤代碼與制作 [打印本頁(yè)]

作者: 探索軟件    時(shí)間: 2018-12-18 16:32
標(biāo)題: Arduino電子秤代碼與制作




代碼:

//電子秤
#include "LCD12864RSPI.h"
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
#include "HX711.h"
int Weight = 0;
unsigned char show0[]={0xB5, 0xE7,  0xD7, 0xD3,  0xB3, 0xD3};  //電子秤
unsigned char show1[]="================";//橫隔線
unsigned char show2[]={ 0xD6, 0xD8,  0xC1, 0xBF,  0x3D, 0x00}; //重量=
unsigned char show3[]={0xBF, 0xCB};           //[克]
unsigned char show4[]={0xCC, 0xBD,  0xCB, 0xF7,  0xC8, 0xED,  0xBC, 0xFE,  0x20, 0x00,  0xD6, 0xC6};  //探索軟件 制
char str[4];                               //定義重量值存儲(chǔ)數(shù)組,5位,其中4位為數(shù)字,1位為小數(shù)點(diǎn)
int temp =0;                       //定義重量值中間變量,,用于將獲取的float型重量值轉(zhuǎn)變?yōu)閡nsigned char數(shù)組

void setup(){
Serial.begin(9600);
LCDA.Initialise();                     // 屏幕初始化
delay(100);
LCDA.DisplayString(0,2,show0,AR_SIZE(show0));  //第1行第2格開(kāi)始,顯示文字"電子秤"
LCDA.DisplayString(1,0,show1,AR_SIZE(show1));//第2行第2格開(kāi)始,顯示橫隔線
LCDA.DisplayString(2,1,show2,AR_SIZE(show2));//第4行第2格開(kāi)始,顯示文字濕度
LCDA.DisplayString(2,7,show3,AR_SIZE(show3));
LCDA.DisplayString(3,1,show4,AR_SIZE(show4));
Init_Hx711();                                //初始化HX711模塊連接的IO設(shè)置
Get_Maopi();                //獲取毛皮
delay(3000);
Get_Maopi();                //獲取毛皮
}

void loop(){
Weight = Get_Weight();        //計(jì)算放在傳感器上的重物重量
temp =Get_Weight();
if(temp<0){  temp =0; }
dtostrf(temp,4,0,str);
LCDA.DisplayString(2,4,(unsigned char *)str,AR_SIZE(str));//第3行第4格開(kāi)始值
//Serial.print("W="); //串口顯示重量
//Serial.print(Weight);        //串口顯示重量
//Serial.print(" g\n");        //顯示單位
   delay(1000);                //延時(shí)1s
}


HX711.h
#ifndef __HX711__H__
#define __HX711__H__
#include <Arduino.h>
#define HX711_SCK 6
#define HX711_DT 7
extern void Init_Hx711();
extern unsigned long HX711_Read(void);
extern unsigned int Get_Weight();
extern void Get_Maopi();
#endif


HX711.cpp
#include "hx711.h"

#define ValueGap 630
//該數(shù)值可用砝碼校準(zhǔn)

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

//****************************************************
//初始化HX711
//****************************************************
void Init_Hx711()
{
        pinMode(HX711_SCK, OUTPUT);        
        pinMode(HX711_DT, INPUT);
}


//****************************************************
//獲取毛皮重量
//****************************************************
void Get_Maopi()
{
        Weight_Maopi = HX711_Read();               
}

//****************************************************
//稱重
//****************************************************
unsigned int Get_Weight()
{
        HX711_Buffer = HX711_Read();

        Weight_Shiwu = HX711_Buffer;
        Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                                //獲取實(shí)物的AD采樣數(shù)值。
        
        Weight_Shiwu = (int)((float)Weight_Shiwu/ValueGap+0.05);         

        return Weight_Shiwu;
}

//****************************************************
//讀取HX711
//****************************************************
unsigned long HX711_Read(void)        //增益128
{
        unsigned long count;
        unsigned char i;
        bool Flag = 0;
        digitalWrite(HX711_DT, HIGH);
        delayMicroseconds(1);
        digitalWrite(HX711_SCK, LOW);
        delayMicroseconds(1);
          count=0;
          while(digitalRead(HX711_DT));
          for(i=0;i<24;i++)
        {
                  digitalWrite(HX711_SCK, HIGH);
                delayMicroseconds(1);
                  count=count<<1;
                digitalWrite(HX711_SCK, LOW);
                delayMicroseconds(1);
                  if(digitalRead(HX711_DT))
                        count++;
        }
         digitalWrite(HX711_SCK, HIGH);
        delayMicroseconds(1);
        digitalWrite(HX711_SCK, LOW);
        delayMicroseconds(1);
        count ^= 0x800000;
        return(count);
}





作者: 1354736852    時(shí)間: 2020-7-8 22:41
大哥還在嗎,我今天用了你的程序很多報(bào)警能幫忙解決下嗎




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1