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@0:1473318f27b6, 2016-11-16 (annotated)
- Committer:
- lrdawg99
- Date:
- Wed Nov 16 15:54:08 2016 +0000
- Revision:
- 0:1473318f27b6
- Child:
- 1:4e4194db7cd6
Ready to integrate I2C into LT_I2C
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 <Wire.h> |
| lrdawg99 | 0:1473318f27b6 | 4 | #include <stdarg.h> |
| lrdawg99 | 0:1473318f27b6 | 5 | |
| lrdawg99 | 0:1473318f27b6 | 6 | int8_t ack; // 0 == ack, 1 == no ack |
| lrdawg99 | 0:1473318f27b6 | 7 | const uint16_t LTC2991_TIMEOUT=1000; //!< Configures the maximum timeout allowed for an LTC2991 read. |
| lrdawg99 | 0:1473318f27b6 | 8 | Serial pc(USBTX, USBRX, 9600); |
| lrdawg99 | 0:1473318f27b6 | 9 | |
| lrdawg99 | 0:1473318f27b6 | 10 | void setup(); |
| lrdawg99 | 0:1473318f27b6 | 11 | int main(); |
| lrdawg99 | 0:1473318f27b6 | 12 | void readSingle(); |
| lrdawg99 | 0:1473318f27b6 | 13 | void readDiff(); |
| lrdawg99 | 0:1473318f27b6 | 14 | |
| lrdawg99 | 0:1473318f27b6 | 15 | |
| lrdawg99 | 0:1473318f27b6 | 16 | void setup() { |
| lrdawg99 | 0:1473318f27b6 | 17 | //Wire.begin(); |
| lrdawg99 | 0:1473318f27b6 | 18 | |
| lrdawg99 | 0:1473318f27b6 | 19 | while (true) |
| lrdawg99 | 0:1473318f27b6 | 20 | { |
| lrdawg99 | 0:1473318f27b6 | 21 | pc.printf("boot\n"); |
| lrdawg99 | 0:1473318f27b6 | 22 | ack |= LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CHANNEL_ENABLE_REG, LTC2991_ENABLE_ALL_CHANNELS); //! Enables all channels |
| lrdawg99 | 0:1473318f27b6 | 23 | ack |= LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, 0x00); //! Sets registers to default starting values. |
| lrdawg99 | 0:1473318f27b6 | 24 | ack |= LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V5678_REG, 0x00); |
| lrdawg99 | 0:1473318f27b6 | 25 | ack |= LTC2991_register_write(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_PWM_Tinternal_REG, LTC2991_REPEAT_MODE); //! Configures LTC2991 for Repeated Acquisition mode |
| lrdawg99 | 0:1473318f27b6 | 26 | if (ack != 0) { |
| lrdawg99 | 0:1473318f27b6 | 27 | pc.printf("Error: No Acknowledge. Check I2C Address.\n"); |
| lrdawg99 | 0:1473318f27b6 | 28 | wait_ms(500); |
| lrdawg99 | 0:1473318f27b6 | 29 | } else { |
| lrdawg99 | 0:1473318f27b6 | 30 | break; |
| lrdawg99 | 0:1473318f27b6 | 31 | } |
| lrdawg99 | 0:1473318f27b6 | 32 | } |
| lrdawg99 | 0:1473318f27b6 | 33 | } |
| lrdawg99 | 0:1473318f27b6 | 34 | |
| lrdawg99 | 0:1473318f27b6 | 35 | int main() { |
| lrdawg99 | 0:1473318f27b6 | 36 | setup(); |
| lrdawg99 | 0:1473318f27b6 | 37 | while(1) { |
| lrdawg99 | 0:1473318f27b6 | 38 | readDiff(); |
| lrdawg99 | 0:1473318f27b6 | 39 | wait_ms(750); |
| lrdawg99 | 0:1473318f27b6 | 40 | } |
| lrdawg99 | 0:1473318f27b6 | 41 | } |
| lrdawg99 | 0:1473318f27b6 | 42 | |
| lrdawg99 | 0:1473318f27b6 | 43 | void readSingle() { |
| lrdawg99 | 0:1473318f27b6 | 44 | int8_t data_valid; |
| lrdawg99 | 0:1473318f27b6 | 45 | int16_t code; |
| lrdawg99 | 0:1473318f27b6 | 46 | float voltage; |
| lrdawg99 | 0:1473318f27b6 | 47 | |
| lrdawg99 | 0:1473318f27b6 | 48 | ack |= LTC2991_register_set_clear_bits(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, 0x00, LTC2991_V1_V2_DIFFERENTIAL_ENABLE | LTC2991_V1_V2_TEMP_ENABLE); |
| lrdawg99 | 0:1473318f27b6 | 49 | ack |= LTC2991_adc_read_new_data(LTC2991_I2C_ADDRESS, LTC2991_V1_MSB_REG, &code, &data_valid, LTC2991_TIMEOUT); |
| lrdawg99 | 0:1473318f27b6 | 50 | voltage = LTC2991_code_to_single_ended_voltage(code, LTC2991_SINGLE_ENDED_lsb); |
| lrdawg99 | 0:1473318f27b6 | 51 | pc.printf("V1: %4.2f V", voltage); |
| lrdawg99 | 0:1473318f27b6 | 52 | } |
| lrdawg99 | 0:1473318f27b6 | 53 | void readDiff() { |
| lrdawg99 | 0:1473318f27b6 | 54 | int8_t data_valid; |
| lrdawg99 | 0:1473318f27b6 | 55 | int16_t code; |
| lrdawg99 | 0:1473318f27b6 | 56 | float voltage; |
| lrdawg99 | 0:1473318f27b6 | 57 | |
| lrdawg99 | 0:1473318f27b6 | 58 | ack |= LTC2991_register_set_clear_bits(LTC2991_I2C_ADDRESS, LTC2991_CONTROL_V1234_REG, LTC2991_V1_V2_DIFFERENTIAL_ENABLE, LTC2991_V1_V2_TEMP_ENABLE); |
| lrdawg99 | 0:1473318f27b6 | 59 | ack |= LTC2991_adc_read_new_data(LTC2991_I2C_ADDRESS, LTC2991_V2_MSB_REG, &code, &data_valid, LTC2991_TIMEOUT); |
| lrdawg99 | 0:1473318f27b6 | 60 | voltage = LTC2991_code_to_differential_voltage(code, LTC2991_DIFFERENTIAL_lsb); |
| lrdawg99 | 0:1473318f27b6 | 61 | pc.printf("V1-V2: %4.2f V", voltage); |
| lrdawg99 | 0:1473318f27b6 | 62 | } |
