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.
Dependents: Inductive_Sensor_3
Fork of LDC1101 by
LDC1000.cpp@15:8a09279a05eb, 2015-08-18 (annotated)
- Committer:
- vsluiter
- Date:
- Tue Aug 18 17:46:35 2015 +0000
- Revision:
- 15:8a09279a05eb
- Parent:
- 12:312970050c8c
lj
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 | 7:7f1522ad3df5 | 21 | mode(LDC_MODE_STANDBY); |
| hamid567 | 5:98d4fd07734a | 22 | setFrequency(f_external); |
| hamid567 | 8:b5fb9681869c | 23 | wait(0.1); |
| hamid567 | 1:a88df80e7664 | 24 | wait_us(10); |
| hamid567 | 8:b5fb9681869c | 25 | |
| hamid567 | 8:b5fb9681869c | 26 | setWatchdog(5000); |
| hamid567 | 8:b5fb9681869c | 27 | setResponseTime(LDC_RESPONSE_6144); |
| hamid567 | 8:b5fb9681869c | 28 | setOutputPower(LDC_AMPLITUDE_4V); |
| hamid567 | 11:0f53fbf73613 | 29 | |
| hamid567 | 10:4fa62f8092c4 | 30 | writeSPIregister(0x05,0x00); // clock config >> we get 0x00 if this line is disabled and the cable is reconnected |
| hamid567 | 11:0f53fbf73613 | 31 | writeSPIregister(0x0C,0x01); // Register 0x0C enables a function that can improve L measurements while disabling RP measurements |
| hamid567 | 10:4fa62f8092c4 | 32 | |
| hamid567 | 5:98d4fd07734a | 33 | mode(LDC_MODE_ACTIVE); |
| vsluiter | 0:90873b4e8330 | 34 | } |
| vsluiter | 0:90873b4e8330 | 35 | |
| vsluiter | 0:90873b4e8330 | 36 | void LDC1000::setOutputPower(LDC_AMPLITUDE amplitude) |
| vsluiter | 0:90873b4e8330 | 37 | { |
| vsluiter | 0:90873b4e8330 | 38 | uint8_t buffer; |
| vsluiter | 0:90873b4e8330 | 39 | _amplitude = amplitude; |
| vsluiter | 0:90873b4e8330 | 40 | readSPI(&buffer, 0x04); |
| vsluiter | 0:90873b4e8330 | 41 | buffer &= 0xE7; //clear amplitude bits |
| hamid567 | 8:b5fb9681869c | 42 | buffer |= (amplitude<<3) & 0x18; |
| vsluiter | 0:90873b4e8330 | 43 | writeSPI(&buffer,0x04); |
| vsluiter | 0:90873b4e8330 | 44 | } |
| vsluiter | 0:90873b4e8330 | 45 | |
| hamid567 | 2:44b76f6f19d5 | 46 | void LDC1000::setWatchdog(float frequency) |
| hamid567 | 2:44b76f6f19d5 | 47 | { |
| hamid567 | 2:44b76f6f19d5 | 48 | uint8_t buffer; |
| hamid567 | 2:44b76f6f19d5 | 49 | buffer = 68.94*log(frequency/2500); |
| hamid567 | 2:44b76f6f19d5 | 50 | writeSPI(&buffer,0x03); |
| hamid567 | 2:44b76f6f19d5 | 51 | } |
| hamid567 | 1:a88df80e7664 | 52 | |
| vsluiter | 0:90873b4e8330 | 53 | void LDC1000::setResponseTime(LDC_RESPONSE responsetime) |
| vsluiter | 0:90873b4e8330 | 54 | { |
| vsluiter | 0:90873b4e8330 | 55 | uint8_t buffer; |
| vsluiter | 0:90873b4e8330 | 56 | _responsetime = responsetime; |
| vsluiter | 0:90873b4e8330 | 57 | readSPI(&buffer, 0x04); |
| vsluiter | 0:90873b4e8330 | 58 | buffer &= 0xF8; //clear responsetime bits |
| hamid567 | 5:98d4fd07734a | 59 | buffer |= responsetime & 0x07; |
| hamid567 | 8:b5fb9681869c | 60 | //writeSPIregister(0x04,buffer); |
| vsluiter | 0:90873b4e8330 | 61 | writeSPI(&buffer,0x04); |
| vsluiter | 0:90873b4e8330 | 62 | } |
| vsluiter | 0:90873b4e8330 | 63 | |
| vsluiter | 0:90873b4e8330 | 64 | void LDC1000::setFrequency(float frequency) |
| vsluiter | 0:90873b4e8330 | 65 | { |
| vsluiter | 0:90873b4e8330 | 66 | _frequency = frequency; |
| vsluiter | 0:90873b4e8330 | 67 | _clock.period(1.0/frequency); |
| hamid567 | 4:62ebb87678f8 | 68 | _clock.pulsewidth(0.5/frequency); |
| vsluiter | 0:90873b4e8330 | 69 | } |
| vsluiter | 0:90873b4e8330 | 70 | |
| vsluiter | 0:90873b4e8330 | 71 | float LDC1000::getInductance() |
| vsluiter | 0:90873b4e8330 | 72 | { |
| hamid567 | 8:b5fb9681869c | 73 | uint16_t resp[] = {0,0,192, 384, 768, 1536, 3072, 6144}; |
| vsluiter | 0:90873b4e8330 | 74 | _raw_l = readRawCounts(); |
| vsluiter | 0:90873b4e8330 | 75 | _fsensor = (_frequency/(_raw_l*3.0))*resp[(uint8_t)(_responsetime)]; |
| hamid567 | 2:44b76f6f19d5 | 76 | return 1./(cap*pow(2*PI*_fsensor,2)); |
| vsluiter | 0:90873b4e8330 | 77 | }; |
| vsluiter | 0:90873b4e8330 | 78 | |
| hamid567 | 8:b5fb9681869c | 79 | |
| hamid567 | 8:b5fb9681869c | 80 | uint32_t LDC1000::readRawCounts(void) |
| hamid567 | 8:b5fb9681869c | 81 | { |
| hamid567 | 8:b5fb9681869c | 82 | uint8_t val[5]; |
| hamid567 | 8:b5fb9681869c | 83 | readSPI(val,0x21,5); |
| hamid567 | 12:312970050c8c | 84 | uint32_t combinedbytes = (val[4]<<16)| (val[3]<<8) | val[2]; // combine the content of the 3 bytes from registers 23, 24 and 25 |
| hamid567 | 9:6e2d808b113d | 85 | return combinedbytes; |
| hamid567 | 8:b5fb9681869c | 86 | } |
| hamid567 | 8:b5fb9681869c | 87 | |
| vsluiter | 0:90873b4e8330 | 88 | void LDC1000::readSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
| vsluiter | 0:90873b4e8330 | 89 | { |
| vsluiter | 0:90873b4e8330 | 90 | _cs_pin.write(0); |
| vsluiter | 0:90873b4e8330 | 91 | _spiport.write(address | 0x80); //read flag |
| vsluiter | 0:90873b4e8330 | 92 | for(int i=0; i < num_bytes ; i++) |
| vsluiter | 0:90873b4e8330 | 93 | { |
| hamid567 | 1:a88df80e7664 | 94 | data[i] = _spiport.write(0xFF); |
| vsluiter | 0:90873b4e8330 | 95 | } |
| vsluiter | 0:90873b4e8330 | 96 | _cs_pin.write(1); |
| vsluiter | 0:90873b4e8330 | 97 | } |
| vsluiter | 0:90873b4e8330 | 98 | |
| vsluiter | 0:90873b4e8330 | 99 | void LDC1000::writeSPI(uint8_t *data, uint8_t address, uint8_t num_bytes) |
| vsluiter | 0:90873b4e8330 | 100 | { |
| vsluiter | 0:90873b4e8330 | 101 | _cs_pin.write(0); |
| vsluiter | 0:90873b4e8330 | 102 | _spiport.write(address); |
| vsluiter | 0:90873b4e8330 | 103 | for(int i=0; i < num_bytes ; i++) |
| vsluiter | 0:90873b4e8330 | 104 | { |
| hamid567 | 1:a88df80e7664 | 105 | _spiport.write(data[i]); |
| vsluiter | 0:90873b4e8330 | 106 | } |
| vsluiter | 0:90873b4e8330 | 107 | _cs_pin.write(1); |
| hamid567 | 8:b5fb9681869c | 108 | } |
| hamid567 | 8:b5fb9681869c | 109 | |
| hamid567 | 11:0f53fbf73613 | 110 | |
| hamid567 | 11:0f53fbf73613 | 111 | // EXTRA test: Get&print values of all variables to verify (to calculate the induction) |
| hamid567 | 11:0f53fbf73613 | 112 | // The data will be printed on the screen using RealTerm: baud 9600. |
| hamid567 | 8:b5fb9681869c | 113 | // Begin *********************************************************** |
| hamid567 | 8:b5fb9681869c | 114 | float LDC1000::get_raw_l() {_raw_l = readRawCounts(); |
| hamid567 | 8:b5fb9681869c | 115 | return _raw_l;}; |
| hamid567 | 8:b5fb9681869c | 116 | float LDC1000::get_fsensor() { |
| hamid567 | 8:b5fb9681869c | 117 | uint16_t resp[] = {0, 0, 192, 384, 768, 1536, 3072, 6144}; |
| hamid567 | 8:b5fb9681869c | 118 | _raw_l = readRawCounts(); |
| hamid567 | 8:b5fb9681869c | 119 | _fsensor = (_frequency/(_raw_l*3.0))*resp[(uint8_t)(_responsetime)]; |
| hamid567 | 8:b5fb9681869c | 120 | return _fsensor;}; |
| hamid567 | 8:b5fb9681869c | 121 | |
| hamid567 | 8:b5fb9681869c | 122 | float LDC1000::get_frequency() {return _frequency;}; |
| hamid567 | 8:b5fb9681869c | 123 | float LDC1000::get_responsetime() {return _responsetime;}; |
| hamid567 | 8:b5fb9681869c | 124 | float LDC1000::get_cap() {return cap;}; |
| hamid567 | 12:312970050c8c | 125 | // END *********************************************************** |
