ACM txt out

Dependents:   loadcell_outF

Committer:
j8813
Date:
Thu Feb 28 03:35:57 2019 +0000
Revision:
0:33851f8d4544
ACM txt out

Who changed what in which revision?

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