This is a data logger program to be implemented with an instrument amplifier.
Data_Logger_V100/mbed/I2C.h@0:d75ca4e39672, 2017-04-04 (annotated)
- Committer:
- KISScientific
- Date:
- Tue Apr 04 18:01:11 2017 +0000
- Revision:
- 0:d75ca4e39672
This is a data logger program.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KISScientific | 0:d75ca4e39672 | 1 | /* mbed Microcontroller Library - I2C |
KISScientific | 0:d75ca4e39672 | 2 | * Copyright (c) 2007-2009 ARM Limited. All rights reserved. |
KISScientific | 0:d75ca4e39672 | 3 | * sford |
KISScientific | 0:d75ca4e39672 | 4 | */ |
KISScientific | 0:d75ca4e39672 | 5 | |
KISScientific | 0:d75ca4e39672 | 6 | #ifndef MBED_I2C_H |
KISScientific | 0:d75ca4e39672 | 7 | #define MBED_I2C_H |
KISScientific | 0:d75ca4e39672 | 8 | |
KISScientific | 0:d75ca4e39672 | 9 | #include "platform.h" |
KISScientific | 0:d75ca4e39672 | 10 | #include "PinNames.h" |
KISScientific | 0:d75ca4e39672 | 11 | #include "PeripheralNames.h" |
KISScientific | 0:d75ca4e39672 | 12 | #include "Base.h" |
KISScientific | 0:d75ca4e39672 | 13 | |
KISScientific | 0:d75ca4e39672 | 14 | namespace mbed { |
KISScientific | 0:d75ca4e39672 | 15 | |
KISScientific | 0:d75ca4e39672 | 16 | /* Class: I2C |
KISScientific | 0:d75ca4e39672 | 17 | * An I2C Master, used for communicating with I2C slave devices |
KISScientific | 0:d75ca4e39672 | 18 | * |
KISScientific | 0:d75ca4e39672 | 19 | * Example: |
KISScientific | 0:d75ca4e39672 | 20 | * > // Read from I2C slave at address 0x62 |
KISScientific | 0:d75ca4e39672 | 21 | * > |
KISScientific | 0:d75ca4e39672 | 22 | * > #include "mbed.h" |
KISScientific | 0:d75ca4e39672 | 23 | * > |
KISScientific | 0:d75ca4e39672 | 24 | * > I2C i2c(p28, p27); |
KISScientific | 0:d75ca4e39672 | 25 | * > |
KISScientific | 0:d75ca4e39672 | 26 | * > int main() { |
KISScientific | 0:d75ca4e39672 | 27 | * > int address = 0x62; |
KISScientific | 0:d75ca4e39672 | 28 | * > char data[2]; |
KISScientific | 0:d75ca4e39672 | 29 | * > i2c.read(address, data, 2); |
KISScientific | 0:d75ca4e39672 | 30 | * > } |
KISScientific | 0:d75ca4e39672 | 31 | */ |
KISScientific | 0:d75ca4e39672 | 32 | class I2C : public Base { |
KISScientific | 0:d75ca4e39672 | 33 | |
KISScientific | 0:d75ca4e39672 | 34 | public: |
KISScientific | 0:d75ca4e39672 | 35 | |
KISScientific | 0:d75ca4e39672 | 36 | enum RxStatus { |
KISScientific | 0:d75ca4e39672 | 37 | NoData |
KISScientific | 0:d75ca4e39672 | 38 | , MasterGeneralCall |
KISScientific | 0:d75ca4e39672 | 39 | , MasterWrite |
KISScientific | 0:d75ca4e39672 | 40 | , MasterRead |
KISScientific | 0:d75ca4e39672 | 41 | }; |
KISScientific | 0:d75ca4e39672 | 42 | |
KISScientific | 0:d75ca4e39672 | 43 | enum Acknowledge { |
KISScientific | 0:d75ca4e39672 | 44 | NoACK = 0 |
KISScientific | 0:d75ca4e39672 | 45 | , ACK = 1 |
KISScientific | 0:d75ca4e39672 | 46 | }; |
KISScientific | 0:d75ca4e39672 | 47 | |
KISScientific | 0:d75ca4e39672 | 48 | /* Constructor: I2C |
KISScientific | 0:d75ca4e39672 | 49 | * Create an I2C Master interface, connected to the specified pins |
KISScientific | 0:d75ca4e39672 | 50 | * |
KISScientific | 0:d75ca4e39672 | 51 | * Variables: |
KISScientific | 0:d75ca4e39672 | 52 | * sda - I2C data line pin |
KISScientific | 0:d75ca4e39672 | 53 | * scl - I2C clock line pin |
KISScientific | 0:d75ca4e39672 | 54 | */ |
KISScientific | 0:d75ca4e39672 | 55 | I2C(PinName sda, PinName scl, const char *name = NULL); |
KISScientific | 0:d75ca4e39672 | 56 | |
KISScientific | 0:d75ca4e39672 | 57 | /* Function: frequency |
KISScientific | 0:d75ca4e39672 | 58 | * Set the frequency of the I2C interface |
KISScientific | 0:d75ca4e39672 | 59 | * |
KISScientific | 0:d75ca4e39672 | 60 | * Variables: |
KISScientific | 0:d75ca4e39672 | 61 | * hz - The bus frequency in hertz |
KISScientific | 0:d75ca4e39672 | 62 | */ |
KISScientific | 0:d75ca4e39672 | 63 | void frequency(int hz); |
KISScientific | 0:d75ca4e39672 | 64 | |
KISScientific | 0:d75ca4e39672 | 65 | /* Function: read |
KISScientific | 0:d75ca4e39672 | 66 | * Read from an I2C slave |
KISScientific | 0:d75ca4e39672 | 67 | * |
KISScientific | 0:d75ca4e39672 | 68 | * Performs a complete read transaction. The bottom bit of |
KISScientific | 0:d75ca4e39672 | 69 | * the address is forced to 1 to indicate a read. |
KISScientific | 0:d75ca4e39672 | 70 | * |
KISScientific | 0:d75ca4e39672 | 71 | * Variables: |
KISScientific | 0:d75ca4e39672 | 72 | * address - 8-bit I2C slave address [ addr | 1 ] |
KISScientific | 0:d75ca4e39672 | 73 | * data - Pointer to the byte-array to read data in to |
KISScientific | 0:d75ca4e39672 | 74 | * length - Number of bytes to read |
KISScientific | 0:d75ca4e39672 | 75 | * repeated - Repeated start, true - don't send stop at end |
KISScientific | 0:d75ca4e39672 | 76 | * returns - 0 on success (ack), or non-0 on failure (nack) |
KISScientific | 0:d75ca4e39672 | 77 | */ |
KISScientific | 0:d75ca4e39672 | 78 | int read(int address, char *data, int length, bool repeated = false); |
KISScientific | 0:d75ca4e39672 | 79 | |
KISScientific | 0:d75ca4e39672 | 80 | /* Function: read |
KISScientific | 0:d75ca4e39672 | 81 | * Read a single byte from the I2C bus |
KISScientific | 0:d75ca4e39672 | 82 | * |
KISScientific | 0:d75ca4e39672 | 83 | * Variables: |
KISScientific | 0:d75ca4e39672 | 84 | * ack - indicates if the byte is to be acknowledged (1 = acknowledge) |
KISScientific | 0:d75ca4e39672 | 85 | * returns - the byte read |
KISScientific | 0:d75ca4e39672 | 86 | */ |
KISScientific | 0:d75ca4e39672 | 87 | int read(int ack); |
KISScientific | 0:d75ca4e39672 | 88 | |
KISScientific | 0:d75ca4e39672 | 89 | /* Function: write |
KISScientific | 0:d75ca4e39672 | 90 | * Write to an I2C slave |
KISScientific | 0:d75ca4e39672 | 91 | * |
KISScientific | 0:d75ca4e39672 | 92 | * Performs a complete write transaction. The bottom bit of |
KISScientific | 0:d75ca4e39672 | 93 | * the address is forced to 0 to indicate a write. |
KISScientific | 0:d75ca4e39672 | 94 | * |
KISScientific | 0:d75ca4e39672 | 95 | * Variables: |
KISScientific | 0:d75ca4e39672 | 96 | * address - 8-bit I2C slave address [ addr | 0 ] |
KISScientific | 0:d75ca4e39672 | 97 | * data - Pointer to the byte-array data to send |
KISScientific | 0:d75ca4e39672 | 98 | * length - Number of bytes to send |
KISScientific | 0:d75ca4e39672 | 99 | * repeated - Repeated start, true - do not send stop at end |
KISScientific | 0:d75ca4e39672 | 100 | * returns - 0 on success (ack), or non-0 on failure (nack) |
KISScientific | 0:d75ca4e39672 | 101 | */ |
KISScientific | 0:d75ca4e39672 | 102 | int write(int address, const char *data, int length, bool repeated = false); |
KISScientific | 0:d75ca4e39672 | 103 | |
KISScientific | 0:d75ca4e39672 | 104 | /* Function: write |
KISScientific | 0:d75ca4e39672 | 105 | * Write single byte out on the I2C bus |
KISScientific | 0:d75ca4e39672 | 106 | * |
KISScientific | 0:d75ca4e39672 | 107 | * Variables: |
KISScientific | 0:d75ca4e39672 | 108 | * data - data to write out on bus |
KISScientific | 0:d75ca4e39672 | 109 | * returns - a '1' if an ACK was received, a '0' otherwise |
KISScientific | 0:d75ca4e39672 | 110 | */ |
KISScientific | 0:d75ca4e39672 | 111 | int write(int data); |
KISScientific | 0:d75ca4e39672 | 112 | |
KISScientific | 0:d75ca4e39672 | 113 | /* Function: start |
KISScientific | 0:d75ca4e39672 | 114 | * Creates a start condition on the I2C bus |
KISScientific | 0:d75ca4e39672 | 115 | */ |
KISScientific | 0:d75ca4e39672 | 116 | |
KISScientific | 0:d75ca4e39672 | 117 | void start(void); |
KISScientific | 0:d75ca4e39672 | 118 | |
KISScientific | 0:d75ca4e39672 | 119 | /* Function: stop |
KISScientific | 0:d75ca4e39672 | 120 | * Creates a stop condition on the I2C bus |
KISScientific | 0:d75ca4e39672 | 121 | */ |
KISScientific | 0:d75ca4e39672 | 122 | void stop(void); |
KISScientific | 0:d75ca4e39672 | 123 | |
KISScientific | 0:d75ca4e39672 | 124 | protected: |
KISScientific | 0:d75ca4e39672 | 125 | |
KISScientific | 0:d75ca4e39672 | 126 | void aquire(); |
KISScientific | 0:d75ca4e39672 | 127 | |
KISScientific | 0:d75ca4e39672 | 128 | I2CName _i2c; |
KISScientific | 0:d75ca4e39672 | 129 | static I2C *_owner; |
KISScientific | 0:d75ca4e39672 | 130 | int _hz; |
KISScientific | 0:d75ca4e39672 | 131 | |
KISScientific | 0:d75ca4e39672 | 132 | }; |
KISScientific | 0:d75ca4e39672 | 133 | |
KISScientific | 0:d75ca4e39672 | 134 | } // namespace mbed |
KISScientific | 0:d75ca4e39672 | 135 | |
KISScientific | 0:d75ca4e39672 | 136 | #endif |