useless

Dependents:   Final_Homework1 ee202Homework1 EE202A_Homework1 PRVIZ_LV8

Fork of TSI by First Last

Committer:
bmdlh
Date:
Sat Feb 08 22:20:24 2014 +0000
Revision:
5:a192baf6250c
Parent:
4:4dc2f5a3a731
cai cai wo shi shui

Who changed what in which revision?

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