Library for working with the HYCON HY3116/8 24-bit weigh-scales ADC series.
HY3116.cpp
- Committer:
- seajayshore
- Date:
- 2016-07-25
- Revision:
- 7:c9a0ce000a18
- Parent:
- 6:435b50250491
- Child:
- 8:530aad60c490
File content as of revision 7:c9a0ce000a18:
#include "HY3116.h" I2C i2c(p22, p21); DigitalIn adcIrq(ADC_IRQ); //I2C i2c(p6, p5); ///** // * Constructor. // * Prepares the output pins. // */ //HY3116::HY3116(PinName sda, // PinName scl) : i2c(sda, scl) //{ // // // Set the I2C clock frequency // i2c.frequency(100000); //} // End constructor ///** // * Destructor. // */ //HY3116::~HY3116() //{ // //} // Single-byte write helper function int HY3116::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) { int write_error = 0; char data_write[2]; data_write[0]=subAddress; // I2C sends MSB first. Namely >>|subAddress|>>|data| data_write[1]=data; write_error = i2c.write(address,data_write,2,0); // i2c.write(int address, char* data, int length, bool repeated=false); if (write_error != 0) { return 1; } return 0; } // Function to send the reset command int HY3116::resetChip() { int write_error = 0; char data_write[1]; data_write[0]=RESET; write_error = i2c.write(HY3116_ADDRESS,data_write,1,0); // i2c.write(int address, char* data, int length, bool repeated=false); if (write_error != 0) { return 1; } return 0; } // Multi-byte read helper function int HY3116::readBytes(uint8_t address, uint8_t subAddress, uint8_t byteNum, uint8_t* dest, bool alreadyRead) { int write_error = 0; int read_error = 0; char data[14],data_write[1]; data_write[0]=subAddress; if (!alreadyRead) { write_error = i2c.write(address,data_write,1,1); } if (write_error != 0) { return 1; } read_error = i2c.read(address,data,byteNum,0); if (read_error != 0) { return 1; } for(int i=0;i<byteNum;i++) // equate the addresses dest[i]=data[i]; return 0; } // Dedicated ADC-output read, check & format function uint8_t HY3116::readAdc(int32_t *_adcReading) { // Initialise function variables uint8_t rawData[3]; bool newReading = 0; int32_t adcReading = 0; uint8_t adc_error = 0; uint8_t returnError = 0; // Change this to improve I2C speed slightly (see datasheet) bool alreadyRead = 0; // Read in the raw ADO bytes adc_error = readBytes(HY3116_ADDRESS, ADO, 3, &rawData[0], alreadyRead); if (adc_error != 0) { returnError = 2; return returnError; } // Test if there is new data (polling mode) if (rawData[2] & 0b00000001) { // Set the newReading flag returnError = 0; // 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 returnError = 1; } *_adcReading = adcReading; return returnError; } // Initialise the HY3116 with the following config.: // bool HY3116::init() { int adc_error = 0; // Set the I2C clock frequency i2c.frequency(100000); // Set-up the SYS register adc_error = writeByte(HY3116_ADDRESS, SYS, 0b00011100); // Enable the ADC & LDO if (adc_error != 0) { return 0; } wait_ms(1); // wait 100 ms to stabilize // Set-up the ADC1 register adc_error = writeByte(HY3116_ADDRESS, ADC1, 0b00001000); // Set inputs to AIN1 & AIN2 if (adc_error != 0) { return 0; } wait_ms(1); // wait 100 ms to stabilize // Set-up the ADC2 register adc_error = writeByte(HY3116_ADDRESS, ADC2, 0b01010000); // Set pos. ref. voltage to VDDA, neg. ref. to VSSA, DC offset to 0VRef if (adc_error != 0) { return 0; } wait_ms(1); // wait 100 ms to stabilize // Set-up the ADC3 register adc_error = writeByte(HY3116_ADDRESS, ADC3, 0b00111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 2x if (adc_error != 0) { return 0; } wait_ms(1); // wait 100 ms to stabilize // Set-up the ADC4 register // [0:1] = LDO voltage select, 01 = 3.0V // [2] = REFO voltage, 0 = 1.2V // [3] = HS conversion rate, 0 = slow (327kHz) // [4:6] = OSR ADC output rate, 110 = 40SPS (when HS = 0) // [7] = N/A adc_error = writeByte(HY3116_ADDRESS, ADC4, 0b01001100); if (adc_error != 0) { return 0; } wait_ms(1); // Wait to stabilise adc_error = resetChip(); if (adc_error != 0) { return 0; } wait_ms(1); return 1; }