![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Control Code with I/O and ADC working
LTC2487/LTC2487.cpp@21:f87464a7e7c6, 2018-07-18 (annotated)
- Committer:
- jrodenburg
- Date:
- Wed Jul 18 21:28:45 2018 +0000
- Revision:
- 21:f87464a7e7c6
- Parent:
- 20:cdeed4dad690
Code with diagnostics
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 | 1:0182b86f9bd4 | 5 | |
jrodenburg | 0:a28a1035c31b | 6 | namespace { |
jrodenburg | 0:a28a1035c31b | 7 | const uint8_t I2C_WRITE = 0x00; |
jrodenburg | 0:a28a1035c31b | 8 | const uint8_t I2C_READ = 0x01; |
jrodenburg | 20:cdeed4dad690 | 9 | |
jrodenburg | 12:1cada1fe4743 | 10 | //Channel addresses (first two bits are fixed) |
jrodenburg | 12:1cada1fe4743 | 11 | //0b10 EN SGL ODD A2 A1 A0 |
jrodenburg | 12:1cada1fe4743 | 12 | //0b10 1 1 0 0 0 0 ch0 --> 0xb0 |
jrodenburg | 12:1cada1fe4743 | 13 | //0b10 1 1 1 0 0 0 ch1 --> 0xb8 |
jrodenburg | 12:1cada1fe4743 | 14 | //0b10 1 1 0 0 0 1 ch2 --> 0xb1 |
jrodenburg | 12:1cada1fe4743 | 15 | //0b10 1 1 1 0 0 1 ch3 --> 0xb9 |
jrodenburg | 20:cdeed4dad690 | 16 | |
jrodenburg | 12:1cada1fe4743 | 17 | |
jrodenburg | 12:1cada1fe4743 | 18 | //Config Section |
jrodenburg | 12:1cada1fe4743 | 19 | // EN2 IM FA FB SPD GS2 GS1 GS0 |
jrodenburg | 12:1cada1fe4743 | 20 | // 1 0 0 0 0 0 1 0 //Initial |
jrodenburg | 12:1cada1fe4743 | 21 | // 1 0 0 0 1 0 1 1 //New Attempt with same gain (8x) |
jrodenburg | 12:1cada1fe4743 | 22 | // 1 0 0 0 1 0 0 0 //New Attempt with unity gain (1x) |
jrodenburg | 20:cdeed4dad690 | 23 | |
jrodenburg | 20:cdeed4dad690 | 24 | |
jrodenburg | 0:a28a1035c31b | 25 | const uint8_t CHNL_0 = 0xb0; |
jrodenburg | 0:a28a1035c31b | 26 | const uint8_t CHNL_1 = 0xb8; |
jrodenburg | 0:a28a1035c31b | 27 | const uint8_t CHNL_2 = 0xb1; |
jrodenburg | 0:a28a1035c31b | 28 | const uint8_t CHNL_3 = 0xb9; |
jrodenburg | 0:a28a1035c31b | 29 | }; |
jrodenburg | 0:a28a1035c31b | 30 | |
jrodenburg | 0:a28a1035c31b | 31 | LTC2487::LTC2487 (PinName sda, PinName scl, uint8_t address, int freq): i2c( sda, scl ){ |
jrodenburg | 0:a28a1035c31b | 32 | addrI2C = address; |
jrodenburg | 0:a28a1035c31b | 33 | i2c.frequency(freq); |
jrodenburg | 0:a28a1035c31b | 34 | } |
jrodenburg | 0:a28a1035c31b | 35 | |
jrodenburg | 0:a28a1035c31b | 36 | void LTC2487::setAddress(int address){ |
jrodenburg | 0:a28a1035c31b | 37 | addrI2C = address; |
jrodenburg | 0:a28a1035c31b | 38 | } |
jrodenburg | 0:a28a1035c31b | 39 | |
jrodenburg | 20:cdeed4dad690 | 40 | int LTC2487::writePort(int chnl){ |
jrodenburg | 0:a28a1035c31b | 41 | char ADC_channel[1]; |
jrodenburg | 0:a28a1035c31b | 42 | char ADC_config[1]; |
jrodenburg | 12:1cada1fe4743 | 43 | ADC_config[0] = 0b10001000;//0x82; //0b10000010 |
jrodenburg | 13:604e6933366f | 44 | char cmd[2]; |
jrodenburg | 20:cdeed4dad690 | 45 | |
jrodenburg | 0:a28a1035c31b | 46 | //select channel to read |
jrodenburg | 0:a28a1035c31b | 47 | switch (chnl){ |
jrodenburg | 0:a28a1035c31b | 48 | case 0: |
jrodenburg | 0:a28a1035c31b | 49 | ADC_channel[0] = CHNL_0; |
jrodenburg | 0:a28a1035c31b | 50 | break; |
jrodenburg | 0:a28a1035c31b | 51 | case 1: |
jrodenburg | 0:a28a1035c31b | 52 | ADC_channel[0] = CHNL_1; |
jrodenburg | 0:a28a1035c31b | 53 | break; |
jrodenburg | 0:a28a1035c31b | 54 | case 2: |
jrodenburg | 0:a28a1035c31b | 55 | ADC_channel[0] = CHNL_2; |
jrodenburg | 0:a28a1035c31b | 56 | break; |
jrodenburg | 0:a28a1035c31b | 57 | case 3: |
jrodenburg | 0:a28a1035c31b | 58 | ADC_channel[0] = CHNL_3; |
jrodenburg | 0:a28a1035c31b | 59 | break; |
jrodenburg | 20:cdeed4dad690 | 60 | } |
jrodenburg | 20:cdeed4dad690 | 61 | |
jrodenburg | 13:604e6933366f | 62 | cmd[0] = ADC_channel[0]; |
jrodenburg | 13:604e6933366f | 63 | cmd[1] = ADC_config[0]; |
jrodenburg | 20:cdeed4dad690 | 64 | |
jrodenburg | 20:cdeed4dad690 | 65 | return i2c.write((addrI2C<<1)|(I2C_WRITE), cmd, 2); |
jrodenburg | 16:82d941b1ef21 | 66 | } |
jrodenburg | 16:82d941b1ef21 | 67 | |
jrodenburg | 16:82d941b1ef21 | 68 | float LTC2487::read(){ |
jrodenburg | 16:82d941b1ef21 | 69 | char ADC_data_rx[3]; |
jrodenburg | 20:cdeed4dad690 | 70 | |
jrodenburg | 0:a28a1035c31b | 71 | //Read data from selected channel --> 24bits --> 23bit=SIGN 22bit=MSB 21-7bits=DATA 5-0bits=JUNK |
jrodenburg | 2:bd118a724f03 | 72 | i2c.read((addrI2C<<1)|(I2C_READ), ADC_data_rx, 3); |
jrodenburg | 2:bd118a724f03 | 73 | //Stitch together the bytes into a 24bit value |
jrodenburg | 0:a28a1035c31b | 74 | unsigned long data = (ADC_data_rx[0] << 16) | (ADC_data_rx[1] << 8)| ADC_data_rx[2]; |
jrodenburg | 0:a28a1035c31b | 75 | //Delete SIGN bit and MSB bit and remove 6 JUNK bits |
jrodenburg | 20:cdeed4dad690 | 76 | unsigned long ADC_Result = (data&0x3fffff)>>6; |
jrodenburg | 20:cdeed4dad690 | 77 | |
jrodenburg | 0:a28a1035c31b | 78 | return float(float(ADC_Result)); |
jrodenburg | 0:a28a1035c31b | 79 | |
jrodenburg | 0:a28a1035c31b | 80 | } |