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 BX-car_2 by
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 void TSISensor::selfCalibration(void) 00127 { 00128 unsigned char cnt; 00129 unsigned char trigger_backup; 00130 00131 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag 00132 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module 00133 00134 if(TSI0->GENCS & TSI_GENCS_STM_MASK) // Back-up TSI Trigger mode from Application 00135 trigger_backup = 1; 00136 else 00137 trigger_backup = 0; 00138 00139 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; // Use SW trigger 00140 TSI0->GENCS &= ~TSI_GENCS_TSIIEN_MASK; // Enable TSI interrupts 00141 00142 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module 00143 00144 for(cnt=0; cnt < total_electrode; cnt++) // Get Counts when Electrode not pressed 00145 { 00146 TSI0->DATA = ((elec_array[cnt] << TSI_DATA_TSICH_SHIFT) ); 00147 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00148 while(!(TSI0->GENCS & TSI_GENCS_EOSF_MASK)); 00149 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; 00150 gu16Baseline[cnt] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); 00151 } 00152 00153 TSI0->GENCS &= ~TSI_GENCS_TSIEN_MASK; // Disable TSI module 00154 TSI0->GENCS |= TSI_GENCS_TSIIEN_MASK; // Enale TSI interrupt 00155 if(trigger_backup) // Restore trigger mode 00156 TSI0->GENCS |= TSI_GENCS_STM_MASK; 00157 else 00158 TSI0->GENCS &= ~TSI_GENCS_STM_MASK; 00159 00160 TSI0->GENCS |= TSI_GENCS_TSIEN_MASK; // Enable TSI module 00161 00162 TSI0->DATA = ((elec_array[0]<<TSI_DATA_TSICH_SHIFT) ); 00163 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00164 } 00165 00166 void TSISensor::sliderRead(void ) { 00167 if(end_flag) { 00168 end_flag = 0; 00169 if((gu16Delta[0] > gu16Threshold[0])||(gu16Delta[1] > gu16Threshold[1])) { 00170 SliderPercentegePosition[0] = (gu16Delta[0]*100)/(gu16Delta[0]+gu16Delta[1]); 00171 SliderPercentegePosition[1] = (gu16Delta[1]*100)/(gu16Delta[0]+gu16Delta[1]); 00172 SliderDistancePosition[0] = (SliderPercentegePosition[0]* SLIDER_LENGTH)/100; 00173 SliderDistancePosition[1] = (SliderPercentegePosition[1]* SLIDER_LENGTH)/100; 00174 AbsolutePercentegePosition = ((100 - SliderPercentegePosition[0]) + SliderPercentegePosition[1])/2; 00175 AbsoluteDistancePosition = ((SLIDER_LENGTH - SliderDistancePosition[0]) + SliderDistancePosition[1])/2; 00176 } else { 00177 SliderPercentegePosition[0] = NO_TOUCH; 00178 SliderPercentegePosition[1] = NO_TOUCH; 00179 SliderDistancePosition[0] = NO_TOUCH; 00180 SliderDistancePosition[1] = NO_TOUCH; 00181 AbsolutePercentegePosition = NO_TOUCH; 00182 AbsoluteDistancePosition = NO_TOUCH; 00183 } 00184 } 00185 } 00186 00187 float TSISensor::readPercentage() { 00188 sliderRead(); 00189 return (float)AbsolutePercentegePosition/100.0; 00190 } 00191 00192 uint8_t TSISensor::readDistance() { 00193 sliderRead(); 00194 return AbsoluteDistancePosition; 00195 } 00196 00197 static void changeElectrode(void) 00198 { 00199 int16_t u16temp_delta; 00200 00201 gu16TSICount[ongoing_elec] = (TSI0->DATA & TSI_DATA_TSICNT_MASK); // Save Counts for current electrode 00202 u16temp_delta = gu16TSICount[ongoing_elec] - gu16Baseline[ongoing_elec]; // Obtains Counts Delta from callibration reference 00203 if(u16temp_delta < 0) 00204 gu16Delta[ongoing_elec] = 0; 00205 else 00206 gu16Delta[ongoing_elec] = u16temp_delta; 00207 00208 //Change Electrode to Scan 00209 if(total_electrode > 1) 00210 { 00211 if((total_electrode-1) > ongoing_elec) 00212 ongoing_elec++; 00213 else 00214 ongoing_elec = 0; 00215 00216 TSI0->DATA = ((elec_array[ongoing_elec]<<TSI_DATA_TSICH_SHIFT) ); 00217 TSI0->DATA |= TSI_DATA_SWTS_MASK; 00218 } 00219 } 00220 00221 void tsi_irq(void) 00222 { 00223 end_flag = 1; 00224 TSI0->GENCS |= TSI_GENCS_EOSF_MASK; // Clear End of Scan Flag 00225 changeElectrode(); 00226 }
Generated on Tue Jul 12 2022 21:33:29 by
