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.
Fork of ltc2991_test by
main.cpp@2:c9e727dcd00e, 2016-12-06 (annotated)
- Committer:
- lrdawg99
- Date:
- Tue Dec 06 18:34:46 2016 +0000
- Revision:
- 2:c9e727dcd00e
- Parent:
- 1:4e4194db7cd6
- Child:
- 3:34e56211d5b6
made object oriented
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| lrdawg99 | 0:1473318f27b6 | 1 | #include "mbed.h" |
| lrdawg99 | 0:1473318f27b6 | 2 | #include "LTC2991.h" |
| lrdawg99 | 0:1473318f27b6 | 3 | #include <stdarg.h> |
| lrdawg99 | 0:1473318f27b6 | 4 | |
| lrdawg99 | 0:1473318f27b6 | 5 | int8_t ack; // 0 == ack, 1 == no ack |
| lrdawg99 | 0:1473318f27b6 | 6 | const uint16_t LTC2991_TIMEOUT=1000; //!< Configures the maximum timeout allowed for an LTC2991 read. |
| lrdawg99 | 0:1473318f27b6 | 7 | Serial pc(USBTX, USBRX, 9600); |
| lrdawg99 | 0:1473318f27b6 | 8 | |
| lrdawg99 | 2:c9e727dcd00e | 9 | LTC2991 *ltc0 = new LTC2991(I2C_SDA, I2C_SCL); |
| lrdawg99 | 2:c9e727dcd00e | 10 | LTC2991 *ltc1 = new LTC2991(PB_3, PB_10); |
| lrdawg99 | 2:c9e727dcd00e | 11 | LTC2991 *ltc2 = new LTC2991(PB_4, PA_8); |
| lrdawg99 | 2:c9e727dcd00e | 12 | |
| lrdawg99 | 1:4e4194db7cd6 | 13 | |
| lrdawg99 | 1:4e4194db7cd6 | 14 | //void setup(); |
| lrdawg99 | 0:1473318f27b6 | 15 | int main(); |
| lrdawg99 | 0:1473318f27b6 | 16 | void readSingle(); |
| lrdawg99 | 0:1473318f27b6 | 17 | void readDiff(); |
| lrdawg99 | 0:1473318f27b6 | 18 | void setup() { |
| lrdawg99 | 2:c9e727dcd00e | 19 | //init lib |
| lrdawg99 | 2:c9e727dcd00e | 20 | |
| lrdawg99 | 2:c9e727dcd00e | 21 | |
| lrdawg99 | 1:4e4194db7cd6 | 22 | ack = 0; |
| lrdawg99 | 0:1473318f27b6 | 23 | while (true) |
| lrdawg99 | 0:1473318f27b6 | 24 | { |
| lrdawg99 | 0:1473318f27b6 | 25 | pc.printf("boot\n"); |
| lrdawg99 | 2:c9e727dcd00e | 26 | ack |= ltc0->LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CHANNEL_ENABLE_REG, LTC2991_ENABLE_ALL_CHANNELS); //! Enables all channels |
| lrdawg99 | 2:c9e727dcd00e | 27 | ack |= ltc0->LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, 0x00); //! Sets registers to default starting values. |
| lrdawg99 | 2:c9e727dcd00e | 28 | ack |= ltc0->LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V5678_REG, 0x00); |
| lrdawg99 | 2:c9e727dcd00e | 29 | ack |= ltc0->LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_PWM_Tinternal_REG, LTC2991_REPEAT_MODE); //! Configures LTC2991 for Repeated Acquisition mode |
| lrdawg99 | 1:4e4194db7cd6 | 30 | |
| lrdawg99 | 0:1473318f27b6 | 31 | if (ack != 0) { |
| lrdawg99 | 0:1473318f27b6 | 32 | pc.printf("Error: No Acknowledge. Check I2C Address.\n"); |
| lrdawg99 | 0:1473318f27b6 | 33 | wait_ms(500); |
| lrdawg99 | 0:1473318f27b6 | 34 | } else { |
| lrdawg99 | 0:1473318f27b6 | 35 | break; |
| lrdawg99 | 0:1473318f27b6 | 36 | } |
| lrdawg99 | 0:1473318f27b6 | 37 | } |
| lrdawg99 | 0:1473318f27b6 | 38 | } |
| lrdawg99 | 0:1473318f27b6 | 39 | |
| lrdawg99 | 0:1473318f27b6 | 40 | int main() { |
| lrdawg99 | 0:1473318f27b6 | 41 | setup(); |
| lrdawg99 | 0:1473318f27b6 | 42 | while(1) { |
| lrdawg99 | 0:1473318f27b6 | 43 | readDiff(); |
| lrdawg99 | 1:4e4194db7cd6 | 44 | readSingle(); |
| lrdawg99 | 0:1473318f27b6 | 45 | wait_ms(750); |
| lrdawg99 | 0:1473318f27b6 | 46 | } |
| lrdawg99 | 0:1473318f27b6 | 47 | } |
| lrdawg99 | 0:1473318f27b6 | 48 | |
| lrdawg99 | 0:1473318f27b6 | 49 | void readSingle() { |
| lrdawg99 | 0:1473318f27b6 | 50 | int8_t data_valid; |
| lrdawg99 | 0:1473318f27b6 | 51 | int16_t code; |
| lrdawg99 | 0:1473318f27b6 | 52 | float voltage; |
| lrdawg99 | 1:4e4194db7cd6 | 53 | ack = 0; |
| lrdawg99 | 2:c9e727dcd00e | 54 | ack |= ltc0->LTC2991_register_set_clear_bits(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, 0x00, LTC2991_V1_V2_DIFFERENTIAL_ENABLE | LTC2991_V1_V2_TEMP_ENABLE); |
| lrdawg99 | 2:c9e727dcd00e | 55 | ack |= ltc0->LTC2991_adc_read_new_data(LTC2991_I2C_ADDRESS, LTC2991_V1_MSB_REG, &code, &data_valid, LTC2991_TIMEOUT); |
| lrdawg99 | 2:c9e727dcd00e | 56 | voltage = ltc0->LTC2991_code_to_single_ended_voltage(code, LTC2991_SINGLE_ENDED_lsb); |
| lrdawg99 | 1:4e4194db7cd6 | 57 | if (ack != 0) { |
| lrdawg99 | 1:4e4194db7cd6 | 58 | pc.printf("Error: No Acknowledge.\n"); |
| lrdawg99 | 1:4e4194db7cd6 | 59 | } else { |
| lrdawg99 | 1:4e4194db7cd6 | 60 | pc.printf("V1: %4.2f V\n", voltage); |
| lrdawg99 | 1:4e4194db7cd6 | 61 | } |
| lrdawg99 | 0:1473318f27b6 | 62 | } |
| lrdawg99 | 1:4e4194db7cd6 | 63 | |
| lrdawg99 | 0:1473318f27b6 | 64 | void readDiff() { |
| lrdawg99 | 0:1473318f27b6 | 65 | int8_t data_valid; |
| lrdawg99 | 0:1473318f27b6 | 66 | int16_t code; |
| lrdawg99 | 0:1473318f27b6 | 67 | float voltage; |
| lrdawg99 | 1:4e4194db7cd6 | 68 | ack = 0; |
| lrdawg99 | 2:c9e727dcd00e | 69 | ack |= ltc0->LTC2991_register_set_clear_bits(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, LTC2991_V1_V2_DIFFERENTIAL_ENABLE, LTC2991_V1_V2_TEMP_ENABLE); |
| lrdawg99 | 2:c9e727dcd00e | 70 | ack |= ltc0->LTC2991_adc_read_new_data(LTC2991_I2C_ADDRESS, LTC2991_V2_MSB_REG, &code, &data_valid, LTC2991_TIMEOUT); |
| lrdawg99 | 2:c9e727dcd00e | 71 | voltage = ltc0->LTC2991_code_to_differential_voltage(code, LTC2991_DIFFERENTIAL_lsb); |
| lrdawg99 | 1:4e4194db7cd6 | 72 | if (ack != 0) { |
| lrdawg99 | 1:4e4194db7cd6 | 73 | pc.printf("Error: No Acknowledge.\n"); |
| lrdawg99 | 1:4e4194db7cd6 | 74 | } else { |
| lrdawg99 | 1:4e4194db7cd6 | 75 | pc.printf("V1-V2: %4.2f V\n", voltage, ack); |
| lrdawg99 | 1:4e4194db7cd6 | 76 | } |
| lrdawg99 | 0:1473318f27b6 | 77 | } |
