Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HY3116.cpp@0:80768ca5d5ff, 2016-05-11 (annotated)
- Committer:
- seajayshore
- Date:
- Wed May 11 12:11:43 2016 +0000
- Revision:
- 0:80768ca5d5ff
- Child:
- 1:1967c3f48465
1st commit! woo!
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 | 0:80768ca5d5ff | 4 | static I2C i2c(p30,p7); // 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 | 0:80768ca5d5ff | 37 | int32_t HY3116::readAdc(bool alreadyRead) |
seajayshore | 0:80768ca5d5ff | 38 | { |
seajayshore | 0:80768ca5d5ff | 39 | uint8_t rawData[3]; |
seajayshore | 0:80768ca5d5ff | 40 | bool newReading = 0; |
seajayshore | 0:80768ca5d5ff | 41 | int32_t adcReading = 0; |
seajayshore | 0:80768ca5d5ff | 42 | |
seajayshore | 0:80768ca5d5ff | 43 | readBytes(HY3116_ADDRESS, ADO, 3, &rawData[0], alreadyRead); |
seajayshore | 0:80768ca5d5ff | 44 | |
seajayshore | 0:80768ca5d5ff | 45 | if (rawData[2] & 0b00000001) { |
seajayshore | 0:80768ca5d5ff | 46 | newReading = 1; |
seajayshore | 0:80768ca5d5ff | 47 | } |
seajayshore | 0:80768ca5d5ff | 48 | else { |
seajayshore | 0:80768ca5d5ff | 49 | newReading = 0; |
seajayshore | 0:80768ca5d5ff | 50 | } |
seajayshore | 0:80768ca5d5ff | 51 | |
seajayshore | 0:80768ca5d5ff | 52 | adcReading += rawData[0] << 15; |
seajayshore | 0:80768ca5d5ff | 53 | adcReading += rawData[1] << 7; |
seajayshore | 0:80768ca5d5ff | 54 | adcReading += rawData[2] >> 1; |
seajayshore | 0:80768ca5d5ff | 55 | |
seajayshore | 0:80768ca5d5ff | 56 | // Account for twos complement polarity |
seajayshore | 0:80768ca5d5ff | 57 | if (rawData[0] & 0b10000000) { |
seajayshore | 0:80768ca5d5ff | 58 | adcReading ^= 0xFF800000; |
seajayshore | 0:80768ca5d5ff | 59 | } |
seajayshore | 0:80768ca5d5ff | 60 | |
seajayshore | 0:80768ca5d5ff | 61 | return adcReading; |
seajayshore | 0:80768ca5d5ff | 62 | } |
seajayshore | 0:80768ca5d5ff | 63 | |
seajayshore | 0:80768ca5d5ff | 64 | // Initialise the HY3116 with the following config.: |
seajayshore | 0:80768ca5d5ff | 65 | // |
seajayshore | 0:80768ca5d5ff | 66 | void HY3116::init() |
seajayshore | 0:80768ca5d5ff | 67 | { |
seajayshore | 0:80768ca5d5ff | 68 | // Set the I2C clock frequency |
seajayshore | 0:80768ca5d5ff | 69 | i2c.frequency(100000); |
seajayshore | 0:80768ca5d5ff | 70 | |
seajayshore | 0:80768ca5d5ff | 71 | // Set-up the SYS register |
seajayshore | 0:80768ca5d5ff | 72 | writeByte(HY3116_ADDRESS, SYS, 0b00011100); // Enable the ADC & LDO |
seajayshore | 0:80768ca5d5ff | 73 | wait_ms(100); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 74 | |
seajayshore | 0:80768ca5d5ff | 75 | // Set-up the ADC1 register |
seajayshore | 0:80768ca5d5ff | 76 | writeByte(HY3116_ADDRESS, ADC1, 0b00001000); // Set inputs to AIN1 & AIN2 |
seajayshore | 0:80768ca5d5ff | 77 | wait_ms(100); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 78 | |
seajayshore | 0:80768ca5d5ff | 79 | // Set-up the ADC2 register |
seajayshore | 0:80768ca5d5ff | 80 | writeByte(HY3116_ADDRESS, ADC2, 0b01010000); // Set pos. ref. voltage to VDDA, neg. ref. to VSSA, DC offset to 0VRef |
seajayshore | 0:80768ca5d5ff | 81 | wait_ms(100); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 82 | |
seajayshore | 0:80768ca5d5ff | 83 | // Set-up the ADC3 register |
seajayshore | 0:80768ca5d5ff | 84 | writeByte(HY3116_ADDRESS, ADC3, 0b01111100); // Set to int. osc. 327kHz, full ref. range, PGA to 32x, pre-amp to 1x |
seajayshore | 0:80768ca5d5ff | 85 | wait_ms(100); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 86 | |
seajayshore | 0:80768ca5d5ff | 87 | // Set-up the ADC4 register |
seajayshore | 0:80768ca5d5ff | 88 | writeByte(HY3116_ADDRESS, ADC4, 0b01011110); // Set LDO to 3.0V, REFO = 1.2V, conv. rate = 327kHz, SPS = 40 |
seajayshore | 0:80768ca5d5ff | 89 | wait_ms(100); // wait 100 ms to stabilize |
seajayshore | 0:80768ca5d5ff | 90 | |
seajayshore | 0:80768ca5d5ff | 91 | resetChip(); |
seajayshore | 0:80768ca5d5ff | 92 | wait_ms(500); |
seajayshore | 0:80768ca5d5ff | 93 | } |