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.
Fork of TSI by
TSISensor.cpp
00001 #if defined(TARGET_KL25Z) 00002 /* Freescale Semiconductor Inc. 00003 * (c) Copyright 2004-2005 Freescale Semiconductor, Inc. 00004 * (c) Copyright 2001-2004 Motorola, Inc. 00005 * 00006 * mbed Microcontroller Library 00007 * (c) Copyright 2009-2012 ARM Limited. 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00010 * and associated documentation files (the "Software"), to deal in the Software without 00011 * restriction, including without limitation the rights to use, copy, modify, merge, publish, 00012 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 00013 * Software is furnished to do so, subject to the following conditions: 00014 * 00015 * The above copyright notice and this permission notice shall be included in all copies or 00016 * substantial portions of the Software. 00017 * 00018 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00019 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00020 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00021 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00023 */ 00024 00025 #include "mbed.h" 00026 #include "TSISensor.h" 00027 00028 #define NO_TOUCH 0 00029 #define SLIDER_LENGTH 40 //LENGTH in mm 00030 #define TOTAL_ELECTRODE 2 00031 00032 #define TSI0a 0 00033 #define TSI1 1 00034 #define TSI2 2 00035 #define TSI3 3 00036 #define TSI4 4 00037 #define TSI5 5 00038 #define TSI6 6 00039 #define TSI7 7 00040 #define TSI8 8 00041 #define TSI9 9 00042 #define TSI10 10 00043 #define TSI11 11 00044 #define TSI12 12 00045 #define TSI13 13 00046 #define TSI14 14 00047 #define TSI15 15 00048 00049 /*Chose the correct TSI channel for the electrode number*/ 00050 #define ELECTRODE0 TSI9 00051 #define ELECTRODE1 TSI10 00052 #define ELECTRODE2 TSI0a 00053 #define ELECTRODE3 TSI1 00054 #define ELECTRODE4 TSI2 00055 #define ELECTRODE5 TSI3 00056 #define ELECTRODE6 TSI4 00057 #define ELECTRODE7 TSI5 00058 #define ELECTRODE8 TSI6 00059 #define ELECTRODE9 TSI7 00060 #define ELECTRODE10 TSI8 00061 #define ELECTRODE11 TSI11 00062 #define ELECTRODE12 TSI12 00063 #define ELECTRODE13 TSI13 00064 #define ELECTRODE14 TSI14 00065 #define ELECTRODE15 TSI15 00066 00067 #define THRESHOLD0 100 00068 #define THRESHOLD1 100 00069 #define THRESHOLD2 100 00070 #define THRESHOLD3 100 00071 #define THRESHOLD4 100 00072 #define THRESHOLD5 100 00073 #define THRESHOLD6 100 00074 #define THRESHOLD7 100 00075 #define THRESHOLD8 100 00076 #define THRESHOLD9 100 00077 #define THRESHOLD10 100 00078 #define THRESHOLD11 100 00079 #define THRESHOLD12 100 00080 #define THRESHOLD13 100 00081 #define THRESHOLD14 100 00082 #define THRESHOLD15 100 00083 00084 static uint8_t total_electrode = TOTAL_ELECTRODE; 00085 static uint8_t elec_array[16]={ELECTRODE0,ELECTRODE1,ELECTRODE2,ELECTRODE3,ELECTRODE4,ELECTRODE5, 00086 ELECTRODE6,ELECTRODE7,ELECTRODE8,ELECTRODE9,ELECTRODE10,ELECTRODE11, 00087 ELECTRODE12,ELECTRODE13,ELECTRODE14,ELECTRODE15}; 00088 static uint16_t gu16TSICount[16]; 00089 static uint16_t gu16Baseline[16]; 00090 static uint16_t gu16Threshold[16]={THRESHOLD0,THRESHOLD1,THRESHOLD2,THRESHOLD3,THRESHOLD4,THRESHOLD5, 00091 THRESHOLD6,THRESHOLD7,THRESHOLD8,THRESHOLD9,THRESHOLD10,THRESHOLD11, 00092 THRESHOLD12,THRESHOLD13,THRESHOLD14,THRESHOLD15}; 00093 static uint16_t gu16Delta[16]; 00094 static uint8_t ongoing_elec; 00095 static uint8_t end_flag = 1; 00096 00097 static uint8_t SliderPercentegePosition[2] = {NO_TOUCH,NO_TOUCH}; 00098 static uint8_t SliderDistancePosition[2] = {NO_TOUCH,NO_TOUCH}; 00099 static uint32_t AbsolutePercentegePosition = NO_TOUCH; 00100 static uint32_t AbsoluteDistancePosition = NO_TOUCH; 00101 00102 static void tsi_irq(); 00103 00104 TSISensor::TSISensor() { 00105 SIM->SCGC5 |= SIM_SCGC5_PORTB_MASK; 00106 SIM->SCGC5 |= SIM_SCGC5_TSI_MASK; 00107 00108 TSI0->GENCS |= (TSI_GENCS_ESOR_MASK 00109 | TSI_GENCS_MODE(0) 00110 | TSI_GENCS_REFCHRG(4) 00111 | TSI_GENCS_DVOLT(0) 00112 | TSI_GENCS_EXTCHRG(7) 00113 | TSI_GENCS_PS(4) 00114 | TSI_GENCS_NSCN(11) 00115 | TSI_GENCS_TSIIEN_MASK 00116 | TSI_GENCS_STPE_MASK 00117 ); 00118 00119 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; 00120 00121 NVIC_SetVector(TSI0_IRQn, (uint32_t)&tsi_irq); 00122 NVIC_EnableIRQ(TSI0_IRQn); 00123 00124 selfCalibration(); 00125 } 00126 00127 00128 void TSISensor::selfCalibration(void) 00129 { 00130 unsigned char cnt; 00131 unsigned char trigger_backup; 00132 00133 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag 00134 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module 00135 00136 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application 00137 trigger_backup = 1; 00138 else 00139 trigger_backup = 0; 00140 00141 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger 00142 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts 00143 00144 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module 00145 00146 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed 00147 { 00148 TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) ); 00149 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00150 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK)); 00151 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; 00152 gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); 00153 } 00154 00155 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module 00156 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt 00157 if(trigger_backup) // Restore trigger mode 00158 TSI0->GENCS |= TSI_GENCS_STM_MASK; 00159 else 00160 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; 00161 00162 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module 00163 00164 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) ); 00165 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00166 } 00167 00168 00169 void TSISensor::sliderRead(void ) { 00170 if(end_flag) { 00171 end_flag = 0; 00172 if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) { 00173 SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]); 00174 SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]); 00175 SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100; 00176 SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100; 00177 AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2; 00178 AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2; 00179 } else { 00180 SliderPercentegePosition[0] = NO_TOUCH; 00181 SliderPercentegePosition[1] = NO_TOUCH; 00182 SliderDistancePosition[0] = NO_TOUCH; 00183 SliderDistancePosition[1] = NO_TOUCH; 00184 AbsolutePercentegePosition = NO_TOUCH; 00185 AbsoluteDistancePosition = NO_TOUCH; 00186 } 00187 } 00188 } 00189 00190 float TSISensor::readPercentage() { 00191 sliderRead(); 00192 return (float)AbsolutePercentegePosition/100.0; 00193 } 00194 00195 uint8_t TSISensor::readDistance() { 00196 sliderRead(); 00197 return AbsoluteDistancePosition; 00198 } 00199 00200 00201 static void changeElectrode(void) 00202 { 00203 int16_t u16temp_delta; 00204 00205 gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode 00206 u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference 00207 if(u16temp_delta < 0) 00208 gu16Delta[ongoing_elec] = 0; 00209 else 00210 gu16Delta[ongoing_elec] = u16temp_delta; 00211 00212 //Change Electrode to Scan 00213 if(total_electrode > 1) 00214 { 00215 if((total_electrode-1) > ongoing_elec) 00216 ongoing_elec++; 00217 else 00218 ongoing_elec = 0; 00219 00220 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) ); 00221 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00222 } 00223 } 00224 00225 00226 void tsi_irq(void) 00227 { 00228 end_flag = 1; 00229 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag 00230 changeElectrode(); 00231 } 00232 00233 #endif // defined(TARGET_KL25Z)
Generated on Sat Jul 16 2022 00:47:56 by
1.7.2
