Library to communicate with LDC1614
Dependents: Inductive_Sensor_3
Fork of LDC1101 by
LDC1101.h@16:07d0e43c2d12, 2015-12-10 (annotated)
- Committer:
- bobgiesberts
- Date:
- Thu Dec 10 15:12:16 2015 +0000
- Revision:
- 16:07d0e43c2d12
- Child:
- 17:a5cf2b4bec13
Started new library for LDC1101, forked from LDC1000
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobgiesberts | 16:07d0e43c2d12 | 1 | #ifndef _LDC1101_H_ |
bobgiesberts | 16:07d0e43c2d12 | 2 | #define _LDC1101_H_ |
bobgiesberts | 16:07d0e43c2d12 | 3 | |
bobgiesberts | 16:07d0e43c2d12 | 4 | #include "FastPWM.h" |
bobgiesberts | 16:07d0e43c2d12 | 5 | /** |
bobgiesberts | 16:07d0e43c2d12 | 6 | * @file LDC1101.h |
bobgiesberts | 16:07d0e43c2d12 | 7 | * @brief this header file will contain all required |
bobgiesberts | 16:07d0e43c2d12 | 8 | * definitions for the functions to interface with Texas |
bobgiesberts | 16:07d0e43c2d12 | 9 | * Instruments' LDC1101. |
bobgiesberts | 16:07d0e43c2d12 | 10 | * |
bobgiesberts | 16:07d0e43c2d12 | 11 | * @author Victor Sluiter |
bobgiesberts | 16:07d0e43c2d12 | 12 | * |
bobgiesberts | 16:07d0e43c2d12 | 13 | * @date 2015-12-09 |
bobgiesberts | 16:07d0e43c2d12 | 14 | */ |
bobgiesberts | 16:07d0e43c2d12 | 15 | |
bobgiesberts | 16:07d0e43c2d12 | 16 | #include "mbed.h" |
bobgiesberts | 16:07d0e43c2d12 | 17 | |
bobgiesberts | 16:07d0e43c2d12 | 18 | #ifndef PI |
bobgiesberts | 16:07d0e43c2d12 | 19 | #define PI 3.14 |
bobgiesberts | 16:07d0e43c2d12 | 20 | #endif |
bobgiesberts | 16:07d0e43c2d12 | 21 | |
bobgiesberts | 16:07d0e43c2d12 | 22 | typedef enum { LDC_RESPONSE_192 = 2, \ |
bobgiesberts | 16:07d0e43c2d12 | 23 | LDC_RESPONSE_384 = 3, \ |
bobgiesberts | 16:07d0e43c2d12 | 24 | LDC_RESPONSE_768 = 4, \ |
bobgiesberts | 16:07d0e43c2d12 | 25 | LDC_RESPONSE_1536= 5, \ |
bobgiesberts | 16:07d0e43c2d12 | 26 | LDC_RESPONSE_3072= 6, \ |
bobgiesberts | 16:07d0e43c2d12 | 27 | LDC_RESPONSE_6144= 7} LDC_RESPONSE; |
bobgiesberts | 16:07d0e43c2d12 | 28 | |
bobgiesberts | 16:07d0e43c2d12 | 29 | typedef enum { LDC_MODE_ACTIVE = 0, \ |
bobgiesberts | 16:07d0e43c2d12 | 30 | LDC_MODE_STANDBY = 1, \ |
bobgiesberts | 16:07d0e43c2d12 | 31 | LDC_MODE_SHUTDOWN = 2} LDC_MODE; |
bobgiesberts | 16:07d0e43c2d12 | 32 | |
bobgiesberts | 16:07d0e43c2d12 | 33 | /** |
bobgiesberts | 16:07d0e43c2d12 | 34 | * Class for the LDC1101. |
bobgiesberts | 16:07d0e43c2d12 | 35 | * @author Victor Sluiter |
bobgiesberts | 16:07d0e43c2d12 | 36 | * @date 2015-12-09 |
bobgiesberts | 16:07d0e43c2d12 | 37 | */ |
bobgiesberts | 16:07d0e43c2d12 | 38 | class LDC1101 |
bobgiesberts | 16:07d0e43c2d12 | 39 | { |
bobgiesberts | 16:07d0e43c2d12 | 40 | public: |
bobgiesberts | 16:07d0e43c2d12 | 41 | /** |
bobgiesberts | 16:07d0e43c2d12 | 42 | * @brief Create a new Class to interface to an LDC1101 |
bobgiesberts | 16:07d0e43c2d12 | 43 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 44 | LDC1101(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_external, PinName clock_out=NC); |
bobgiesberts | 16:07d0e43c2d12 | 45 | |
bobgiesberts | 16:07d0e43c2d12 | 46 | /** |
bobgiesberts | 16:07d0e43c2d12 | 47 | * @brief Set power mode. |
bobgiesberts | 16:07d0e43c2d12 | 48 | * The constructor sets the LDC1101 in Active mode. |
bobgiesberts | 16:07d0e43c2d12 | 49 | * @param mode choose from LDC_MODE_ACTIVE, LDC_MODE STANDBY or LDC_MODE_SHUTDOWN |
bobgiesberts | 16:07d0e43c2d12 | 50 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 51 | void mode(LDC_MODE mode) { writeSPI((uint8_t *)(&mode), 0x0B); }; |
bobgiesberts | 16:07d0e43c2d12 | 52 | |
bobgiesberts | 16:07d0e43c2d12 | 53 | /** |
bobgiesberts | 16:07d0e43c2d12 | 54 | * @brief initial configurations |
bobgiesberts | 16:07d0e43c2d12 | 55 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 56 | void init(void); |
bobgiesberts | 16:07d0e43c2d12 | 57 | |
bobgiesberts | 16:07d0e43c2d12 | 58 | /** |
bobgiesberts | 16:07d0e43c2d12 | 59 | * @brief get the calculated inductance value |
bobgiesberts | 16:07d0e43c2d12 | 60 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 61 | float getInductance(void); |
bobgiesberts | 16:07d0e43c2d12 | 62 | |
bobgiesberts | 16:07d0e43c2d12 | 63 | |
bobgiesberts | 16:07d0e43c2d12 | 64 | // EXTRA test get variables values to verify (to calculate the induction) |
bobgiesberts | 16:07d0e43c2d12 | 65 | float get_raw_l(void); |
bobgiesberts | 16:07d0e43c2d12 | 66 | float get_fsensor(void); |
bobgiesberts | 16:07d0e43c2d12 | 67 | float get_frequency(void); |
bobgiesberts | 16:07d0e43c2d12 | 68 | float get_responsetime(void); |
bobgiesberts | 16:07d0e43c2d12 | 69 | float get_cap(void); |
bobgiesberts | 16:07d0e43c2d12 | 70 | |
bobgiesberts | 16:07d0e43c2d12 | 71 | |
bobgiesberts | 16:07d0e43c2d12 | 72 | /** |
bobgiesberts | 16:07d0e43c2d12 | 73 | * @brief Set the value of the external capacitor |
bobgiesberts | 16:07d0e43c2d12 | 74 | * This is needed for the calculation of the inductance. |
bobgiesberts | 16:07d0e43c2d12 | 75 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 76 | void setCapacitor(float c){cap = c;}; |
bobgiesberts | 16:07d0e43c2d12 | 77 | /** |
bobgiesberts | 16:07d0e43c2d12 | 78 | * @brief set the value of the external clock |
bobgiesberts | 16:07d0e43c2d12 | 79 | * If PWMout is used to generate a clock signal, this will update the output frequency.s |
bobgiesberts | 16:07d0e43c2d12 | 80 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 81 | void setFrequency(float frequency); |
bobgiesberts | 16:07d0e43c2d12 | 82 | /** |
bobgiesberts | 16:07d0e43c2d12 | 83 | * @brief Read the raw 24-bit inductance value. |
bobgiesberts | 16:07d0e43c2d12 | 84 | * This is needed for the calculation of the inductance. |
bobgiesberts | 16:07d0e43c2d12 | 85 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 86 | uint32_t readRawL(void){_raw_l = readRawCounts(); return _raw_l;}; |
bobgiesberts | 16:07d0e43c2d12 | 87 | |
bobgiesberts | 16:07d0e43c2d12 | 88 | |
bobgiesberts | 16:07d0e43c2d12 | 89 | /** |
bobgiesberts | 16:07d0e43c2d12 | 90 | * @brief Set the Response Time parameters. |
bobgiesberts | 16:07d0e43c2d12 | 91 | * @param responsetime |
bobgiesberts | 16:07d0e43c2d12 | 92 | * Larger value increases accuracy, but slows down the output data rate. Choose one of these values: |
bobgiesberts | 16:07d0e43c2d12 | 93 | * - LDC_RESPONSE_192 |
bobgiesberts | 16:07d0e43c2d12 | 94 | * - LDC_RESPONSE_384 |
bobgiesberts | 16:07d0e43c2d12 | 95 | * - LDC_RESPONSE_768 |
bobgiesberts | 16:07d0e43c2d12 | 96 | * - LDC_RESPONSE_1536 |
bobgiesberts | 16:07d0e43c2d12 | 97 | * - LDC_RESPONSE_3072 |
bobgiesberts | 16:07d0e43c2d12 | 98 | * - LDC_RESPONSE_6144 |
bobgiesberts | 16:07d0e43c2d12 | 99 | **/ |
bobgiesberts | 16:07d0e43c2d12 | 100 | void setResponseTime(LDC_RESPONSE responsetime); |
bobgiesberts | 16:07d0e43c2d12 | 101 | |
bobgiesberts | 16:07d0e43c2d12 | 102 | private: |
bobgiesberts | 16:07d0e43c2d12 | 103 | void readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1); |
bobgiesberts | 16:07d0e43c2d12 | 104 | void writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes = 1); |
bobgiesberts | 16:07d0e43c2d12 | 105 | void writeSPIregister(uint8_t reg, uint8_t value){writeSPI(&value,reg);}; // VERKEERD OM?! |
bobgiesberts | 16:07d0e43c2d12 | 106 | |
bobgiesberts | 16:07d0e43c2d12 | 107 | uint32_t readRawCounts(void); |
bobgiesberts | 16:07d0e43c2d12 | 108 | uint32_t readINTB(void); // EXTRA UNTB Read register |
bobgiesberts | 16:07d0e43c2d12 | 109 | LDC_RESPONSE _responsetime; |
bobgiesberts | 16:07d0e43c2d12 | 110 | float _fsensor; |
bobgiesberts | 16:07d0e43c2d12 | 111 | float _inductance; |
bobgiesberts | 16:07d0e43c2d12 | 112 | float _frequency; //frequency of external clock |
bobgiesberts | 16:07d0e43c2d12 | 113 | float cap; |
bobgiesberts | 16:07d0e43c2d12 | 114 | uint32_t _raw_l; |
bobgiesberts | 16:07d0e43c2d12 | 115 | uint32_t INTB; // extra: read register INTB |
bobgiesberts | 16:07d0e43c2d12 | 116 | |
bobgiesberts | 16:07d0e43c2d12 | 117 | SPI _spiport; |
bobgiesberts | 16:07d0e43c2d12 | 118 | DigitalOut _cs_pin; |
bobgiesberts | 16:07d0e43c2d12 | 119 | |
bobgiesberts | 16:07d0e43c2d12 | 120 | FastPWM _clock; |
bobgiesberts | 16:07d0e43c2d12 | 121 | }; |
bobgiesberts | 16:07d0e43c2d12 | 122 | |
bobgiesberts | 16:07d0e43c2d12 | 123 | #endif |