Fork of hx711

Fork of HX711 by Cr300-Litho

Committer:
BB50
Date:
Fri Jun 12 16:05:39 2015 +0000
Revision:
0:f82840dd806a
Child:
1:cb1c777e3d22
1st release.; Work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BB50 0:f82840dd806a 1 /*
BB50 0:f82840dd806a 2 * FILE: HX711.cpp
BB50 0:f82840dd806a 3 *
BB50 0:f82840dd806a 4 * VERSION: 0.1
BB50 0:f82840dd806a 5 * PURPOSE: HX711 weight library for Nucleo STM32
BB50 0:f82840dd806a 6 * AUTHOR: Bertrand Bouvier
BB50 0:f82840dd806a 7 * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
BB50 0:f82840dd806a 8 *
BB50 0:f82840dd806a 9 * DATASHEET: http://www.dfrobot.com/image/data/SEN0160/hx711_english.pdf
BB50 0:f82840dd806a 10 * URL:
BB50 0:f82840dd806a 11 *
BB50 0:f82840dd806a 12 * HISTORY:
BB50 0:f82840dd806a 13 * 24/05/2015 - Bertrand Bouvier - Original version
BB50 0:f82840dd806a 14 * see HX711.h
BB50 0:f82840dd806a 15 *
BB50 0:f82840dd806a 16 * SPECIAL THANKS:
BB50 0:f82840dd806a 17 * Inspiré du travail de Weihong Guan (@aguegu)
BB50 0:f82840dd806a 18 * https://github.com/aguegu/Arduino
BB50 0:f82840dd806a 19 * http://aguegu.net
BB50 0:f82840dd806a 20 *
BB50 0:f82840dd806a 21 * Inspiré du travail de bodge
BB50 0:f82840dd806a 22 * https://github.com/bogde/HX711
BB50 0:f82840dd806a 23 *
BB50 0:f82840dd806a 24 */
BB50 0:f82840dd806a 25
BB50 0:f82840dd806a 26 #include "HX711.h"
BB50 0:f82840dd806a 27 #include "mbed.h"
BB50 0:f82840dd806a 28
BB50 0:f82840dd806a 29 #define SCALE_VALUE 259.79 //multiple propre à chaque hardware
BB50 0:f82840dd806a 30
BB50 0:f82840dd806a 31 HX711::HX711(PinName pinData, PinName pinSck, uint8_t gain)
BB50 0:f82840dd806a 32 {
BB50 0:f82840dd806a 33 _pinData = pinData;
BB50 0:f82840dd806a 34 _pinSck = pinSck;
BB50 0:f82840dd806a 35
BB50 0:f82840dd806a 36 this->setGain(gain);
BB50 0:f82840dd806a 37
BB50 0:f82840dd806a 38 DigitalOut sck(_pinSck);
BB50 0:f82840dd806a 39 DigitalIn data(_pinData);
BB50 0:f82840dd806a 40
BB50 0:f82840dd806a 41 sck = 1; //Initialisation HX711
BB50 0:f82840dd806a 42 wait_us(100);
BB50 0:f82840dd806a 43 sck = 0;
BB50 0:f82840dd806a 44
BB50 0:f82840dd806a 45 this->setOffset(averageValue(10)); //TARE de la balance
BB50 0:f82840dd806a 46 this->setScale(SCALE_VALUE); //Réglage du valeur du SCALE
BB50 0:f82840dd806a 47 }
BB50 0:f82840dd806a 48
BB50 0:f82840dd806a 49 HX711::~HX711()
BB50 0:f82840dd806a 50 {
BB50 0:f82840dd806a 51
BB50 0:f82840dd806a 52 }
BB50 0:f82840dd806a 53
BB50 0:f82840dd806a 54 int HX711::averageValue(uint8_t times) //Calcule une moyenne sur plusieurs mesures
BB50 0:f82840dd806a 55 {
BB50 0:f82840dd806a 56 int sum = 0;
BB50 0:f82840dd806a 57 for (int i = 0; i < times; i++)
BB50 0:f82840dd806a 58 {
BB50 0:f82840dd806a 59 sum += getValue();
BB50 0:f82840dd806a 60 }
BB50 0:f82840dd806a 61
BB50 0:f82840dd806a 62 return sum / times;
BB50 0:f82840dd806a 63 }
BB50 0:f82840dd806a 64
BB50 0:f82840dd806a 65 int HX711::getValue() //Obtenir la valeur brut du controller
BB50 0:f82840dd806a 66 {
BB50 0:f82840dd806a 67 int buffer;
BB50 0:f82840dd806a 68 buffer = 0 ;
BB50 0:f82840dd806a 69
BB50 0:f82840dd806a 70 DigitalOut sck(_pinSck);
BB50 0:f82840dd806a 71 DigitalIn data(_pinData);
BB50 0:f82840dd806a 72
BB50 0:f82840dd806a 73 while (data.read()==1) //wait is ready
BB50 0:f82840dd806a 74 ;
BB50 0:f82840dd806a 75
BB50 0:f82840dd806a 76 for (uint8_t i = 24; i--;) //read 24 bit 1 per 1 and save to buffer
BB50 0:f82840dd806a 77 {
BB50 0:f82840dd806a 78 sck=1;
BB50 0:f82840dd806a 79
BB50 0:f82840dd806a 80 buffer = buffer << 1 ;
BB50 0:f82840dd806a 81
BB50 0:f82840dd806a 82 if (data.read())
BB50 0:f82840dd806a 83 {
BB50 0:f82840dd806a 84 buffer ++;
BB50 0:f82840dd806a 85 }
BB50 0:f82840dd806a 86
BB50 0:f82840dd806a 87 sck=0;
BB50 0:f82840dd806a 88 }
BB50 0:f82840dd806a 89
BB50 0:f82840dd806a 90 for (int i = 0; i < _gain; i++)
BB50 0:f82840dd806a 91 {
BB50 0:f82840dd806a 92 sck=1;
BB50 0:f82840dd806a 93 sck=0;
BB50 0:f82840dd806a 94 }
BB50 0:f82840dd806a 95
BB50 0:f82840dd806a 96 buffer = buffer ^ 0x800000;
BB50 0:f82840dd806a 97
BB50 0:f82840dd806a 98 return buffer;
BB50 0:f82840dd806a 99 }
BB50 0:f82840dd806a 100
BB50 0:f82840dd806a 101 void HX711::setOffset(int offset)
BB50 0:f82840dd806a 102 {
BB50 0:f82840dd806a 103 _offset = offset;
BB50 0:f82840dd806a 104 }
BB50 0:f82840dd806a 105
BB50 0:f82840dd806a 106 void HX711::setScale(float scale)
BB50 0:f82840dd806a 107 {
BB50 0:f82840dd806a 108 _scale = scale;
BB50 0:f82840dd806a 109 }
BB50 0:f82840dd806a 110
BB50 0:f82840dd806a 111 float HX711::getGram()
BB50 0:f82840dd806a 112 {
BB50 0:f82840dd806a 113 long val = (averageValue(2) - _offset);
BB50 0:f82840dd806a 114 return (float) val / _scale;
BB50 0:f82840dd806a 115 }
BB50 0:f82840dd806a 116
BB50 0:f82840dd806a 117 void HX711::setGain(uint8_t gain)
BB50 0:f82840dd806a 118 {
BB50 0:f82840dd806a 119 switch (gain)
BB50 0:f82840dd806a 120 {
BB50 0:f82840dd806a 121 case 128: // channel A, gain factor 128
BB50 0:f82840dd806a 122 _gain = 1;
BB50 0:f82840dd806a 123 break;
BB50 0:f82840dd806a 124
BB50 0:f82840dd806a 125 case 64: // channel A, gain factor 64
BB50 0:f82840dd806a 126 _gain = 3;
BB50 0:f82840dd806a 127 break;
BB50 0:f82840dd806a 128
BB50 0:f82840dd806a 129 case 32: // channel B, gain factor 32
BB50 0:f82840dd806a 130 _gain = 2;
BB50 0:f82840dd806a 131 break;
BB50 0:f82840dd806a 132 }
BB50 0:f82840dd806a 133 DigitalOut sck(_pinSck);
BB50 0:f82840dd806a 134 sck = 0;
BB50 0:f82840dd806a 135 getValue();
BB50 0:f82840dd806a 136 }
BB50 0:f82840dd806a 137
BB50 0:f82840dd806a 138 void HX711::powerDown()
BB50 0:f82840dd806a 139 {
BB50 0:f82840dd806a 140 DigitalOut sck(_pinSck);
BB50 0:f82840dd806a 141 sck = 0;
BB50 0:f82840dd806a 142 sck = 1;
BB50 0:f82840dd806a 143 }
BB50 0:f82840dd806a 144
BB50 0:f82840dd806a 145 void HX711::powerUp()
BB50 0:f82840dd806a 146 {
BB50 0:f82840dd806a 147 DigitalOut sck(_pinSck);
BB50 0:f82840dd806a 148 sck = 0;
BB50 0:f82840dd806a 149 }
BB50 0:f82840dd806a 150
BB50 0:f82840dd806a 151 void HX711::tare(uint8_t times)
BB50 0:f82840dd806a 152 {
BB50 0:f82840dd806a 153 int sum = averageValue(times);
BB50 0:f82840dd806a 154 setOffset(sum);
BB50 0:f82840dd806a 155 }