Skip to content

Commit

Permalink
added hx711 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lishen1 committed Jan 25, 2018
1 parent f19a1c8 commit ca0f659
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
155 changes: 155 additions & 0 deletions devLib/hx711.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#include <stdio.h>
#include <inttypes.h>
#include <wiringPi.h>

#include "hx711.h"

//channel A, gain 128
#define GAIN_128 128

//channel A, gain 64
#define GAIN_64 64

//channel B, gain 32
#define GAIN_32 32

uint8_t sGainBits[256];
float sScale[256];
int sOffset[256];

uint8_t sClockPin[256];
uint8_t sDataPin[256];

uint8_t hx711Setup(uint8_t clockPin, uint8_t dataPin, uint8_t skipSetup, uint8_t instId)
{
sGainBits[instId] = 1;
sScale[instId] = 1.0f;
sOffset[instId] = 0;
sClockPin[instId] = clockPin;
sDataPin[instId] = dataPin;
if ((!skipSetup) && wiringPiSetup() == -1)
{
printf("initialization failed");
return 255;
}
pinMode(sClockPin[instId], OUTPUT);
pinMode(sDataPin[instId], INPUT);
return 0;
}

uint8_t hx711IsReady(uint8_t instId)
{
return digitalRead(sDataPin[instId]) == LOW;
}

void hx711SetGain(uint8_t gain, uint8_t instId)
{
switch (gain)
{
case GAIN_128:
sGainBits[instId] = 1;
break;
case GAIN_64:
sGainBits[instId] = 3;
break;
case GAIN_32:
sGainBits[instId] = 2;
break;
default:
//invalid gain, ignore
break;
}

digitalWrite(sClockPin[instId], LOW);
hx711Read(instId);
}

int hx711Read(uint8_t instId)
{
// wait for the chip to become ready
while (!hx711IsReady(instId))
;

int data = 0;
// pulse the clock pin 24 times to read the data
for (uint8_t i = 24; i--;)
{
digitalWrite(sClockPin[instId], HIGH);

digitalRead(sDataPin[instId]);
data |= (digitalRead(sDataPin[instId]) << i);

digitalWrite(sClockPin[instId], LOW);
}

// set the channel and the gain factor for the next reading using the clock pin
for (int i = 0; i < sGainBits[instId]; i++)
{
digitalWrite(sClockPin[instId], HIGH);
digitalWrite(sClockPin[instId], LOW);
}

if (data & 0x800000)
{
data |= (long)~0xffffff;
}

return data;
}

int hx711ReadAverage(uint8_t times, uint8_t instId)
{
int64_t sum = 0;
for (uint8_t i = 0; i < times; i++)
{
sum += hx711Read(instId);
}
return sum / times;
}

int hx711GetRawValue(uint8_t times, uint8_t instId)
{
return hx711ReadAverage(times, instId) - sOffset[instId];
}

float hx711GetUnits(uint8_t times, uint8_t instId)
{
return hx711GetRawValue(times, instId) / sScale[instId];
}

void hx711Tare(uint8_t times, uint8_t instId)
{
uint64_t sum = hx711ReadAverage(times, instId);
hx711SetOffset(sum, instId);
}

void hx711SetScale(float scale, uint8_t instId)
{
sScale[instId] = scale;
}

void hx711SetOffset(int offset, uint8_t instId)
{
sOffset[instId] = offset;
}

void hx711PowerDown(uint8_t instId)
{
digitalWrite(sClockPin[instId], LOW);
digitalWrite(sClockPin[instId], HIGH);
}

void hx711PowerUp(uint8_t instId)
{
digitalWrite(sClockPin[instId], LOW);
}

int hx711GetOffset(uint8_t instId)
{
return sOffset[instId];
}

float hx711GetScale(uint8_t instId)
{
return sScale[instId];
}
56 changes: 56 additions & 0 deletions devLib/hx711.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

/*
* hx711.h:
* 24-Bit Analog-to-Digital Converter (ADC) for weight measure
* Adaptation of https://github.com/pommoisel/HX711
*
* Copyright (c) 2013 Gordon Henderson.
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/

#ifndef _HX711_H_
#define _HX711_H_

#include <inttypes.h>

#ifdef __cplusplus
extern "C" {
#endif

extern uint8_t hx711Setup(uint8_t clockPin, uint8_t dataPin, uint8_t skipSetup, uint8_t instId);
extern uint8_t hx711IsReady(uint8_t instId);
extern void hx711SetGain(uint8_t gain, uint8_t instId);
extern int hx711Read(uint8_t instId);

extern int hx711ReadAverage(uint8_t times, uint8_t instId);
extern int hx711GetRawValue(uint8_t times, uint8_t instId);
extern float hx711GetUnits(uint8_t times, uint8_t instId);
extern void hx711Tare(uint8_t times, uint8_t instId);
extern void hx711SetScale(float scale, uint8_t instId);
extern void hx711SetOffset(int offset, uint8_t instId);
extern void hx711PowerDown(uint8_t instId);
extern void hx711PowerUp(uint8_t instId);
extern int hx711GetOffset(uint8_t instId);
extern float hx711GetScale(uint8_t instId);

#ifdef __cplusplus
}
#endif

#endif //_HX711_H_

0 comments on commit ca0f659

Please sign in to comment.