FRDM_wahadlo_torsyjne

Dependencies:   mbed BufferedSerial

Fork of FRDM_wahadlo_torsyjne by Wojciech M

Committer:
Pawel_13
Date:
Tue Jun 10 15:32:03 2014 +0000
Revision:
8:434d613f0929
Pozosta?my do jutra przy tej wersji. Przy tej szybko?ci pomiaru zapis do portu szeregowego spowalnia jej wykonanie (trwa zbyt d?ugo).

Who changed what in which revision?

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