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