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

Files at this revision

API Documentation at this revision

Comitter:
seajayshore
Date:
Tue Jul 26 16:26:35 2016 +0000
Parent:
7:c9a0ce000a18
Commit message:
Refactoring & re-organisation with true C++ constructors.

Changed in this revision

HY3116.cpp Show annotated file Show diff for this revision Revisions of this file
HY3116.h Show annotated file Show diff for this revision Revisions of this file
diff -r c9a0ce000a18 -r 530aad60c490 HY3116.cpp
--- a/HY3116.cpp	Mon Jul 25 21:23:56 2016 +0000
+++ b/HY3116.cpp	Tue Jul 26 16:26:35 2016 +0000
@@ -1,78 +1,107 @@
 #include "HY3116.h"
 
-I2C i2c(p22, p21);
-DigitalIn adcIrq(ADC_IRQ);
-//I2C i2c(p6, p5);
+/**
+ * Default Constructor.
+ * Sets default I2C pins with default frequency
+ */
+HY3116::HY3116() : 
+    i2c(p22, p21)
+{
+    
+    // Set the I2C clock frequency
+    i2c.frequency(250000);
+} // End constructor
 
-///**
-// * Constructor.
-// * Prepares the output pins.
-// */
-//HY3116::HY3116(PinName sda,
-//               PinName scl) : i2c(sda, scl)
-//{
-//    
-//    // Set the I2C clock frequency
-//    i2c.frequency(100000);
-//} // End constructor
+/**
+ * Constructor #2
+ * Sets I2C pins with default frequency
+ */
+HY3116::HY3116(PinName sda,
+               PinName scl) : 
+    i2c(sda, scl)
+{
+    
+    // Set the I2C clock frequency
+    i2c.frequency(250000);
+} // End constructor
+
+/**
+ * Constructor #3
+ * Sets I2C pins and non-default frequency
+ */
+HY3116::HY3116(PinName sda,
+               PinName scl,
+               int freq   ) : 
+    i2c(sda, scl)
+{
+    
+    // Set the I2C clock frequency
+    i2c.frequency(freq);
+} // End constructor
 
-///**
-// * Destructor.
-// */
-//HY3116::~HY3116()
-//{
-//
-//}
+/**
+ * Destructor.
+ */
+HY3116::~HY3116()
+{
+
+}
+
+/**
+ * Helper function to write correct data to HY3116 registers
+ */
+int HY3116::writeRegister(uint8_t regAddress, uint8_t writeData)
+{
+    int retval = 1;
+    char writeBuffer[2];
+    writeBuffer[0]=regAddress;
+    writeBuffer[1]=writeData;
+    retval = i2c.write(HY3116_ADDRESS,writeBuffer,sizeof(writeBuffer),0);
+    if (retval != 0) {
+        return retval;
+    }
+    return 0;
+}
 
-// Single-byte write helper function
-int HY3116::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
+/**
+ * Helper function to write correct data to HY3116 registers
+ */
+int HY3116::readRegister(uint8_t regAddress, uint8_t byteNum, uint8_t* dest)
 {
-    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;
+    int retval = 1;
+    if (byteNum > sizeof(dest)) {
+        return retval;
     }
-    return  0;
+    char writeBuffer[1];
+    writeBuffer[0] = regAddress;
+    char readBuffer[byteNum];
+    retval = i2c.write(HY3116_ADDRESS,writeBuffer,sizeof(writeBuffer),1);
+    if (retval != 0) {
+        return retval;
+    }
+    retval = i2c.read(HY3116_ADDRESS,readBuffer,sizeof(readBuffer),0);
+    if (retval != 0) {
+        return retval;
+    }
+    for(int i=0; i<byteNum; i++) {
+        dest[i]=readBuffer[i];
+    }
+    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) {
+    int retval = 0;
+    char writeBuffer[1];
+    writeBuffer[0] = RESET;
+    retval = i2c.write(RESET_ADDRESS,writeBuffer,sizeof(writeBuffer),0);
+    if (retval != 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)
 {
@@ -83,11 +112,8 @@
     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);
+    adc_error = readRegister(ADO, 3, &rawData[0]);
     if (adc_error != 0) {
             returnError = 2;
             return returnError;
@@ -126,32 +152,36 @@
 {
     int adc_error = 0;
     
-    // Set the I2C clock frequency
-    i2c.frequency(100000);
+    // Reset the chip
+    adc_error = resetChip();
+    if (adc_error != 0) {
+            return 0;
+    }
+    wait_ms(1);
     
     // Set-up the SYS register
-    adc_error = writeByte(HY3116_ADDRESS, SYS, 0b00011100); // Enable the ADC & LDO
+    adc_error = writeRegister(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
+    adc_error = writeRegister(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
+    adc_error = writeRegister(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
+    adc_error = writeRegister(ADC3, 0b00111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 2x
     if (adc_error != 0) {
             return 0;
     }
@@ -163,17 +193,11 @@
     // [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);
+    adc_error = writeRegister(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;
 }
\ No newline at end of file
diff -r c9a0ce000a18 -r 530aad60c490 HY3116.h
--- a/HY3116.h	Mon Jul 25 21:23:56 2016 +0000
+++ b/HY3116.h	Tue Jul 26 16:26:35 2016 +0000
@@ -1,29 +1,40 @@
 #include "main.h"
 #include "mbed.h"
 
+/**
+ * Define I2C addresses for the device
+ */
 #define HY3116_ADDRESS 0xA0
+#define RESET_ADDRESS  0x00
 
-#define SYS 0x00
-#define ADC1 0x01
-#define ADC2 0x02
-#define ADC3 0x03
-#define ADC4 0x04
-#define ADO 0x05
-#define RESET 0x06
+/**
+ * Define settings & I2C command registers
+ */
+#define SYS     0x00
+#define ADC1    0x01
+#define ADC2    0x02
+#define ADC3    0x03
+#define ADC4    0x04
+#define ADO     0x05
+#define RESET   0x06
 
-/* Function Prototypes */
+/**
+ * HY3116 class definition
+ */
 class HY3116 
 {
 public:
-//    HY3116(PinName sda, PinName scl);
-//    ~HY3116();
-    int writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
-    int resetChip();
-    int readBytes(uint8_t address, uint8_t subAddress, uint8_t byteNum, uint8_t* dest, bool alreadyRead);
+    HY3116();
+    HY3116(PinName sda, PinName scl);
+    HY3116(PinName sda, PinName scl, int freq);
+    ~HY3116();
     uint8_t readAdc(int32_t *_adcReading);
     bool init();
     
 private:
-//    I2C i2c;
+    I2C i2c;
     
+    int writeRegister(uint8_t regAddress, uint8_t data);
+    int readRegister(uint8_t regAddress, uint8_t byteNum, uint8_t* dest);
+    int resetChip();
 };