Library to work with the LDC1000 from Texas Instruments
LDC1000
This library was written to interface to Texas Instruments' LDC1000 in order to perform inductance measurement. This libary needs a SPI peripheral on your mbed device to talk to the LDC1000.
Clock
The LDC1000 needs a high speed clock for its internal frequency counter. In order to provide this clock, the FastPWM library is used. This may change the behaviour of other PWM channels, please be aware of that, and read the FastPWM documentation to understand the implications.
Unsupported
Not supported (yet):
- Setting the RpMAX and RpMIN values
- Setting the interrupt pin functionality
LDC1000.cpp@6:5b6ad98d6436, 2015-05-26 (annotated)
- Committer:
- hamid567
- Date:
- Tue May 26 14:43:33 2015 +0000
- Revision:
- 6:5b6ad98d6436
- Parent:
- 5:98d4fd07734a
- Child:
- 7:7f1522ad3df5
Working readout. Skipped all nice functions, just plain registers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:90873b4e8330 | 1 | /** |
vsluiter | 0:90873b4e8330 | 2 | * @file LDC1000.h |
vsluiter | 0:90873b4e8330 | 3 | * @brief this C++ file wcontains all required |
vsluiter | 0:90873b4e8330 | 4 | * functions to interface with Texas |
vsluiter | 0:90873b4e8330 | 5 | * Instruments' LDC1000. |
vsluiter | 0:90873b4e8330 | 6 | * |
vsluiter | 0:90873b4e8330 | 7 | * @author Victor Sluiter |
vsluiter | 0:90873b4e8330 | 8 | * |
vsluiter | 0:90873b4e8330 | 9 | * @date 2015-04-01 |
vsluiter | 0:90873b4e8330 | 10 | */ |
vsluiter | 0:90873b4e8330 | 11 | |
vsluiter | 0:90873b4e8330 | 12 | #include "LDC1000.h" |
vsluiter | 0:90873b4e8330 | 13 | |
hamid567 | 4:62ebb87678f8 | 14 | LDC1000::LDC1000(PinName mosi, PinName miso, PinName sck, PinName cs, float capacitor, float f_external, PinName clock_out) : _spiport(mosi,miso,sck,NC), _cs_pin(cs), _clock(clock_out,1) |
vsluiter | 0:90873b4e8330 | 15 | { |
vsluiter | 0:90873b4e8330 | 16 | cap = capacitor; |
vsluiter | 0:90873b4e8330 | 17 | _spiport.format(8,3); |
vsluiter | 0:90873b4e8330 | 18 | _spiport.frequency(1E6); |
vsluiter | 0:90873b4e8330 | 19 | _cs_pin.write(1); |
hamid567 | 1:a88df80e7664 | 20 | wait_us(100); |
hamid567 | 6:5b6ad98d6436 | 21 | //mode(LDC_MODE_STANDBY); |
hamid567 | 5:98d4fd07734a | 22 | setFrequency(f_external); |
hamid567 | 5:98d4fd07734a | 23 | wait(0.1); |
hamid567 | 6:5b6ad98d6436 | 24 | //setOutputPower(LDC_AMPLITUDE_4V); |
hamid567 | 1:a88df80e7664 | 25 | wait_us(10); |
hamid567 | 6:5b6ad98d6436 | 26 | //setResponseTime(LDC_RESPONSE_384); |
hamid567 | 6:5b6ad98d6436 | 27 | //setWatchdog(5000); |
hamid567 | 5:98d4fd07734a | 28 | |
hamid567 | 5:98d4fd07734a | 29 | // Write comando's like in EVM start log: |
hamid567 | 6:5b6ad98d6436 | 30 | writeSPIregister(0x0B,0x00); |
hamid567 | 5:98d4fd07734a | 31 | writeSPIregister(0x01,0x0E); |
hamid567 | 5:98d4fd07734a | 32 | writeSPIregister(0x02,0x3C); |
hamid567 | 5:98d4fd07734a | 33 | writeSPIregister(0x03,0x15); |
hamid567 | 5:98d4fd07734a | 34 | writeSPIregister(0x04,0x17); |
hamid567 | 5:98d4fd07734a | 35 | writeSPIregister(0x05,0x00); |
hamid567 | 5:98d4fd07734a | 36 | writeSPIregister(0x06,0x50); |
hamid567 | 5:98d4fd07734a | 37 | writeSPIregister(0x07,0x14); |
hamid567 | 5:98d4fd07734a | 38 | writeSPIregister(0x08,0xC0); |
hamid567 | 5:98d4fd07734a | 39 | writeSPIregister(0x09,0x12); |
hamid567 | 5:98d4fd07734a | 40 | writeSPIregister(0x0A,0x04); |
hamid567 | 5:98d4fd07734a | 41 | // writeSPIregister(0x0C,0x01); |
hamid567 | 6:5b6ad98d6436 | 42 | writeSPIregister(0x0B,0x01); |
hamid567 | 5:98d4fd07734a | 43 | |
hamid567 | 5:98d4fd07734a | 44 | mode(LDC_MODE_ACTIVE); |
vsluiter | 0:90873b4e8330 | 45 | } |
vsluiter | 0:90873b4e8330 | 46 | |
vsluiter | 0:90873b4e8330 | 47 | void LDC1000::setOutputPower(LDC_AMPLITUDE amplitude) |
vsluiter | 0:90873b4e8330 | 48 | { |
vsluiter | 0:90873b4e8330 | 49 | uint8_t buffer; |
vsluiter | 0:90873b4e8330 | 50 | _amplitude = amplitude; |
vsluiter | 0:90873b4e8330 | 51 | readSPI(&buffer, 0x04); |
vsluiter | 0:90873b4e8330 | 52 | buffer &= 0xE7; //clear amplitude bits |
vsluiter | 0:90873b4e8330 | 53 | buffer |= (amplitude<<3) & 0xE7; |
vsluiter | 0:90873b4e8330 | 54 | writeSPI(&buffer,0x04); |
vsluiter | 0:90873b4e8330 | 55 | |
vsluiter | 0:90873b4e8330 | 56 | } |
vsluiter | 0:90873b4e8330 | 57 | |
hamid567 | 2:44b76f6f19d5 | 58 | void LDC1000::setWatchdog(float frequency) |
hamid567 | 2:44b76f6f19d5 | 59 | { |
hamid567 | 2:44b76f6f19d5 | 60 | uint8_t buffer; |
hamid567 | 2:44b76f6f19d5 | 61 | buffer = 68.94*log(frequency/2500); |
hamid567 | 2:44b76f6f19d5 | 62 | writeSPI(&buffer,0x03); |
hamid567 | 1:a88df80e7664 | 63 | |
hamid567 | 2:44b76f6f19d5 | 64 | } |
hamid567 | 1:a88df80e7664 | 65 | |
vsluiter | 0:90873b4e8330 | 66 | void LDC1000::setResponseTime(LDC_RESPONSE responsetime) |
vsluiter | 0:90873b4e8330 | 67 | { |
vsluiter | 0:90873b4e8330 | 68 | uint8_t buffer; |
vsluiter | 0:90873b4e8330 | 69 | _responsetime = responsetime; |
vsluiter | 0:90873b4e8330 | 70 | readSPI(&buffer, 0x04); |
vsluiter | 0:90873b4e8330 | 71 | buffer &= 0xF8; //clear responsetime bits |
hamid567 | 5:98d4fd07734a | 72 | buffer |= responsetime & 0x07; |
vsluiter | 0:90873b4e8330 | 73 | writeSPI(&buffer,0x04); |
vsluiter | 0:90873b4e8330 | 74 | } |
vsluiter | 0:90873b4e8330 | 75 | |
vsluiter | 0:90873b4e8330 | 76 | void LDC1000::setFrequency(float frequency) |
vsluiter | 0:90873b4e8330 | 77 | { |
vsluiter | 0:90873b4e8330 | 78 | _frequency = frequency; |
vsluiter | 0:90873b4e8330 | 79 | _clock.period(1.0/frequency); |
hamid567 | 4:62ebb87678f8 | 80 | _clock.pulsewidth(0.5/frequency); |
vsluiter | 0:90873b4e8330 | 81 | } |
vsluiter | 0:90873b4e8330 | 82 | |
vsluiter | 0:90873b4e8330 | 83 | float LDC1000::getInductance() |
vsluiter | 0:90873b4e8330 | 84 | { |
vsluiter | 0:90873b4e8330 | 85 | uint16_t resp[] = {192, 384, 768, 1536, 3072, 6144}; |
vsluiter | 0:90873b4e8330 | 86 | _raw_l = readRawCounts(); |
vsluiter | 0:90873b4e8330 | 87 | _fsensor = (_frequency/(_raw_l*3.0))*resp[(uint8_t)(_responsetime)]; |
hamid567 | 2:44b76f6f19d5 | 88 | return 1./(cap*pow(2*PI*_fsensor,2)); |
vsluiter | 0:90873b4e8330 | 89 | }; |
vsluiter | 0:90873b4e8330 | 90 | |
vsluiter | 0:90873b4e8330 | 91 | uint32_t LDC1000::readRawCounts(void) |
vsluiter | 0:90873b4e8330 | 92 | { |
vsluiter | 0:90873b4e8330 | 93 | union |
vsluiter | 0:90873b4e8330 | 94 | { |
vsluiter | 0:90873b4e8330 | 95 | uint8_t buf[4]; |
vsluiter | 0:90873b4e8330 | 96 | uint32_t value; |
vsluiter | 0:90873b4e8330 | 97 | } val; |
vsluiter | 0:90873b4e8330 | 98 | val.value = 0; |
hamid567 | 6:5b6ad98d6436 | 99 | uint8_t bla[5]; |
hamid567 | 6:5b6ad98d6436 | 100 | readSPI(bla,0x21,5); |
vsluiter | 0:90873b4e8330 | 101 | return val.value; |
vsluiter | 0:90873b4e8330 | 102 | } |
vsluiter | 0:90873b4e8330 | 103 | |
hamid567 | 5:98d4fd07734a | 104 | |
hamid567 | 5:98d4fd07734a | 105 | |
vsluiter | 0:90873b4e8330 | 106 | void LDC1000::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
vsluiter | 0:90873b4e8330 | 107 | { |
vsluiter | 0:90873b4e8330 | 108 | _cs_pin.write(0); |
vsluiter | 0:90873b4e8330 | 109 | _spiport.write(address | 0x80); //read flag |
vsluiter | 0:90873b4e8330 | 110 | for(int i=0; i < num_bytes ; i++) |
vsluiter | 0:90873b4e8330 | 111 | { |
hamid567 | 1:a88df80e7664 | 112 | data[i] = _spiport.write(0xFF); |
vsluiter | 0:90873b4e8330 | 113 | } |
vsluiter | 0:90873b4e8330 | 114 | _cs_pin.write(1); |
vsluiter | 0:90873b4e8330 | 115 | } |
vsluiter | 0:90873b4e8330 | 116 | |
vsluiter | 0:90873b4e8330 | 117 | void LDC1000::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
vsluiter | 0:90873b4e8330 | 118 | { |
vsluiter | 0:90873b4e8330 | 119 | _cs_pin.write(0); |
vsluiter | 0:90873b4e8330 | 120 | _spiport.write(address); |
vsluiter | 0:90873b4e8330 | 121 | for(int i=0; i < num_bytes ; i++) |
vsluiter | 0:90873b4e8330 | 122 | { |
hamid567 | 1:a88df80e7664 | 123 | _spiport.write(data[i]); |
vsluiter | 0:90873b4e8330 | 124 | } |
vsluiter | 0:90873b4e8330 | 125 | _cs_pin.write(1); |
vsluiter | 0:90873b4e8330 | 126 | } |