Library for working with the HYCON HY3116/8 24-bit weigh-scales ADC series.

HY3116.cpp

Committer:
seajayshore
Date:
2016-06-28
Revision:
4:1571244a8486
Parent:
3:535ed9a0ce59
Child:
5:3fdbe36e5057

File content as of revision 4:1571244a8486:

#include "HY3116.h"

/**
 * 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
void HY3116::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
    char data_write[2];
    data_write[0]=subAddress;           // I2C sends MSB first. Namely  >>|subAddress|>>|data|
    data_write[1]=data;
    i2c.write(address,data_write,2,0);  // i2c.write(int address, char* data, int length, bool repeated=false);  
}

// Function to send the reset command
void HY3116::resetChip()
{
    char data_write[1];
    data_write[0]=RESET;
    i2c.write(HY3116_ADDRESS,data_write,1,0);  // i2c.write(int address, char* data, int length, bool repeated=false);  
}

// Multi-byte read helper function
void HY3116::readBytes(uint8_t address, uint8_t subAddress, uint8_t byteNum, uint8_t* dest, bool alreadyRead)
{
    char data[14],data_write[1];  
    data_write[0]=subAddress;
    if (!alreadyRead) {     
        i2c.write(address,data_write,1,1);
    }
    i2c.read(address,data,byteNum,0);
    for(int i=0;i<byteNum;i++)         // equate the addresses
        dest[i]=data[i];
}

// Dedicated ADC-output read, check & format function
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 = adcReading;
    
    return newReading;
}

// Initialise the HY3116 with the following config.:
//
void HY3116::init()
{
    
    // Set-up the SYS register
    writeByte(HY3116_ADDRESS, SYS, 0b00011100); // Enable the ADC & LDO
    wait_ms(1); // wait 100 ms to stabilize 
        
    // Set-up the ADC1 register
    writeByte(HY3116_ADDRESS, ADC1, 0b00001000); // Set inputs to AIN1 & AIN2
    wait_ms(1); // wait 100 ms to stabilize
    
    // Set-up the ADC2 register
    writeByte(HY3116_ADDRESS, ADC2, 0b01010000); // Set pos. ref. voltage to VDDA, neg. ref. to VSSA, DC offset to 0VRef
    wait_ms(1); // wait 100 ms to stabilize
    
    // Set-up the ADC3 register
    writeByte(HY3116_ADDRESS, ADC3, 0b00111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 2x
    wait_ms(1); // wait 100 ms to stabilize
    
    // Set-up the ADC4 register
    writeByte(HY3116_ADDRESS, ADC4, 0b01001100); // Set LDO to 3.0V, REFO = 1.2V, conv. rate = 327kHz, SPS = 40
    wait_ms(1); // wait 100 ms to stabilize
    
    resetChip();
    wait_ms(1);
}