Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Lendo_Tensao_HX711_V1
HX711.cpp
00001 /** 00002 * @brief HX711.h 00003 * @details 24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales. 00004 * Function file. 00005 * 00006 * 00007 * @return NA 00008 * 00009 * @author Manuel Caballero 00010 * @date 11/September/2017 00011 * @version 11/September/2017 The ORIGIN 00012 * @pre NaN. 00013 * @warning NaN 00014 * @pre This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ). 00015 */ 00016 00017 #include "HX711.h" 00018 00019 00020 HX711::HX711 ( PinName PD_SCK, PinName DOUT ) 00021 : _PD_SCK ( PD_SCK ) 00022 , _DOUT ( DOUT ) 00023 { 00024 00025 } 00026 00027 00028 HX711::~HX711() 00029 { 00030 } 00031 00032 00033 00034 /** 00035 * @brief HX711_Reset ( void ) 00036 * 00037 * @details It performs an internal reset. 00038 * 00039 * @param[in] NaN. 00040 * 00041 * @param[out] NaN. 00042 * 00043 * 00044 * @return Status of HX711_Reset. 00045 * 00046 * 00047 * @author Manuel Caballero 00048 * @date 11/September/2017 00049 * @version 11/September/2017 The ORIGIN 00050 * @pre When PD_SCK pin changes from low to high and stays at high for 00051 * longer than 60μs, HX711 enters power down mode. 00052 * 00053 * When PD_SCK returns to low, chip will reset and enter normal 00054 * operation mode. 00055 * @warning NaN. 00056 */ 00057 HX711::HX711_status_t HX711::HX711_Reset ( void ) 00058 { 00059 _PD_SCK = HX711_PIN_HIGH; 00060 wait_us ( 120 ); // Datasheet p5. At least 60us ( Security Factor: 2*60us = 120us ) 00061 _PD_SCK = HX711_PIN_LOW; 00062 00063 00064 00065 if ( _DOUT == HX711_PIN_HIGH ) 00066 return HX711_SUCCESS; 00067 else 00068 return HX711_FAILURE; 00069 } 00070 00071 00072 00073 /** 00074 * @brief HX711_PowerDown ( void ) 00075 * 00076 * @details It puts the device in power-down mode. 00077 * 00078 * @param[in] NaN. 00079 * 00080 * @param[out] NaN. 00081 * 00082 * 00083 * @return Status of HX711_PowerDown. 00084 * 00085 * 00086 * @author Manuel Caballero 00087 * @date 11/September/2017 00088 * @version 11/September/2017 The ORIGIN 00089 * @pre When PD_SCK pin changes from low to high and stays at high for 00090 * longer than 60μs, HX711 enters power down mode. 00091 * @warning NaN. 00092 */ 00093 HX711::HX711_status_t HX711::HX711_PowerDown ( void ) 00094 { 00095 _PD_SCK = HX711_PIN_HIGH; 00096 wait_us ( 120 ); // Datasheet p5. At least 60us ( Security Factor: 2*60us = 120us ) 00097 00098 00099 00100 if ( _DOUT == HX711_PIN_HIGH ) 00101 return HX711_SUCCESS; 00102 else 00103 return HX711_FAILURE; 00104 } 00105 00106 00107 00108 /** 00109 * @brief HX711_SetChannelAndGain ( HX711_channel_gain_t myChannel_Gain ) 00110 * 00111 * @details It sets both the channel and the gain for the next measurement. 00112 * 00113 * @param[in] myChannel_Gain: Channel and Gain to perform the new measurement. 00114 * 00115 * @param[out] NaN. 00116 * 00117 * 00118 * @return Status of HX711_SetChannelAndGain. 00119 * 00120 * 00121 * @author Manuel Caballero 00122 * @date 11/September/2017 00123 * @version 11/September/2017 The ORIGIN 00124 * @pre NaN. 00125 * @warning NaN. 00126 */ 00127 HX711::HX711_status_t HX711::HX711_SetChannelAndGain ( HX711_channel_gain_t myChannel_Gain ) 00128 { 00129 uint32_t myPulses = 0; 00130 uint32_t i = 0; // Counter and timeout variable 00131 00132 // Select the gain/channel 00133 switch ( myChannel_Gain ) { 00134 default: 00135 case CHANNEL_A_GAIN_128 : 00136 _HX711_CHANNEL_GAIN = CHANNEL_A_GAIN_128 ; // Update the gain parameter 00137 myPulses = 25; 00138 break; 00139 00140 case CHANNEL_B_GAIN_32 : 00141 _HX711_CHANNEL_GAIN = CHANNEL_B_GAIN_32 ; // Update the gain parameter 00142 myPulses = 26; 00143 break; 00144 00145 case CHANNEL_A_GAIN_64 : 00146 _HX711_CHANNEL_GAIN = CHANNEL_A_GAIN_64 ; // Update the gain parameter 00147 myPulses = 27; 00148 break; 00149 } 00150 00151 00152 // Wait until the device is ready or timeout 00153 i = 23232323; 00154 _PD_SCK = HX711_PIN_LOW; 00155 while ( ( _DOUT == HX711_PIN_HIGH ) && ( --i ) ); 00156 00157 // Check if something is wrong with the device because of the timeout 00158 if ( i < 1 ) 00159 return HX711_FAILURE; 00160 00161 // Change the gain for the NEXT mesurement ( previous data will be ignored ) 00162 do { 00163 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00164 _PD_SCK = HX711_PIN_HIGH; 00165 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00166 _PD_SCK = HX711_PIN_LOW; 00167 00168 myPulses--; 00169 } while ( myPulses > 0 ); 00170 00171 00172 00173 00174 if ( _DOUT == HX711_PIN_HIGH ) 00175 return HX711_SUCCESS; 00176 else 00177 return HX711_FAILURE; 00178 } 00179 00180 00181 00182 /** 00183 * @brief HX711_GetChannelAndGain ( void ) 00184 * 00185 * @details It gets both the channel and the gain for the current measurement. 00186 * 00187 * @param[in] NaN. 00188 * 00189 * @param[out] NaN. 00190 * 00191 * 00192 * @return Channel and Gain. 00193 * 00194 * 00195 * @author Manuel Caballero 00196 * @date 12/September/2017 00197 * @version 12/September/2017 The ORIGIN 00198 * @pre NaN. 00199 * @warning NaN. 00200 */ 00201 HX711::HX711_channel_gain_t HX711::HX711_GetChannelAndGain ( void ) 00202 { 00203 return _HX711_CHANNEL_GAIN; 00204 } 00205 00206 00207 00208 /** 00209 * @brief HX711_ReadRawData ( HX711_channel_gain_t myChannel_Gain, Vector_count_t*, uint32_t ) 00210 * 00211 * @details It reads the raw data from the device according to the channel 00212 * and its gain. 00213 * 00214 * @param[in] myChannel_Gain: Channel and Gain to perform the new read. 00215 * @param[in] myAverage: How many measurement we have to get and deliver the average. 00216 * 00217 * @param[out] myNewRawData: The new value from the device. 00218 * 00219 * 00220 * @return Status of HX711_ReadRawData. 00221 * 00222 * 00223 * @author Manuel Caballero 00224 * @date 11/September/2017 00225 * @version 12/September/2017 Gain mode was fixed, now it gets the value 00226 * a given gain/channel. A timeout was added to 00227 * avoid the microcontroller gets stuck. 00228 * 11/September/2017 The ORIGIN 00229 * @pre NaN. 00230 * @warning NaN. 00231 */ 00232 HX711::HX711_status_t HX711::HX711_ReadRawData ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage ) 00233 { 00234 uint32_t i = 0; // Counter and timeout variable 00235 uint32_t ii = 0; // Counter variable 00236 uint32_t myAuxData = 0; 00237 uint32_t myPulses = 0; 00238 00239 00240 00241 myNewRawData->myRawValue = 0; // Reset variable at the beginning 00242 00243 // Check the gain if it is different, update it ( previous data will be ignored! ) 00244 if ( myChannel_Gain != _HX711_CHANNEL_GAIN ) 00245 HX711_SetChannelAndGain ( myChannel_Gain ); 00246 00247 00248 // Start collecting the new measurement as many as myAverage 00249 for ( ii = 0; ii < myAverage; ii++ ) { 00250 // Reset the value 00251 myAuxData = 0; 00252 00253 // Wait until the device is ready or timeout 00254 i = 23232323; 00255 _PD_SCK = HX711_PIN_LOW; 00256 while ( ( _DOUT == HX711_PIN_HIGH ) && ( --i ) ); 00257 00258 // Check if something is wrong with the device because of the timeout 00259 if ( i < 1 ) 00260 return HX711_FAILURE; 00261 00262 00263 // Read the data 00264 for ( i = 0; i < 24; i++ ) { 00265 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00266 _PD_SCK = HX711_PIN_HIGH; 00267 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00268 myAuxData <<= 1; 00269 _PD_SCK = HX711_PIN_LOW; 00270 00271 // High or Low bit 00272 if ( _DOUT == HX711_PIN_HIGH ) 00273 myAuxData++; 00274 } 00275 00276 // Last bit to release the bus 00277 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00278 _PD_SCK = HX711_PIN_HIGH; 00279 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00280 _PD_SCK = HX711_PIN_LOW; 00281 00282 00283 // Depending on the Gain we have to generate more CLK pulses 00284 switch ( _HX711_CHANNEL_GAIN ) { 00285 default: 00286 case CHANNEL_A_GAIN_128 : 00287 myPulses = 25; 00288 break; 00289 00290 case CHANNEL_B_GAIN_32 : 00291 myPulses = 26; 00292 break; 00293 00294 case CHANNEL_A_GAIN_64 : 00295 myPulses = 27; 00296 break; 00297 } 00298 00299 // Generate those extra pulses for the next measurement 00300 for ( i = 25; i < myPulses; i++ ) { 00301 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00302 _PD_SCK = HX711_PIN_HIGH; 00303 wait_us ( 1 ); // Datasheet p5. T3 and T4 ( Min. 0.2us | Typ. 1us ) 00304 _PD_SCK = HX711_PIN_LOW; 00305 } 00306 00307 // Update data to get the average 00308 myAuxData ^= 0x800000; 00309 myNewRawData->myRawValue += myAuxData; 00310 } 00311 00312 myNewRawData->myRawValue /= (float)myAverage; 00313 00314 00315 00316 if ( _DOUT == HX711_PIN_HIGH ) 00317 return HX711_SUCCESS; 00318 else 00319 return HX711_FAILURE; 00320 }
Generated on Thu Dec 8 2022 17:18:10 by
