fastest sampling rate

Dependencies:   mbed

Committer:
TimeString
Date:
Mon Feb 03 02:45:03 2014 +0000
Revision:
0:459f1731fa6a
fastest sampling rate for all sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TimeString 0:459f1731fa6a 1 /* Freescale Semiconductor Inc.
TimeString 0:459f1731fa6a 2 * (c) Copyright 2004-2005 Freescale Semiconductor, Inc.
TimeString 0:459f1731fa6a 3 * (c) Copyright 2001-2004 Motorola, Inc.
TimeString 0:459f1731fa6a 4 *
TimeString 0:459f1731fa6a 5 * mbed Microcontroller Library
TimeString 0:459f1731fa6a 6 * (c) Copyright 2009-2012 ARM Limited.
TimeString 0:459f1731fa6a 7 *
TimeString 0:459f1731fa6a 8 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
TimeString 0:459f1731fa6a 9 * and associated documentation files (the "Software"), to deal in the Software without
TimeString 0:459f1731fa6a 10 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
TimeString 0:459f1731fa6a 11 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
TimeString 0:459f1731fa6a 12 * Software is furnished to do so, subject to the following conditions:
TimeString 0:459f1731fa6a 13 *
TimeString 0:459f1731fa6a 14 * The above copyright notice and this permission notice shall be included in all copies or
TimeString 0:459f1731fa6a 15 * substantial portions of the Software.
TimeString 0:459f1731fa6a 16 *
TimeString 0:459f1731fa6a 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
TimeString 0:459f1731fa6a 18 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
TimeString 0:459f1731fa6a 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
TimeString 0:459f1731fa6a 20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
TimeString 0:459f1731fa6a 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
TimeString 0:459f1731fa6a 22 */
TimeString 0:459f1731fa6a 23
TimeString 0:459f1731fa6a 24 #include "mbed.h"
TimeString 0:459f1731fa6a 25 #include "TSISensor.h"
TimeString 0:459f1731fa6a 26
TimeString 0:459f1731fa6a 27 #define NO_TOUCH 0
TimeString 0:459f1731fa6a 28 #define SLIDER_LENGTH 40 //LENGTH in mm
TimeString 0:459f1731fa6a 29 #define TOTAL_ELECTRODE 3
TimeString 0:459f1731fa6a 30
TimeString 0:459f1731fa6a 31 #define TSI0a 0
TimeString 0:459f1731fa6a 32 #define TSI1 1
TimeString 0:459f1731fa6a 33 #define TSI2 2
TimeString 0:459f1731fa6a 34 #define TSI3 3
TimeString 0:459f1731fa6a 35 #define TSI4 4
TimeString 0:459f1731fa6a 36 #define TSI5 5
TimeString 0:459f1731fa6a 37 #define TSI6 6
TimeString 0:459f1731fa6a 38 #define TSI7 7
TimeString 0:459f1731fa6a 39 #define TSI8 8
TimeString 0:459f1731fa6a 40 #define TSI9 9
TimeString 0:459f1731fa6a 41 #define TSI10 10
TimeString 0:459f1731fa6a 42 #define TSI11 11
TimeString 0:459f1731fa6a 43 #define TSI12 12
TimeString 0:459f1731fa6a 44 #define TSI13 13
TimeString 0:459f1731fa6a 45 #define TSI14 14
TimeString 0:459f1731fa6a 46 #define TSI15 15
TimeString 0:459f1731fa6a 47
TimeString 0:459f1731fa6a 48 /*Chose the correct TSI channel for the electrode number*/
TimeString 0:459f1731fa6a 49 #define ELECTRODE0 TSI9
TimeString 0:459f1731fa6a 50 #define ELECTRODE1 TSI10
TimeString 0:459f1731fa6a 51 #define ELECTRODE2 TSI0a
TimeString 0:459f1731fa6a 52 #define ELECTRODE3 TSI1
TimeString 0:459f1731fa6a 53 #define ELECTRODE4 TSI2
TimeString 0:459f1731fa6a 54 #define ELECTRODE5 TSI3
TimeString 0:459f1731fa6a 55 #define ELECTRODE6 TSI4
TimeString 0:459f1731fa6a 56 #define ELECTRODE7 TSI5
TimeString 0:459f1731fa6a 57 #define ELECTRODE8 TSI6
TimeString 0:459f1731fa6a 58 #define ELECTRODE9 TSI7
TimeString 0:459f1731fa6a 59 #define ELECTRODE10 TSI8
TimeString 0:459f1731fa6a 60 #define ELECTRODE11 TSI11
TimeString 0:459f1731fa6a 61 #define ELECTRODE12 TSI12
TimeString 0:459f1731fa6a 62 #define ELECTRODE13 TSI13
TimeString 0:459f1731fa6a 63 #define ELECTRODE14 TSI14
TimeString 0:459f1731fa6a 64 #define ELECTRODE15 TSI15
TimeString 0:459f1731fa6a 65
TimeString 0:459f1731fa6a 66 #define THRESHOLD0 100
TimeString 0:459f1731fa6a 67 #define THRESHOLD1 100
TimeString 0:459f1731fa6a 68 #define THRESHOLD2 100
TimeString 0:459f1731fa6a 69 #define THRESHOLD3 100
TimeString 0:459f1731fa6a 70 #define THRESHOLD4 100
TimeString 0:459f1731fa6a 71 #define THRESHOLD5 100
TimeString 0:459f1731fa6a 72 #define THRESHOLD6 100
TimeString 0:459f1731fa6a 73 #define THRESHOLD7 100
TimeString 0:459f1731fa6a 74 #define THRESHOLD8 100
TimeString 0:459f1731fa6a 75 #define THRESHOLD9 100
TimeString 0:459f1731fa6a 76 #define THRESHOLD10 100
TimeString 0:459f1731fa6a 77 #define THRESHOLD11 100
TimeString 0:459f1731fa6a 78 #define THRESHOLD12 100
TimeString 0:459f1731fa6a 79 #define THRESHOLD13 100
TimeString 0:459f1731fa6a 80 #define THRESHOLD14 100
TimeString 0:459f1731fa6a 81 #define THRESHOLD15 100
TimeString 0:459f1731fa6a 82
TimeString 0:459f1731fa6a 83 static uint8_t total_electrode = TOTAL_ELECTRODE;
TimeString 0:459f1731fa6a 84 static uint8_t elec_array[16]={ELECTRODE0,ELECTRODE1,ELECTRODE2,ELECTRODE3,ELECTRODE4,ELECTRODE5,
TimeString 0:459f1731fa6a 85 ELECTRODE6,ELECTRODE7,ELECTRODE8,ELECTRODE9,ELECTRODE10,ELECTRODE11,
TimeString 0:459f1731fa6a 86 ELECTRODE12,ELECTRODE13,ELECTRODE14,ELECTRODE15};
TimeString 0:459f1731fa6a 87 static uint16_t gu16TSICount[16];
TimeString 0:459f1731fa6a 88 static uint16_t gu16Baseline[16];
TimeString 0:459f1731fa6a 89 static uint16_t gu16Threshold[16]={THRESHOLD0,THRESHOLD1,THRESHOLD2,THRESHOLD3,THRESHOLD4,THRESHOLD5,
TimeString 0:459f1731fa6a 90 THRESHOLD6,THRESHOLD7,THRESHOLD8,THRESHOLD9,THRESHOLD10,THRESHOLD11,
TimeString 0:459f1731fa6a 91 THRESHOLD12,THRESHOLD13,THRESHOLD14,THRESHOLD15};
TimeString 0:459f1731fa6a 92 static uint16_t gu16Delta[16];
TimeString 0:459f1731fa6a 93 static uint8_t ongoing_elec;
TimeString 0:459f1731fa6a 94 static uint8_t end_flag = 1;
TimeString 0:459f1731fa6a 95
TimeString 0:459f1731fa6a 96 static uint8_t SliderPercentegePosition[2] = {NO_TOUCH,NO_TOUCH};
TimeString 0:459f1731fa6a 97 static uint8_t SliderDistancePosition[2] = {NO_TOUCH,NO_TOUCH};
TimeString 0:459f1731fa6a 98 static uint32_t AbsolutePercentegePosition = NO_TOUCH;
TimeString 0:459f1731fa6a 99 static uint32_t AbsoluteDistancePosition = NO_TOUCH;
TimeString 0:459f1731fa6a 100
TimeString 0:459f1731fa6a 101 static void tsi_irq();
TimeString 0:459f1731fa6a 102
TimeString 0:459f1731fa6a 103 TSISensor::TSISensor() {
TimeString 0:459f1731fa6a 104 SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
TimeString 0:459f1731fa6a 105 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
TimeString 0:459f1731fa6a 106
TimeString 0:459f1731fa6a 107 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
TimeString 0:459f1731fa6a 108 | TSI_GENCS_MODE(0)
TimeString 0:459f1731fa6a 109 | TSI_GENCS_REFCHRG(4)
TimeString 0:459f1731fa6a 110 | TSI_GENCS_DVOLT(0)
TimeString 0:459f1731fa6a 111 | TSI_GENCS_EXTCHRG(7)
TimeString 0:459f1731fa6a 112 | TSI_GENCS_PS(4)
TimeString 0:459f1731fa6a 113 | TSI_GENCS_NSCN(11)
TimeString 0:459f1731fa6a 114 | TSI_GENCS_TSIIEN_MASK
TimeString 0:459f1731fa6a 115 | TSI_GENCS_STPE_MASK
TimeString 0:459f1731fa6a 116 );
TimeString 0:459f1731fa6a 117
TimeString 0:459f1731fa6a 118 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
TimeString 0:459f1731fa6a 119
TimeString 0:459f1731fa6a 120 NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
TimeString 0:459f1731fa6a 121 NVIC_EnableIRQ(TSI0_IRQn);
TimeString 0:459f1731fa6a 122
TimeString 0:459f1731fa6a 123 selfCalibration();
TimeString 0:459f1731fa6a 124 }
TimeString 0:459f1731fa6a 125
TimeString 0:459f1731fa6a 126 void TSISensor::TSISensor_reset(void) {
TimeString 0:459f1731fa6a 127 SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK;
TimeString 0:459f1731fa6a 128 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK;
TimeString 0:459f1731fa6a 129
TimeString 0:459f1731fa6a 130 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK
TimeString 0:459f1731fa6a 131 | TSI_GENCS_MODE(0)
TimeString 0:459f1731fa6a 132 | TSI_GENCS_REFCHRG(4)
TimeString 0:459f1731fa6a 133 | TSI_GENCS_DVOLT(0)
TimeString 0:459f1731fa6a 134 | TSI_GENCS_EXTCHRG(7)
TimeString 0:459f1731fa6a 135 | TSI_GENCS_PS(4)
TimeString 0:459f1731fa6a 136 | TSI_GENCS_NSCN(11)
TimeString 0:459f1731fa6a 137 | TSI_GENCS_TSIIEN_MASK
TimeString 0:459f1731fa6a 138 | TSI_GENCS_STPE_MASK
TimeString 0:459f1731fa6a 139 );
TimeString 0:459f1731fa6a 140
TimeString 0:459f1731fa6a 141 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK;
TimeString 0:459f1731fa6a 142
TimeString 0:459f1731fa6a 143 //NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq);
TimeString 0:459f1731fa6a 144 //NVIC_EnableIRQ(TSI0_IRQn);
TimeString 0:459f1731fa6a 145
TimeString 0:459f1731fa6a 146 selfCalibration();
TimeString 0:459f1731fa6a 147 }
TimeString 0:459f1731fa6a 148
TimeString 0:459f1731fa6a 149 void TSISensor::selfCalibration(void)
TimeString 0:459f1731fa6a 150 {
TimeString 0:459f1731fa6a 151 unsigned char cnt;
TimeString 0:459f1731fa6a 152 unsigned char trigger_backup;
TimeString 0:459f1731fa6a 153
TimeString 0:459f1731fa6a 154 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
TimeString 0:459f1731fa6a 155 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
TimeString 0:459f1731fa6a 156
TimeString 0:459f1731fa6a 157 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application
TimeString 0:459f1731fa6a 158 trigger_backup = 1;
TimeString 0:459f1731fa6a 159 else
TimeString 0:459f1731fa6a 160 trigger_backup = 0;
TimeString 0:459f1731fa6a 161
TimeString 0:459f1731fa6a 162 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger
TimeString 0:459f1731fa6a 163 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts
TimeString 0:459f1731fa6a 164
TimeString 0:459f1731fa6a 165 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
TimeString 0:459f1731fa6a 166
TimeString 0:459f1731fa6a 167 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed
TimeString 0:459f1731fa6a 168 {
TimeString 0:459f1731fa6a 169 TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) );
TimeString 0:459f1731fa6a 170 TSI0->DATA |= TSI_DATA_SWTS_MASK;
TimeString 0:459f1731fa6a 171 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK));
TimeString 0:459f1731fa6a 172 TSI0->GENCS |= TSI_GENCS_EOSF_MASK;
TimeString 0:459f1731fa6a 173 gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK);
TimeString 0:459f1731fa6a 174 }
TimeString 0:459f1731fa6a 175
TimeString 0:459f1731fa6a 176 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module
TimeString 0:459f1731fa6a 177 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt
TimeString 0:459f1731fa6a 178 if(trigger_backup) // Restore trigger mode
TimeString 0:459f1731fa6a 179 TSI0->GENCS |= TSI_GENCS_STM_MASK;
TimeString 0:459f1731fa6a 180 else
TimeString 0:459f1731fa6a 181 TSI0->GENCS &= ~TSI_GENCS_STM_MASK;
TimeString 0:459f1731fa6a 182
TimeString 0:459f1731fa6a 183 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module
TimeString 0:459f1731fa6a 184
TimeString 0:459f1731fa6a 185 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) );
TimeString 0:459f1731fa6a 186 TSI0->DATA |= TSI_DATA_SWTS_MASK;
TimeString 0:459f1731fa6a 187 }
TimeString 0:459f1731fa6a 188
TimeString 0:459f1731fa6a 189 void TSISensor::sliderRead(void ) {
TimeString 0:459f1731fa6a 190 if(end_flag) {
TimeString 0:459f1731fa6a 191 end_flag = 0;
TimeString 0:459f1731fa6a 192 if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) {
TimeString 0:459f1731fa6a 193 SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]);
TimeString 0:459f1731fa6a 194 SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]);
TimeString 0:459f1731fa6a 195 SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100;
TimeString 0:459f1731fa6a 196 SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100;
TimeString 0:459f1731fa6a 197 AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2;
TimeString 0:459f1731fa6a 198 AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2;
TimeString 0:459f1731fa6a 199 } else {
TimeString 0:459f1731fa6a 200 SliderPercentegePosition[0] = NO_TOUCH;
TimeString 0:459f1731fa6a 201 SliderPercentegePosition[1] = NO_TOUCH;
TimeString 0:459f1731fa6a 202 SliderDistancePosition[0] = NO_TOUCH;
TimeString 0:459f1731fa6a 203 SliderDistancePosition[1] = NO_TOUCH;
TimeString 0:459f1731fa6a 204 AbsolutePercentegePosition = NO_TOUCH;
TimeString 0:459f1731fa6a 205 AbsoluteDistancePosition = NO_TOUCH;
TimeString 0:459f1731fa6a 206 }
TimeString 0:459f1731fa6a 207 }
TimeString 0:459f1731fa6a 208 }
TimeString 0:459f1731fa6a 209
TimeString 0:459f1731fa6a 210 float TSISensor::readPercentage() {
TimeString 0:459f1731fa6a 211 sliderRead();
TimeString 0:459f1731fa6a 212 return (float)AbsolutePercentegePosition/100.0;
TimeString 0:459f1731fa6a 213 }
TimeString 0:459f1731fa6a 214
TimeString 0:459f1731fa6a 215 uint8_t TSISensor::readDistance() {
TimeString 0:459f1731fa6a 216 sliderRead();
TimeString 0:459f1731fa6a 217 return AbsoluteDistancePosition;
TimeString 0:459f1731fa6a 218 }
TimeString 0:459f1731fa6a 219
TimeString 0:459f1731fa6a 220 uint16_t TSISensor::readValue(uint8_t index)
TimeString 0:459f1731fa6a 221 {
TimeString 0:459f1731fa6a 222 return gu16TSICount[index];
TimeString 0:459f1731fa6a 223 }
TimeString 0:459f1731fa6a 224
TimeString 0:459f1731fa6a 225 static void changeElectrode(void)
TimeString 0:459f1731fa6a 226 {
TimeString 0:459f1731fa6a 227 int16_t u16temp_delta;
TimeString 0:459f1731fa6a 228
TimeString 0:459f1731fa6a 229 gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode
TimeString 0:459f1731fa6a 230 u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference
TimeString 0:459f1731fa6a 231 if(u16temp_delta < 0)
TimeString 0:459f1731fa6a 232 gu16Delta[ongoing_elec] = 0;
TimeString 0:459f1731fa6a 233 else
TimeString 0:459f1731fa6a 234 gu16Delta[ongoing_elec] = u16temp_delta;
TimeString 0:459f1731fa6a 235
TimeString 0:459f1731fa6a 236 //Change Electrode to Scan
TimeString 0:459f1731fa6a 237 if(total_electrode > 1)
TimeString 0:459f1731fa6a 238 {
TimeString 0:459f1731fa6a 239 if((total_electrode-1) > ongoing_elec)
TimeString 0:459f1731fa6a 240 ongoing_elec++;
TimeString 0:459f1731fa6a 241 else
TimeString 0:459f1731fa6a 242 ongoing_elec = 0;
TimeString 0:459f1731fa6a 243
TimeString 0:459f1731fa6a 244 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) );
TimeString 0:459f1731fa6a 245 TSI0->DATA |= TSI_DATA_SWTS_MASK;
TimeString 0:459f1731fa6a 246 }
TimeString 0:459f1731fa6a 247 }
TimeString 0:459f1731fa6a 248
TimeString 0:459f1731fa6a 249 void tsi_irq(void)
TimeString 0:459f1731fa6a 250 {
TimeString 0:459f1731fa6a 251 end_flag = 1;
TimeString 0:459f1731fa6a 252 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag
TimeString 0:459f1731fa6a 253 changeElectrode();
TimeString 0:459f1731fa6a 254 }