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