Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Using TSI for capacitance measurement

  1. #1

    Using TSI for capacitance measurement

    Greetings everyone!

    I am currently trying to implement a capacitance measurement using the TSI module on the K20 MCU (Teensy 3.1 board). I thought to have grasped the principle of operation, but I am still running into some trouble. I can provide some example source code, which is mostly copied from the touch.c file:

    Code:
    #define nscn 0	// number of scans
    #define ps 0	// scan number prescaler
    #define iel 5	// electrode dis-/charge current
    #define iref 5	// reference oscillator dis-/charge current
    
    {
    	Serial.begin(9600);
    
    	SIM_SCGC5 |= SIM_SCGC5_TSI; // enable tsi clock
    	TSI0_GENCS = 0;				// reset tsi module
    	TSI0_PEN = (1 << 9);		// enable channel 9 (pin 0)
    	// set dis-/charge currents
    	TSI0_SCANC = TSI_SCANC_REFCHRG(iref) | TSI_SCANC_EXTCHRG(iel);
    	// set scan number + prescaler, activate module and initiate first scan (software trigger)
    	TSI0_GENCS = TSI_GENCS_NSCN(nscn) | TSI_GENCS_PS(ps) | TSI_GENCS_TSIEN | TSI_GENCS_SWTS;
    	delayMicroseconds(10);
    }
    
    uint16_t val;
    double c;
    void loop() 
    {
    	while (TSI0_GENCS & TSI_GENCS_SCNIP);	// wait for conversion to finish
    	delay(1);								// for for the output register to update
    	val = (TSI0_CNTR9 >> 16) & 0xFFFF;		// read content of the output register
    	delay(10);
    	// using Figure 50-39. Equation 5 from the reference manula to calculate the capacitance
    	c = (double)val  * (iel + 1) / ((nscn + 1) * (1 << ps) * (iref + 1));
    	Serial.println(c);
    
    	TSI0_GENCS |= TSI_GENCS_SWTS;	// initiate next scan
    }
    Running this code with a 1000pF capacitor attached gives a result of approx. 900. With all the parameters set to 0 (see #define) counts is equal to pF. Now according to my understanding, changing for example both currents simultaneously should not change the result, only increase the frequencies. In my case setting for example both currents to 12 ľA (iel = iref = 5) gives a scan result of only 745 and ths trend continues further the more the currents are increased. This effect can only be observed when only one current is changed.
    So my quesions basically are: Am I overlooking something? Are the formulas I provided are not correct? Are the tolerances on the current settings so bad?

    As an aside I was confused by this line in the kinetis.h file:
    Code:
    #define TSI_SCANC_EXTCHRG(n)		(((n) & 7) << 16)
    Since the external current register is 4 bits long, should it not be:
    Code:
    #define TSI_SCANC_EXTCHRG(n)		(((n) & 15) << 16)
    Is this maybe an error source? Are the current values provided by the reference manual not correct (see page 1350p)?

    I would be very thankful for any hints you can provide me with!
    Thanks a lot in advance and have a nice day all of you,
    Chrono.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    -
    Posts
    9,756
    I think you're right,
    #define TSI_SCANC_EXTCHRG(n) (((n) & 15) << 16)

    is correct.

    Pullrequest:
    https://github.com/PaulStoffregen/cores/pull/87
    Last edited by Frank B; 09-09-2015 at 09:35 PM. Reason: added bugfix-pullrequest

  3. #3
    I am glad to help improving the software.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •