![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
LTC2487/LTC2487.cpp@0:a28a1035c31b, 2018-01-22 (annotated)
- Committer:
- jrodenburg
- Date:
- Mon Jan 22 20:11:52 2018 +0000
- Revision:
- 0:a28a1035c31b
- Child:
- 1:0182b86f9bd4
Code that has I/O working, after replacing the hotswap buffer with a dumb buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrodenburg | 0:a28a1035c31b | 1 | #include "mbed.h" |
jrodenburg | 0:a28a1035c31b | 2 | #include "LTC2487.h" |
jrodenburg | 0:a28a1035c31b | 3 | #include "MODSERIAL.h" |
jrodenburg | 0:a28a1035c31b | 4 | |
jrodenburg | 0:a28a1035c31b | 5 | namespace { |
jrodenburg | 0:a28a1035c31b | 6 | const uint8_t I2C_WRITE = 0x00; |
jrodenburg | 0:a28a1035c31b | 7 | const uint8_t I2C_READ = 0x01; |
jrodenburg | 0:a28a1035c31b | 8 | |
jrodenburg | 0:a28a1035c31b | 9 | //Channel addresses |
jrodenburg | 0:a28a1035c31b | 10 | //0b10110000 ch0 --> 0xb0 |
jrodenburg | 0:a28a1035c31b | 11 | //0b10111000 ch1 --> 0xb8 |
jrodenburg | 0:a28a1035c31b | 12 | //0b10110001 ch2 --> 0xb1 |
jrodenburg | 0:a28a1035c31b | 13 | //0b10111001 ch3 --> 0xb9 |
jrodenburg | 0:a28a1035c31b | 14 | const uint8_t CHNL_0 = 0xb0; |
jrodenburg | 0:a28a1035c31b | 15 | const uint8_t CHNL_1 = 0xb8; |
jrodenburg | 0:a28a1035c31b | 16 | const uint8_t CHNL_2 = 0xb1; |
jrodenburg | 0:a28a1035c31b | 17 | const uint8_t CHNL_3 = 0xb9; |
jrodenburg | 0:a28a1035c31b | 18 | }; |
jrodenburg | 0:a28a1035c31b | 19 | |
jrodenburg | 0:a28a1035c31b | 20 | LTC2487::LTC2487 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){ |
jrodenburg | 0:a28a1035c31b | 21 | addrI2C = address; |
jrodenburg | 0:a28a1035c31b | 22 | i2c.frequency(freq); |
jrodenburg | 0:a28a1035c31b | 23 | } |
jrodenburg | 0:a28a1035c31b | 24 | |
jrodenburg | 0:a28a1035c31b | 25 | void LTC2487::setAddress(int address){ |
jrodenburg | 0:a28a1035c31b | 26 | addrI2C = address; |
jrodenburg | 0:a28a1035c31b | 27 | } |
jrodenburg | 0:a28a1035c31b | 28 | |
jrodenburg | 0:a28a1035c31b | 29 | float LTC2487::readOutput(int chnl){ |
jrodenburg | 0:a28a1035c31b | 30 | char ADC_channel[1]; |
jrodenburg | 0:a28a1035c31b | 31 | char ADC_data_rx[3]; |
jrodenburg | 0:a28a1035c31b | 32 | char ADC_config[1]; |
jrodenburg | 0:a28a1035c31b | 33 | ADC_config[0] = 0x82; |
jrodenburg | 0:a28a1035c31b | 34 | |
jrodenburg | 0:a28a1035c31b | 35 | //select channel to read |
jrodenburg | 0:a28a1035c31b | 36 | switch (chnl){ |
jrodenburg | 0:a28a1035c31b | 37 | case 0: |
jrodenburg | 0:a28a1035c31b | 38 | ADC_channel[0] = CHNL_0; |
jrodenburg | 0:a28a1035c31b | 39 | break; |
jrodenburg | 0:a28a1035c31b | 40 | case 1: |
jrodenburg | 0:a28a1035c31b | 41 | ADC_channel[0] = CHNL_1; |
jrodenburg | 0:a28a1035c31b | 42 | break; |
jrodenburg | 0:a28a1035c31b | 43 | case 2: |
jrodenburg | 0:a28a1035c31b | 44 | ADC_channel[0] = CHNL_2; |
jrodenburg | 0:a28a1035c31b | 45 | break; |
jrodenburg | 0:a28a1035c31b | 46 | case 3: |
jrodenburg | 0:a28a1035c31b | 47 | ADC_channel[0] = CHNL_3; |
jrodenburg | 0:a28a1035c31b | 48 | break; |
jrodenburg | 0:a28a1035c31b | 49 | } |
jrodenburg | 0:a28a1035c31b | 50 | |
jrodenburg | 0:a28a1035c31b | 51 | //send message to select channel |
jrodenburg | 0:a28a1035c31b | 52 | i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_channel, 1); |
jrodenburg | 0:a28a1035c31b | 53 | //must wait, otherwise breaks... |
jrodenburg | 0:a28a1035c31b | 54 | wait(0.3); |
jrodenburg | 0:a28a1035c31b | 55 | //send configuration (1 gain, autocalibration |
jrodenburg | 0:a28a1035c31b | 56 | i2c.write((addrI2C<<1)|(I2C_WRITE), ADC_config, 1); |
jrodenburg | 0:a28a1035c31b | 57 | //must wait, otherwise breaks... |
jrodenburg | 0:a28a1035c31b | 58 | wait(0.3); |
jrodenburg | 0:a28a1035c31b | 59 | //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK |
jrodenburg | 0:a28a1035c31b | 60 | i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3); |
jrodenburg | 0:a28a1035c31b | 61 | //Stich togethor the bytes into a 24bit value |
jrodenburg | 0:a28a1035c31b | 62 | unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2]; |
jrodenburg | 0:a28a1035c31b | 63 | //Delete SIGN bit and MSB bit and remove 6 JUNK bits |
jrodenburg | 0:a28a1035c31b | 64 | unsigned long ADC_Result = (data&0x3fffff)>>6; |
jrodenburg | 0:a28a1035c31b | 65 | |
jrodenburg | 0:a28a1035c31b | 66 | return float(float(ADC_Result)); |
jrodenburg | 0:a28a1035c31b | 67 | |
jrodenburg | 0:a28a1035c31b | 68 | } |