Lindholm Engineers / Mbed 2 deprecated Ultrasonic_Firmware

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TDC7200.h Source File

TDC7200.h

00001 //TDC7200.h
00002 //More plain-english functions for changing settings and stuff.
00003 //Created on 11 December 2015 by Eric Lindholm
00004 
00005 #include "mbed.h"
00006 #include "TI_Registers.h"
00007 
00008 #ifndef TDC7200_H
00009 #define TDC7200_H
00010 
00011 /** The SettingChoice variable
00012  *   The setting choice variable allows the program to look up what register and 
00013  *    bit number a setting corresponds to.
00014  */
00015  //This only applies to on-off settings.
00016  enum SettingChoice7200{FORCE_CAL, PARITY_EN, TRIGG_EDGE, STOP_EDGE, START_EDGE,
00017         START_MEAS, CLOCK_CNTR_OVF_MASK, COARSE_CNTR_OVF_MASK, NEW_MEAS_MASK};
00018 const int settingAddress7200[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03};
00019 const int settingLookUp7200[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x01, 0x04, 0x02, 0x01};
00020 
00021 
00022 /** TDC7200 class
00023  *   The TDC7200 class is a chip function manifest.  It controls and manipulates 
00024  *   settings, counters, interrupts, and masks.
00025  *   This file assumes that the appropriate pins are linked to the MCU.
00026  *   Also, any setting that is set to an invalid number will be set instead to the default.
00027  *   It should be noted that the TDC7200 gets its accuracy from the accuracy of its 
00028  *    clock driver.  So, a clock signal that varies by even 1% may give highly inaccurate times.
00029  *   Pinout Table from the TDC7200:
00030  *      1               |   Enable pin.  Turns chip on or off.
00031  *      8               |   Interrupt pin.  Signals when an event happens.
00032  *      9               |   MISO pin.  Goes to SPI driver.
00033  *      10              |   MOSI pin.  Goes to SPI driver.
00034  *      11              |   Chip select pin. Part of SPI system, but can go to any I/O pin.
00035  *      12              |   Serial clock pin. Part of SPI system, goes to SPI driver.
00036  */
00037 class TDC7200
00038 {
00039 public:
00040     //! The constructor takes all the pinout data.
00041     TDC7200(PinName EN = p29, PinName INTRPT = p30, PinName MISO = p12, PinName MOSI = p11, PinName SCLK = p13, PinName SPICS = p14);
00042     
00043     //! Please don't go mucking about in the registers if you don't know what you're doing.
00044     TI_Registers tdc7200_registers;
00045 
00046     /** ReadToggleSetting
00047      *  The read toggle setting function reads a toggle setting.
00048      *  NOTICE: a value of 'true' does not always mean the setting is on.
00049      *  Chart for toggle settings:
00050      *  FORCE_CAL               |   Ensures chip performs calibration, even after an interrupted measurement (default off)
00051      *  PARITY_EN               |   Turns on the parity bits for the results (default off)
00052      *  TRIGG_EDGE              |   Changes between using the rising edge for the trigger or the falling edge (default rising edge)
00053      *  STOP_EDGE               |   Changes between using the rising edge for the stop signal or the falling edge (default rising edge)
00054      *  START_EDGE              |   Changes between using the rising edge for the start signal or the falling edge (default rising edge)
00055      *  START_MEAS              |   Tells the chip to begin a new measurement, also resets results registers (default off)
00056      *  CLOCK_CNTR_OVF_MASK     |   Turns on the interrupt for the clock counter overflow (default enabled)
00057      *  COARSE_CNTR_OVF_MASK    |   Turns on the interrupt for the coarse counter overflow (default enabled)
00058      *  NEW_MEAS_MASK           |   turns on the interrupt for when a new measurement is completed (default enabled)
00059      *
00060      *      @param choice indicates which setting should be changed.
00061      */
00062     bool readToggleSetting(SettingChoice7200); 
00063    /** setToggle function
00064     *   This toggle function is very similar to the readToggle function, except that 
00065     *    instead of simply reading the setting value, it changes it.  If the value was on,
00066     *    this function turns it off, and if it was off, this function turns it on.
00067     *    Just like how toggles should work.
00068     *
00069     *   @param choice Choice indicates which setting you want to toggle.
00070     */
00071     void setToggle(SettingChoice7200);
00072     
00073     //! More specific functions
00074     
00075     /** MODE_SELECT
00076      *  There are two modes for this chip: mode 1 and mode 2.
00077      *  Mode 1 is for when your expected time-of-flight is less than about 
00078      *   twenty clock cycles (~500 ns).
00079      *  Mode 2 is for when your expected time-of-flight is higher than that.
00080      *   But still probably less than 2000 clock cycles.
00081      *  The reason this isn't a toggle function is that this chip has the ability
00082      *   to have more than two modes.  But they aren't listed in the documentation.
00083      */
00084     int readMODE_SELECT();
00085     void setMODE_SELECT(int);
00086     
00087     /** CALIBRATION2_PERIODS
00088      *  This setting controls how many clock cycles the second calibration measurement
00089      *   will go through.
00090      *  ---
00091      *  setting value       |   Actual number of cycles
00092      *  0                   |   2 
00093      *  1                   |   10
00094      *  2                   |   20
00095      *  3                   |   40
00096      */
00097      int readCALIBRATION2_PERIODS();
00098      void setCALIBRATION2_PERIODS(int);
00099      
00100     /** AVG_CYCLES
00101      *  The setting for the number of measurement cycles the chip will average
00102      *   a reading through.  The number of cycles is equal to 2 ^ (setting value)
00103      */
00104     int readAVG_CYCLES();
00105     void setAVG_CYCLES(int);
00106     
00107     /** NUM_STOP
00108      *  This setting is the number of stop signals the TDC7200 expects to see.
00109      *   It can only count up to five stop signals, though.
00110      *  A value of zero is one stop, one means two stops, and so on.
00111      */
00112      int readNUM_STOP();
00113      void setNUM_STOP(int);
00114      
00115     //! Maintenance Functions
00116     
00117     /** The enable pin
00118      *  The enable pin is actually a little weird.  This pin has to be set to off when 
00119      *   the chip initially receives power, then turned on for the chip to work.
00120      */
00121     bool read_EN();
00122     void set_EN(bool);
00123     
00124     /// The interrupt function
00125     void interruptReceived();
00126     
00127     //! The clock frequency in
00128     void setClockFrequencyIn(int);
00129     int readClockFrequencyIn();
00130     
00131     //! Other miscellaneous values
00132     int readCalibrationPeriods();
00133     int readAverageCycles();
00134     int readNumberStops();
00135     /// for readTimeOfFlight, the parameter is WHICH time-of-flight you want.
00136     double readTimeOfFlight(int);
00137     double readNormLSB();
00138     double readCalibrationCount();
00139 
00140     //! Finally, the two functions I want to use the most.
00141     void forceMeasurementRead();
00142     void startMeasurement();
00143     
00144     /** Formulae
00145      *  calibrationCount = (calibration2 - calibration1) / (calibration2_periods -1)
00146      *  normLSB = 1 / ((clock frequency) * (calibrationCount))
00147      *  MODE 1:
00148      *   timeOfFlight[n] = (time[n] * normLSB)
00149      *  MODE 2:
00150      *   offset = (clock period) - (calibration1) * (normLSB)
00151      *   timeOfFlight[n] = (normLSB) * (time[1] - time[n+1]) + (clock count[n]) * (clock period)
00152      */
00153 private:
00154     DigitalOut en;
00155     InterruptIn int_7200;
00156     int fclkin;
00157     int currentmode;
00158     bool parityBit;
00159     int calibrationPeriods, averageCycles, numberStops;
00160     double timeOfFlight[5], normLSB, calibrationCount;
00161 };
00162 #endif