Library for working with the HYCON HY3116/8 24-bit weigh-scales ADC series.
HY3116.cpp@3:535ed9a0ce59, 2016-06-28 (annotated)
- Committer:
- seajayshore
- Date:
- Tue Jun 28 13:27:09 2016 +0000
- Revision:
- 3:535ed9a0ce59
- Parent:
- 2:2da9fbab02b7
- Child:
- 4:1571244a8486
Post-China revision, trying to use proper constructors now, incomplete
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
seajayshore | 0:80768ca5d5ff | 1 | #include "HY3116.h" |
seajayshore | 0:80768ca5d5ff | 2 | |
seajayshore | 0:80768ca5d5ff | 3 | // Define I2C pins (SDA,SCL) |
seajayshore | 3:535ed9a0ce59 | 4 | //static I2C i2c(p6,p5); // nRF51-DK |
seajayshore | 3:535ed9a0ce59 | 5 | //static I2C i2c(p7,p20); // nRF51-DK |
seajayshore | 3:535ed9a0ce59 | 6 | //static I2C i2c(p22,p21); // nRF51-DK |
seajayshore | 3:535ed9a0ce59 | 7 | |
seajayshore | 3:535ed9a0ce59 | 8 | /** |
seajayshore | 3:535ed9a0ce59 | 9 | * Constructor. |
seajayshore | 3:535ed9a0ce59 | 10 | * Prepares the output pins. |
seajayshore | 3:535ed9a0ce59 | 11 | */ |
seajayshore | 3:535ed9a0ce59 | 12 | HY3116::HY3116(PinName sda, |
seajayshore | 3:535ed9a0ce59 | 13 | PinName scl) |
seajayshore | 3:535ed9a0ce59 | 14 | { |
seajayshore | 3:535ed9a0ce59 | 15 | i2c(sda, scl); |
seajayshore | 3:535ed9a0ce59 | 16 | // Set the I2C clock frequency |
seajayshore | 3:535ed9a0ce59 | 17 | i2c.frequency(100000); |
seajayshore | 3:535ed9a0ce59 | 18 | } // End constructor |
seajayshore | 3:535ed9a0ce59 | 19 | |
seajayshore | 3:535ed9a0ce59 | 20 | /** |
seajayshore | 3:535ed9a0ce59 | 21 | * Destructor. |
seajayshore | 3:535ed9a0ce59 | 22 | */ |
seajayshore | 3:535ed9a0ce59 | 23 | HY3116::~HY3116() |
seajayshore | 3:535ed9a0ce59 | 24 | { |
seajayshore | 3:535ed9a0ce59 | 25 | |
seajayshore | 3:535ed9a0ce59 | 26 | } |
seajayshore | 0:80768ca5d5ff | 27 | |
seajayshore | 0:80768ca5d5ff | 28 | // Single-byte write helper function |
seajayshore | 0:80768ca5d5ff | 29 | void HY3116::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) |
seajayshore | 0:80768ca5d5ff | 30 | { |
seajayshore | 0:80768ca5d5ff | 31 | char data_write[2]; |
seajayshore | 0:80768ca5d5ff | 32 | data_write[0]=subAddress; // I2C sends MSB first. Namely >>|subAddress|>>|data| |
seajayshore | 0:80768ca5d5ff | 33 | data_write[1]=data; |
seajayshore | 0:80768ca5d5ff | 34 | i2c.write(address,data_write,2,0); // i2c.write(int address, char* data, int length, bool repeated=false); |
seajayshore | 0:80768ca5d5ff | 35 | } |
seajayshore | 0:80768ca5d5ff | 36 | |
seajayshore | 0:80768ca5d5ff | 37 | // Function to send the reset command |
seajayshore | 0:80768ca5d5ff | 38 | void HY3116::resetChip() |
seajayshore | 0:80768ca5d5ff | 39 | { |
seajayshore | 0:80768ca5d5ff | 40 | char data_write[1]; |
seajayshore | 0:80768ca5d5ff | 41 | data_write[0]=RESET; |
seajayshore | 0:80768ca5d5ff | 42 | i2c.write(HY3116_ADDRESS,data_write,1,0); // i2c.write(int address, char* data, int length, bool repeated=false); |
seajayshore | 0:80768ca5d5ff | 43 | } |
seajayshore | 0:80768ca5d5ff | 44 | |
seajayshore | 0:80768ca5d5ff | 45 | // Multi-byte read helper function |
seajayshore | 0:80768ca5d5ff | 46 | void HY3116::readBytes(uint8_t address, uint8_t subAddress, uint8_t byteNum, uint8_t* dest, bool alreadyRead) |
seajayshore | 0:80768ca5d5ff | 47 | { |
seajayshore | 0:80768ca5d5ff | 48 | char data[14],data_write[1]; |
seajayshore | 0:80768ca5d5ff | 49 | data_write[0]=subAddress; |
seajayshore | 0:80768ca5d5ff | 50 | if (!alreadyRead) { |
seajayshore | 0:80768ca5d5ff | 51 | i2c.write(address,data_write,1,1); |
seajayshore | 0:80768ca5d5ff | 52 | } |
seajayshore | 0:80768ca5d5ff | 53 | i2c.read(address,data,byteNum,0); |
seajayshore | 0:80768ca5d5ff | 54 | for(int i=0;i<byteNum;i++) // equate the addresses |
seajayshore | 0:80768ca5d5ff | 55 | dest[i]=data[i]; |
seajayshore | 0:80768ca5d5ff | 56 | } |
seajayshore | 0:80768ca5d5ff | 57 | |
seajayshore | 0:80768ca5d5ff | 58 | // Dedicated ADC-output read, check & format function |
seajayshore | 1:1967c3f48465 | 59 | bool HY3116::readAdc(int32_t *_adcReading) |
seajayshore | 0:80768ca5d5ff | 60 | { |
seajayshore | 1:1967c3f48465 | 61 | // Initialise function variables |
seajayshore | 0:80768ca5d5ff | 62 | uint8_t rawData[3]; |
seajayshore | 0:80768ca5d5ff | 63 | bool newReading = 0; |
seajayshore | 0:80768ca5d5ff | 64 | int32_t adcReading = 0; |
seajayshore | 0:80768ca5d5ff | 65 | |
seajayshore | 1:1967c3f48465 | 66 | // Change this to improve I2C speed slightly (see datasheet) |
seajayshore | 1:1967c3f48465 | 67 | bool alreadyRead = 0; |
seajayshore | 1:1967c3f48465 | 68 | |
seajayshore | 1:1967c3f48465 | 69 | // Read in the raw ADO bytes |
seajayshore | 0:80768ca5d5ff | 70 | readBytes(HY3116_ADDRESS, ADO, 3, &rawData[0], alreadyRead); |
seajayshore | 0:80768ca5d5ff | 71 | |
seajayshore | 1:1967c3f48465 | 72 | // Test if there is new data (polling mode) |
seajayshore | 0:80768ca5d5ff | 73 | if (rawData[2] & 0b00000001) { |
seajayshore | 1:1967c3f48465 | 74 | |
seajayshore | 1:1967c3f48465 | 75 | // Set the newReading flag |
seajayshore | 0:80768ca5d5ff | 76 | newReading = 1; |
seajayshore | 1:1967c3f48465 | 77 | |
seajayshore | 1:1967c3f48465 | 78 | // Shift the raw bytes into the 32-bit variable |
seajayshore | 1:1967c3f48465 | 79 | adcReading += rawData[0] << 15; |
seajayshore | 1:1967c3f48465 | 80 | adcReading += rawData[1] << 7; |
seajayshore | 1:1967c3f48465 | 81 | adcReading += rawData[2] >> 1; |
seajayshore | 1:1967c3f48465 | 82 | |
seajayshore | 1:1967c3f48465 | 83 | // Account for twos complement polarity |
seajayshore | 1:1967c3f48465 | 84 | if (rawData[0] & 0b10000000) { |
seajayshore | 1:1967c3f48465 | 85 | adcReading ^= 0xFF800000; |
seajayshore | 1:1967c3f48465 | 86 | } |
seajayshore | 0:80768ca5d5ff | 87 | } |
seajayshore | 0:80768ca5d5ff | 88 | else { |
seajayshore | 1:1967c3f48465 | 89 | |
seajayshore | 1:1967c3f48465 | 90 | // Set the newReading flag |
seajayshore | 0:80768ca5d5ff | 91 | newReading = 0; |
seajayshore | 0:80768ca5d5ff | 92 | } |
seajayshore | 0:80768ca5d5ff | 93 | |
seajayshore | 1:1967c3f48465 | 94 | *_adcReading = adcReading; |
seajayshore | 0:80768ca5d5ff | 95 | |
seajayshore | 1:1967c3f48465 | 96 | return newReading; |
seajayshore | 0:80768ca5d5ff | 97 | } |
seajayshore | 0:80768ca5d5ff | 98 | |
seajayshore | 0:80768ca5d5ff | 99 | // Initialise the HY3116 with the following config.: |
seajayshore | 0:80768ca5d5ff | 100 | // |
seajayshore | 0:80768ca5d5ff | 101 | void HY3116::init() |
seajayshore | 0:80768ca5d5ff | 102 | { |
seajayshore | 0:80768ca5d5ff | 103 | |
seajayshore | 0:80768ca5d5ff | 104 | // Set-up the SYS register |
seajayshore | 0:80768ca5d5ff | 105 | writeByte(HY3116_ADDRESS, SYS, 0b00011100); // Enable the ADC & LDO |
seajayshore | 2:2da9fbab02b7 | 106 | wait_ms(1); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 107 | |
seajayshore | 0:80768ca5d5ff | 108 | // Set-up the ADC1 register |
seajayshore | 0:80768ca5d5ff | 109 | writeByte(HY3116_ADDRESS, ADC1, 0b00001000); // Set inputs to AIN1 & AIN2 |
seajayshore | 2:2da9fbab02b7 | 110 | wait_ms(1); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 111 | |
seajayshore | 0:80768ca5d5ff | 112 | // Set-up the ADC2 register |
seajayshore | 0:80768ca5d5ff | 113 | writeByte(HY3116_ADDRESS, ADC2, 0b01010000); // Set pos. ref. voltage to VDDA, neg. ref. to VSSA, DC offset to 0VRef |
seajayshore | 2:2da9fbab02b7 | 114 | wait_ms(1); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 115 | |
seajayshore | 0:80768ca5d5ff | 116 | // Set-up the ADC3 register |
seajayshore | 2:2da9fbab02b7 | 117 | writeByte(HY3116_ADDRESS, ADC3, 0b00111111); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 2x |
seajayshore | 2:2da9fbab02b7 | 118 | wait_ms(1); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 119 | |
seajayshore | 0:80768ca5d5ff | 120 | // Set-up the ADC4 register |
seajayshore | 2:2da9fbab02b7 | 121 | writeByte(HY3116_ADDRESS, ADC4, 0b01001100); // Set LDO to 3.0V, REFO = 1.2V, conv. rate = 327kHz, SPS = 40 |
seajayshore | 2:2da9fbab02b7 | 122 | wait_ms(1); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 123 | |
seajayshore | 0:80768ca5d5ff | 124 | resetChip(); |
seajayshore | 2:2da9fbab02b7 | 125 | wait_ms(1); |
seajayshore | 0:80768ca5d5ff | 126 | } |