Library for working with the HYCON HY3116/8 24-bit weigh-scales ADC series.
Diff: HY3116.cpp
- Revision:
- 1:1967c3f48465
- Parent:
- 0:80768ca5d5ff
- Child:
- 2:2da9fbab02b7
--- a/HY3116.cpp Wed May 11 12:11:43 2016 +0000 +++ b/HY3116.cpp Thu May 12 13:40:12 2016 +0000 @@ -34,31 +34,44 @@ } // Dedicated ADC-output read, check & format function -int32_t HY3116::readAdc(bool alreadyRead) +bool HY3116::readAdc(int32_t *_adcReading) { + // Initialise function variables uint8_t rawData[3]; bool newReading = 0; int32_t adcReading = 0; + // Change this to improve I2C speed slightly (see datasheet) + bool alreadyRead = 0; + + // Read in the raw ADO bytes readBytes(HY3116_ADDRESS, ADO, 3, &rawData[0], alreadyRead); + // Test if there is new data (polling mode) if (rawData[2] & 0b00000001) { + + // Set the newReading flag newReading = 1; + + // Shift the raw bytes into the 32-bit variable + adcReading += rawData[0] << 15; + adcReading += rawData[1] << 7; + adcReading += rawData[2] >> 1; + + // Account for twos complement polarity + if (rawData[0] & 0b10000000) { + adcReading ^= 0xFF800000; + } } else { + + // Set the newReading flag newReading = 0; } - adcReading += rawData[0] << 15; - adcReading += rawData[1] << 7; - adcReading += rawData[2] >> 1; + *_adcReading = adcReading; - // Account for twos complement polarity - if (rawData[0] & 0b10000000) { - adcReading ^= 0xFF800000; - } - - return adcReading; + return newReading; } // Initialise the HY3116 with the following config.: @@ -81,7 +94,7 @@ wait_ms(100); // wait 100 ms to stabilize // Set-up the ADC3 register - writeByte(HY3116_ADDRESS, ADC3, 0b01111100); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 1x + writeByte(HY3116_ADDRESS, ADC3, 0b01111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 1x wait_ms(100); // wait 100 ms to stabilize // Set-up the ADC4 register @@ -89,5 +102,5 @@ wait_ms(100); // wait 100 ms to stabilize resetChip(); - wait_ms(500); + wait_ms(100); } \ No newline at end of file