ACM experimental

Dependents:   loadcell_2

Committer:
j8813
Date:
Thu Feb 28 02:43:30 2019 +0000
Revision:
0:fa474b1aef70
ACM experimental

Who changed what in which revision?

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