ACM

Dependents:   loadcell

Committer:
j8813
Date:
Thu Feb 28 02:28:24 2019 +0000
Revision:
0:93a883c6277d
ACM

Who changed what in which revision?

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