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