test publish

Dependencies:   BLE_API nRF51822 mbed

Fork of KS7 by masaaki makabe

Committer:
masaaki_makabe
Date:
Fri Aug 12 04:07:00 2016 +0000
Branch:
KS3
Revision:
36:3cca74c30551
Parent:
31:b5e19d153db4
???????????????????????????????

Who changed what in which revision?

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