| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | |
| 2 | /***************************************************************************** |
||
| 3 | * FileName: mTouchCap_PIC18_CTMU_Physical.c |
||
| 4 | * Dependencies: |
||
| 5 | * Processor: PIC18 |
||
| 6 | * Compiler: C18 |
||
| 7 | * Linker: MPLINK |
||
| 8 | * Company: Microchip Technology Incorporated |
||
| 9 | * |
||
| 10 | * Software License Agreement |
||
| 11 | * |
||
| 12 | * Copyright © 2009 Microchip Technology Inc. |
||
| 13 | * Microchip licenses this software to you solely for use with Microchip products, according to the terms of the |
||
| 14 | * accompanying click-wrap software license. Microchip and its licensors retain all right, title and interest in |
||
| 15 | * and to the software. All rights reserved. This software and any accompanying information is for suggestion only. |
||
| 16 | * It shall not be deemed to modify Microchips standard warranty for its products. It is your responsibility to ensure |
||
| 17 | * that this software meets your requirements. |
||
| 18 | |||
| 19 | * SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
||
| 20 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT |
||
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP OR ITS LICENSORS BE LIABLE |
||
| 22 | * FOR ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT LIMITED TO INCIDENTAL, |
||
| 23 | * SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, |
||
| 24 | * COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, OR ANY CLAIMS BY THIRD PARTIES |
||
| 25 | * (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. |
||
| 26 | * The aggregate and cumulative liability of Microchip and its licensors for damages related to the use of the software |
||
| 27 | * will in no event exceed $1000 or the amount you paid Microchip for the software, whichever is greater. |
||
| 28 | |||
| 29 | * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE TERMS AND |
||
| 30 | * THE TERMS OF THE ACCOMPANYING CLICK-WRAP SOFTWARE LICENSE. |
||
| 31 | * |
||
| 32 | * |
||
| 33 | * Author Date Comment |
||
| 34 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
| 35 | * Naveen. M 14 Apr 2009 Version 0.1 Initial Draft |
||
| 36 | * Nithin Kumar. M 14 May 2009 Version 0.2 Updates |
||
| 37 | * Sasha. M / Naveen. M 4 June 2009 Version 0.3 Updates |
||
| 38 | * Sasha. M / Naveen. M 11 Nov 2009 Version 1.0 Release |
||
| 39 | * Sasha. M / Nithin. 10 April 2010 Version 1.20 Release |
||
| 40 | *****************************************************************************/ |
||
| 41 | |||
| 42 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 43 | /* ~~~~~~~~~~~~~~~~~~~~~ Includes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 44 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 45 | #include "mTouchCap_PIC18_CTMU_Physical.h" |
||
| 46 | |||
| 47 | |||
| 48 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 49 | /* ~~~~~~~~~~~~~~~~~~~~~ Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 50 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 51 | #pragma udata udata_mTouchCap_PIC18_CTMU_Physical_section1 |
||
| 52 | |||
| 53 | WORD curRawData[MAX_ADC_CHANNELS]; //Storage for CTMU channel values |
||
| 54 | WORD tripValue[MAX_ADC_CHANNELS]; //Storage for the trip point for each channel |
||
| 55 | WORD hystValue[MAX_ADC_CHANNELS]; //Storage for the hysterisis value for each channel |
||
| 56 | WORD pressedCount[MAX_ADC_CHANNELS]; //Storage for count of pressed value for each channel |
||
| 57 | WORD unpressedCount[MAX_ADC_CHANNELS]; //Storage for count of unpressed value for each channel |
||
| 58 | WORD avg_delay[MAX_ADC_CHANNELS]; //Storage for count for average update for each channel |
||
| 59 | WORD averageData[MAX_ADC_CHANNELS]; // running average of CTMU channels |
||
| 60 | |||
| 61 | #pragma udata udata_mTouchCap_PIC18_CTMU_Physical_section2 |
||
| 62 | WORD smallAvg[MAX_ADC_CHANNELS]; // current button smallavg |
||
| 63 | WORD actualValue[MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
| 64 | WORD channel_TRIM_value[MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
| 65 | WORD channel_IRNG_value[MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
| 66 | BYTE chFilterType[MAX_ADC_CHANNELS]; // Channel filter type |
||
| 67 | BYTE Press_State[MAX_ADC_CHANNELS]; |
||
| 68 | BYTE Channel_Pressed_Status[MAX_ADC_CHANNELS]; |
||
| 69 | #pragma udata |
||
| 70 | |||
| 71 | WORD currentADCValue; // current button value |
||
| 72 | WORD scaledBigValue; // current button bigval |
||
| 73 | SHORT startupCount; // variable to 'discard' first N samples |
||
| 74 | SHORT loopCount; |
||
| 75 | |||
| 76 | /* CHANGE_MCHP created a new channelIndex array for different |
||
| 77 | analog channels */ |
||
| 78 | |||
| 79 | extern CHAR trimbitsReady; |
||
| 80 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 81 | /* ~~~~~~~~~~~~~~~~~~~~~ Function Prototypes ~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 82 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 87 | /* ~~~~~~~~~~~~~~~~~~~~~ Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 88 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 95 | /* ~~~~~~~~~~~~~~~~~~~~~ Function Definitions ~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 96 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
| 97 | |||
| 98 | |||
| 99 | /******************************************************************** |
||
| 100 | * Function : void mTouchCapPhy_StablizeChannelData(void) |
||
| 101 | * |
||
| 102 | * PreCondition : None |
||
| 103 | * |
||
| 104 | * Input : None |
||
| 105 | * |
||
| 106 | * Output : None |
||
| 107 | * |
||
| 108 | * Side Effects : None |
||
| 109 | * |
||
| 110 | * Overview : This function will ignore the first few samples of data before |
||
| 111 | * processing the data for determining the press and unpressed data. |
||
| 112 | * |
||
| 113 | * Note : Stablizes the CTMU channel data for the time defined by User. |
||
| 114 | * Change the count "INITIAL_STARTUP_COUNT" in mTouch_config.h file. |
||
| 115 | * |
||
| 116 | *******************************************************************/ |
||
| 117 | void mTouchCapPhy_StablizeChannelData(void) |
||
| 118 | { |
||
| 119 | BYTE Index; |
||
| 120 | startupCount = INITIAL_STARTUP_COUNT; |
||
| 121 | |||
| 122 | while (startupCount > 0) |
||
| 123 | { |
||
| 124 | |||
| 125 | while(!dataReadyCTMU); |
||
| 126 | |||
| 127 | dataReadyCTMU = 0; //clear flag |
||
| 128 | |||
| 129 | Set_ScanTimer_IF_Bit_State(DISABLE); //Clear timer 4 SHORT flag |
||
| 130 | Set_ScanTimer_IE_Bit_State(DISABLE); //Disable interrupt |
||
| 131 | Set_ScanTimer_ON_Bit_State(DISABLE); //Stop timer 4 |
||
| 132 | |||
| 133 | |||
| 134 | startupCount--; // Decr. N # times to establish startup |
||
| 135 | |||
| 136 | |||
| 137 | for(Index=0; Index<ScanChannelIndex; Index++) |
||
| 138 | { |
||
| 139 | averageData[ScanChannels[Index]] = curRawData[ScanChannels[Index]]; // During start up time, set Average each pass. |
||
| 140 | } |
||
| 141 | |||
| 142 | |||
| 143 | Set_ScanTimer_IF_Bit_State(DISABLE); //Clear flag |
||
| 144 | Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt |
||
| 145 | Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer |
||
| 146 | } |
||
| 147 | |||
| 148 | |||
| 149 | } |
||
| 150 | |||
| 151 | /********* End of Function *********************/ |
||
| 152 | |||
| 153 | |||
| 154 | /******************************************************************** |
||
| 155 | * Function : void mTouchCapPhy_CTMUSetup(void) |
||
| 156 | * |
||
| 157 | * PreCondition : None |
||
| 158 | * |
||
| 159 | * Input : None |
||
| 160 | * |
||
| 161 | * Output : None |
||
| 162 | * |
||
| 163 | * Side Effects : None |
||
| 164 | * |
||
| 165 | * Overview : This function will setup the CTMU control registers. |
||
| 166 | * |
||
| 167 | * |
||
| 168 | * Note : Does the necessary CTMU setup. |
||
| 169 | *******************************************************************/ |
||
| 170 | |||
| 171 | void mTouchCapPhy_CTMUSetup(void) |
||
| 172 | { |
||
| 173 | |||
| 174 | |||
| 175 | Set_CTMU_ENABLE_Status(DISABLE); //make sure CTMU is disabled |
||
| 176 | Idle_Mode_CTMU_Operation(DISABLE); //CTMU continues to run in idle mode |
||
| 177 | Set_CTMU_Time_Generation(DISABLE); //disable edge delay generation mode of the CTMU |
||
| 178 | Block_CTMU_Edge(DISABLE); //edges are blocked |
||
| 179 | Set_Edge_Sequence(DISABLE); //edge sequence not needed |
||
| 180 | Disable_CTMU_CurrentDischarge; //Do not ground the current source |
||
| 181 | Set_CTMU_Trigger_Control(DISABLE); //Trigger Output is disabled |
||
| 182 | Enable_CTMU_Negative_Edge2_Polarity; |
||
| 183 | CTMU_Edge2_Source_Select(TRIGGER_SOURCE_EXTERNAL1); |
||
| 184 | Enable_CTMU_Negative_Edge1_Polarity; |
||
| 185 | CTMU_Edge1_Source_Select(TRIGGER_SOURCE_EXTERNAL1); |
||
| 186 | |||
| 187 | |||
| 188 | /*************************************************************************** |
||
| 189 | --------------- Demo of API "mTouchCapAPI_CTMU_SetupCurrentSource" ----------------------- |
||
| 190 | Setup the current source using the API "mTouchCapAPI_SetUpCTMU_Default". Pass the current source |
||
| 191 | range and Trim value as parameters. |
||
| 192 | ***************************************************************************/ |
||
| 193 | |||
| 194 | mTouchCapAPI_CTMU_SetupCurrentSource(CURRENT_RANGE_BASE_CURRENT,0x00); |
||
| 195 | |||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | |||
| 201 | /********* End of Function *********************/ |
||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | /******************************************************************** |
||
| 206 | * Function : void mTouchCapPhy_ChannelSetup(WORD ChannelNum) |
||
| 207 | * |
||
| 208 | * PreCondition : None |
||
| 209 | * |
||
| 210 | * Input : ChannelNum |
||
| 211 | CHANNEL_AN0 , |
||
| 212 | CHANNEL_AN1 , |
||
| 213 | CHANNEL_AN2 , |
||
| 214 | CHANNEL_AN3 , |
||
| 215 | CHANNEL_AN4 , |
||
| 216 | CHANNEL_AN5 , |
||
| 217 | CHANNEL_AN6 , |
||
| 218 | CHANNEL_AN7 , |
||
| 219 | CHANNEL_AN8 , |
||
| 220 | CHANNEL_AN9 , |
||
| 221 | CHANNEL_AN10 , |
||
| 222 | CHANNEL_AN11 , |
||
| 223 | CHANNEL_AN12 , |
||
| 224 | CHANNEL_AN13 , // for PIC24. Not available in PIC18 |
||
| 225 | CHANNEL_AN14 , // for PIC24. Not available in PIC18 |
||
| 226 | CHANNEL_AN15 // for PIC24. Not available in PIC18 |
||
| 227 | * |
||
| 228 | * Output : None |
||
| 229 | * |
||
| 230 | * Side Effects : None |
||
| 231 | * |
||
| 232 | * Overview : This function will setup the ADC channels that are used by the CTMU |
||
| 233 | * Eval Board. |
||
| 234 | * |
||
| 235 | * |
||
| 236 | * Note : Does the necessary CTMU port setup. |
||
| 237 | *******************************************************************/ |
||
| 238 | void mTouchCapPhy_ChannelSetup(WORD ChannelNum) |
||
| 239 | { |
||
| 240 | WORD chanvar; |
||
| 241 | |||
| 242 | #if defined(__18CXX) |
||
| 243 | // Make sure that only selected channel is made input while the other port pins remain configured as output. |
||
| 244 | //The following If-else needed becasue on PIC18F46J50, the CTMU channels are on PortA, PORTB, PORTC and PORTE |
||
| 245 | |||
| 246 | // First make all the analog pins as Digital |
||
| 247 | Adc_Port_Configuration1 (MAKE_ALL_DIGITAL_PINS); |
||
| 248 | Adc_Port_Configuration2 (MAKE_ALL_DIGITAL_PINS); |
||
| 249 | |||
| 250 | |||
| 251 | // configure the port pins of the respective channels as input and as Analog |
||
| 252 | // Update the current channel number which has to be processed by the ADC |
||
| 253 | // configurations for the Channel 0 |
||
| 254 | switch (ChannelNum) |
||
| 255 | { |
||
| 256 | |||
| 257 | case CHANNEL_AN0: |
||
| 258 | TRIS_CHANNEL_AN0 = INPUT; //make the channel 0 as input based on the Index |
||
| 259 | ADPCF_CHANNEL_AN0(ANALOG); |
||
| 260 | |||
| 261 | break; |
||
| 262 | |||
| 263 | case CHANNEL_AN1: |
||
| 264 | TRIS_CHANNEL_AN1 = INPUT; //make the channel 1 as input based on the Index |
||
| 265 | ADPCF_CHANNEL_AN1(ANALOG); |
||
| 266 | break; |
||
| 267 | |||
| 268 | case CHANNEL_AN2: |
||
| 269 | TRIS_CHANNEL_AN2 = INPUT; //make the channel 2 as input based on the Index |
||
| 270 | ADPCF_CHANNEL_AN2(ANALOG); |
||
| 271 | break; |
||
| 272 | |||
| 273 | case CHANNEL_AN3: |
||
| 274 | TRIS_CHANNEL_AN3 = INPUT; //make the channel 3 as input based on the Index |
||
| 275 | ADPCF_CHANNEL_AN3(ANALOG); |
||
| 276 | break; |
||
| 277 | |||
| 278 | case CHANNEL_AN4: |
||
| 279 | TRIS_CHANNEL_AN4 = INPUT; //make the channel 4 as input based on the Index |
||
| 280 | ADPCF_CHANNEL_AN4(ANALOG); |
||
| 281 | break; |
||
| 282 | |||
| 283 | case CHANNEL_AN5: |
||
| 284 | TRIS_CHANNEL_AN5 = INPUT; //make the channel 5 as input based on the Index |
||
| 285 | ADPCF_CHANNEL_AN5(ANALOG); |
||
| 286 | break; |
||
| 287 | |||
| 288 | case CHANNEL_AN6: |
||
| 289 | TRIS_CHANNEL_AN6 = INPUT; //make the channel 6 as input based on the Index |
||
| 290 | ADPCF_CHANNEL_AN6(ANALOG); |
||
| 291 | break; |
||
| 292 | |||
| 293 | case CHANNEL_AN7: |
||
| 294 | TRIS_CHANNEL_AN7 = INPUT; //make the channel 7 as input based on the Index |
||
| 295 | ADPCF_CHANNEL_AN7(ANALOG); |
||
| 296 | break; |
||
| 297 | |||
| 298 | case CHANNEL_AN8: |
||
| 299 | TRIS_CHANNEL_AN8 = INPUT; //make the channel 8 as input based on the Index |
||
| 300 | ADPCF_CHANNEL_AN8(ANALOG); |
||
| 301 | break; |
||
| 302 | |||
| 303 | case CHANNEL_AN9: |
||
| 304 | TRIS_CHANNEL_AN9 = INPUT; //make the channel 9 as input based on the Index |
||
| 305 | ADPCF_CHANNEL_AN9(ANALOG); |
||
| 306 | break; |
||
| 307 | |||
| 308 | case CHANNEL_AN10: |
||
| 309 | TRIS_CHANNEL_AN1O = INPUT; //make the channel 10 as input based on the Index |
||
| 310 | ADPCF_CHANNEL_AN10(ANALOG); |
||
| 311 | break; |
||
| 312 | |||
| 313 | case CHANNEL_AN11: |
||
| 314 | TRIS_CHANNEL_AN11 = INPUT; //make the channel 11 as input based on the Index |
||
| 315 | ADPCF_CHANNEL_AN11(ANALOG); |
||
| 316 | break; |
||
| 317 | |||
| 318 | case CHANNEL_AN12: |
||
| 319 | TRIS_CHANNEL_AN12 = INPUT; //make the channel 12 as input based on the Index |
||
| 320 | ADPCF_CHANNEL_AN12(ANALOG); |
||
| 321 | break; |
||
| 322 | |||
| 323 | |||
| 324 | default: |
||
| 325 | TRIS_CHANNEL_AN0 = INPUT; //make the channel 0 as input based on the Index |
||
| 326 | ADPCF_CHANNEL_AN0(ANALOG); |
||
| 327 | break; |
||
| 328 | |||
| 329 | } |
||
| 330 | |||
| 331 | /* Connect the selected channel to ADC MUX */ |
||
| 332 | //load the channel number that has to be read |
||
| 333 | mTouchCapADC_SetChannelADC(ChannelNum); |
||
| 334 | |||
| 335 | } |
||
| 336 | |||
| 337 | /********* End of Function *********************/ |
||
| 338 | |||
| 339 | |||
| 340 | |||
| 341 | /******************************************************************** |
||
| 342 | * Function : void mTouchCapPhy_AdcSetup(void) |
||
| 343 | * |
||
| 344 | * PreCondition : None |
||
| 345 | * |
||
| 346 | * Input : None |
||
| 347 | * |
||
| 348 | * Output : None |
||
| 349 | * |
||
| 350 | * Side Effects : None |
||
| 351 | * |
||
| 352 | * Overview : This function will setup the ADC module |
||
| 353 | * |
||
| 354 | * |
||
| 355 | * Note : Does the necessary ADC peripheral setup. |
||
| 356 | *******************************************************************/ |
||
| 357 | void mTouchCapPhy_AdcSetup(void) |
||
| 358 | { |
||
| 359 | mTouchCapADC_SetChannelADC (0x00); |
||
| 360 | mTouchCapADC_OpenADC(); |
||
| 361 | |||
| 362 | } |
||
| 363 | /********* End of Function *********************/ |
||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | /******************************************************************** |
||
| 368 | * Function : void mTouchCapPhy_SetupCurrentSourceRange(WORD CurrentSourceRange); |
||
| 369 | * |
||
| 370 | * PreCondition : None |
||
| 371 | * |
||
| 372 | * Input : |
||
| 373 | * CURRENT_RANGE_100XBASE_CURRENT //Current source Range is 100*Base current (55uA) |
||
| 374 | * CURRENT_RANGE_10XBASE_CURRENT //Current source Range is 10*Base current (5.5uA) |
||
| 375 | * CURRENT_RANGE_BASE_CURRENT //Current source Range is Base current (0.55uA) |
||
| 376 | * CURRENT_SRC_DISABLED //Current source disabled |
||
| 377 | * |
||
| 378 | * Output : None |
||
| 379 | * |
||
| 380 | * Side Effects : None |
||
| 381 | * |
||
| 382 | * Overview : This function will select the required Analog Current Source Range. |
||
| 383 | * |
||
| 384 | * |
||
| 385 | * |
||
| 386 | * Note : Sets up the CTMU current source. |
||
| 387 | *******************************************************************/ |
||
| 388 | void mTouchCapPhy_SetupCurrentSourceRange(WORD CurrentSourceRange) |
||
| 389 | { |
||
| 390 | CTMU_Select_Current_Source(CurrentSourceRange); |
||
| 391 | } |
||
| 392 | |||
| 393 | /********* End of Function *********************/ |
||
| 394 | |||
| 395 | |||
| 396 | /******************************************************************** |
||
| 397 | * Function : void mTouchCapPhy_SetTrimValue( WORD TrimValue); |
||
| 398 | * PreCondition : None |
||
| 399 | * |
||
| 400 | * Input : TrimValue : CTMU TRIM bits Settings |
||
| 401 | * |
||
| 402 | * Output : None |
||
| 403 | * |
||
| 404 | * Side Effects : None |
||
| 405 | * |
||
| 406 | * Overview : This function will setup the trim values for the selected current source range |
||
| 407 | * |
||
| 408 | * |
||
| 409 | * Note : Set the proper Trim value of the current source as passed in the argument. |
||
| 410 | *******************************************************************/ |
||
| 411 | |||
| 412 | void mTouchCapPhy_SetTrimValue( WORD TrimValue) |
||
| 413 | { |
||
| 414 | |||
| 415 | CTMU_Trim_Value (TrimValue); |
||
| 416 | |||
| 417 | |||
| 418 | } |
||
| 419 | |||
| 420 | |||
| 421 | |||
| 422 | /******************************************************************** |
||
| 423 | * Function : void mTouchCapPhy_Discharge_Current (void) |
||
| 424 | * PreCondition : None |
||
| 425 | * |
||
| 426 | * Input : None |
||
| 427 | * |
||
| 428 | * Output : None |
||
| 429 | * |
||
| 430 | * Side Effects : None |
||
| 431 | * |
||
| 432 | * Overview : Discharges the current source. |
||
| 433 | * |
||
| 434 | * |
||
| 435 | * Note : |
||
| 436 | *******************************************************************/ |
||
| 437 | |||
| 438 | void mTouchCapPhy_Discharge_Current (void) |
||
| 439 | { |
||
| 440 | Enable_CTMU_CurrentDischarge; |
||
| 441 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
| 442 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
| 443 | Nop(); // Delay for CTMU charge time |
||
| 444 | Disable_CTMU_CurrentDischarge; |
||
| 445 | |||
| 446 | } |
||
| 447 | |||
| 448 | /********* End of Function *********************/ |
||
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | /******************************************************************** |
||
| 453 | * Function : void mTouchCapPhy_Charge_Current (void) |
||
| 454 | * PreCondition : None |
||
| 455 | * |
||
| 456 | * Input : None |
||
| 457 | * |
||
| 458 | * Output : None |
||
| 459 | * |
||
| 460 | * Side Effects : None |
||
| 461 | * |
||
| 462 | * Overview : Charges the cusrrent for the time as per the value given to "CTMU_CHARGE_TIME_COUNT" |
||
| 463 | * in mTouchCap_Config.h file. |
||
| 464 | * |
||
| 465 | * |
||
| 466 | * Note : |
||
| 467 | *******************************************************************/ |
||
| 468 | |||
| 469 | void mTouchCapPhy_Charge_Current (void) |
||
| 470 | { |
||
| 471 | WORD loopIndex; |
||
| 472 | |||
| 473 | #ifdef CTMU_GND_ON |
||
| 474 | Enable_CTMU_CurrentDischarge; //CTMUCONHbits.IDISSEN = 1; // Drain any charge on the circuit |
||
| 475 | Nop(); |
||
| 476 | Nop(); |
||
| 477 | Nop(); |
||
| 478 | Nop(); |
||
| 479 | Nop(); |
||
| 480 | Nop(); |
||
| 481 | Nop(); |
||
| 482 | Nop(); |
||
| 483 | Nop(); |
||
| 484 | Nop(); |
||
| 485 | Nop(); // Delay for CTMU charge time |
||
| 486 | Disable_CTMU_CurrentDischarge;// CTMUCONHbits.IDISSEN = 0; |
||
| 487 | #endif |
||
| 488 | |||
| 489 | ADC1_Clear_Intr_Status_Bit; |
||
| 490 | |||
| 491 | Disbl_CTMUEdge2; |
||
| 492 | Enable_CTMUEdge1; |
||
| 493 | |||
| 494 | |||
| 495 | /* When we require to enaable EDGE1 or EDGE2, should we pass as a param*/ |
||
| 496 | for (loopIndex = 0; loopIndex < loopCount; loopIndex++) |
||
| 497 | { |
||
| 498 | Nop(); // Delay for CTMU charge time // PIC18 - 4 TCY to execute Nop(); //PIC24 - 2 TCY to execute Nop(); |
||
| 499 | } |
||
| 500 | |||
| 501 | Disbl_CTMUEdge1; |
||
| 502 | |||
| 503 | } |
||
| 504 | |||
| 505 | |||
| 506 | |||
| 507 | |||
| 508 | /********************************************************************* |
||
| 509 | * Function : WORD mTouchCapPhy_Read_ADC(void) |
||
| 510 | * |
||
| 511 | * PreCondition : None |
||
| 512 | * |
||
| 513 | * Input : None |
||
| 514 | * |
||
| 515 | * Output : Resultant ADC Buffer value |
||
| 516 | * |
||
| 517 | * Side Effects : None |
||
| 518 | * |
||
| 519 | * Overview : Reads the ADC data of CTMU Channel. |
||
| 520 | * |
||
| 521 | * |
||
| 522 | * Note : |
||
| 523 | *******************************************************************/ |
||
| 524 | |||
| 525 | WORD mTouchCapPhy_Read_ADC(void) |
||
| 526 | { |
||
| 527 | |||
| 528 | WORD Channel_Data; |
||
| 529 | |||
| 530 | ADC1_Clear_Intr_Status_Bit; |
||
| 531 | Enable_ADC_Sampling; //ADCON0bits.GO = 1; // Manually start the conversion |
||
| 532 | // Wait for the A/D conversion to finish |
||
| 533 | |||
| 534 | while(ADC_GO_Bit ); |
||
| 535 | |||
| 536 | Channel_Data = mTouchCapADC_ReadADCBuff(0); // Read the value from the A/D conversion |
||
| 537 | |||
| 538 | ADC1_Clear_Intr_Status_Bit; // Disable the interrupt flag |
||
| 539 | |||
| 540 | /* |
||
| 541 | Disable_ADC_Sampling; |
||
| 542 | ADC1_Clear_Intr_Status_Bit; |
||
| 543 | ADC_Done_Bit = 0; */ |
||
| 544 | |||
| 545 | |||
| 546 | #ifdef GROUND_TEST |
||
| 547 | |||
| 548 | // A/D mux must connect to channel for CTMU to drain charge |
||
| 549 | |||
| 550 | Enable_CTMU_CurrentDischarge;//CTMUCONHbits.IDISSEN = 1; // Drain any charge on the circuit |
||
| 551 | Nop(); |
||
| 552 | Nop(); |
||
| 553 | Nop(); |
||
| 554 | Nop(); |
||
| 555 | Nop(); |
||
| 556 | Nop(); |
||
| 557 | Nop(); |
||
| 558 | Nop(); |
||
| 559 | Nop(); |
||
| 560 | Nop(); |
||
| 561 | Nop(); |
||
| 562 | |||
| 563 | Disable_CTMU_CurrentDischarge;//CTMUCONHbits.IDISSEN = 0; |
||
| 564 | Nop(); |
||
| 565 | Nop(); |
||
| 566 | Nop(); |
||
| 567 | Nop(); |
||
| 568 | Nop(); |
||
| 569 | Nop(); |
||
| 570 | Nop(); |
||
| 571 | Nop(); |
||
| 572 | |||
| 573 | #endif |
||
| 574 | return Channel_Data; |
||
| 575 | |||
| 576 | |||
| 577 | } |
||
| 578 | |||
| 579 | |||
| 580 | /********* End of Function *********************/ |
||
| 581 | |||
| 582 | |||
| 583 | /******************************************************************** |
||
| 584 | * Function : void mTouchCapPhy_InitCTMU(void) |
||
| 585 | * |
||
| 586 | * PreCondition : None |
||
| 587 | * |
||
| 588 | * Input : None |
||
| 589 | * |
||
| 590 | * Output : None |
||
| 591 | * |
||
| 592 | * Side Effects : None |
||
| 593 | * |
||
| 594 | * Overview : Does the initialization of all peripherals/ports necessary for CTMU operation. |
||
| 595 | * |
||
| 596 | * |
||
| 597 | * Note : |
||
| 598 | *******************************************************************/ |
||
| 599 | /*.................................................................... |
||
| 600 | . Init() |
||
| 601 | . |
||
| 602 | . Initialization Routine to set up part's variables and |
||
| 603 | . peripherals. |
||
| 604 | ....................................................................*/ |
||
| 605 | void mTouchCapPhy_InitCTMU(void) |
||
| 606 | { |
||
| 607 | /* Initialize PORTs */ |
||
| 608 | mTouchCapApp_PortSetup(); |
||
| 609 | |||
| 610 | /* Initialize CTMU */ |
||
| 611 | mTouchCapPhy_CTMUSetup(); |
||
| 612 | |||
| 613 | /* Initialize ADC */ |
||
| 614 | mTouchCapPhy_AdcSetup(); |
||
| 615 | |||
| 616 | Set_Adc_Enable_State(ENABLE); |
||
| 617 | |||
| 618 | Enable_ADC_Sampling; |
||
| 619 | |||
| 620 | Set_CTMU_ENABLE_Status(ENABLE); |
||
| 621 | |||
| 622 | Enable_CTMU_CurrentDischarge; // Drain any charge on the circuit |
||
| 623 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
| 624 | |||
| 625 | Disable_CTMU_CurrentDischarge; |
||
| 626 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
| 627 | // Initialize Capacitive Sensing |
||
| 628 | // InitAvgDelay(); // Zero out the slow averaging function |
||
| 629 | |||
| 630 | } |
||
| 631 | |||
| 632 | |||
| 633 | |||
| 634 | /******************************************************************** |
||
| 635 | * Function : void mTouchCapPhy_ChargeTimeInit(void) |
||
| 636 | * |
||
| 637 | * PreCondition : None |
||
| 638 | * |
||
| 639 | * Input : None |
||
| 640 | * |
||
| 641 | * Output : None |
||
| 642 | * |
||
| 643 | * Side Effects : None |
||
| 644 | * |
||
| 645 | * Overview : Initializes the charge time. User can change this by modifying the value given for "CTMU_CHARGE_TIME_COUNT" |
||
| 646 | * in Config.h file. |
||
| 647 | * |
||
| 648 | * |
||
| 649 | * Note : |
||
| 650 | *******************************************************************/ |
||
| 651 | void mTouchCapPhy_ChargeTimeInit(void) |
||
| 652 | { |
||
| 653 | loopCount = CTMU_CHARGE_TIME_COUNT; //Loop counter - determines charge time for each |
||
| 654 | } |
||
| 655 | |||
| 656 | |||
| 657 | |||
| 658 | /******************************************************************** |
||
| 659 | * Function : void mTouchCapPhy_ReadCTMU(WORD ChannelNumber) |
||
| 660 | * |
||
| 661 | * PreCondition : None |
||
| 662 | * |
||
| 663 | * Input : Channel Number to be scanned. |
||
| 664 | CHANNEL_AN0 , |
||
| 665 | CHANNEL_AN1 , |
||
| 666 | CHANNEL_AN2 , |
||
| 667 | CHANNEL_AN3 , |
||
| 668 | CHANNEL_AN4 , |
||
| 669 | CHANNEL_AN5 , |
||
| 670 | CHANNEL_AN6 , |
||
| 671 | CHANNEL_AN7 , |
||
| 672 | CHANNEL_AN8 , |
||
| 673 | CHANNEL_AN9 , |
||
| 674 | CHANNEL_AN10 , |
||
| 675 | CHANNEL_AN11 , |
||
| 676 | CHANNEL_AN12 , |
||
| 677 | * |
||
| 678 | * Output : None |
||
| 679 | * |
||
| 680 | * Side Effects : None |
||
| 681 | * |
||
| 682 | * Overview : Scans the CTMU channel for ADC voltage. It updates the "curRawData" and "actualValue" buffers. |
||
| 683 | * |
||
| 684 | * |
||
| 685 | * Note : |
||
| 686 | *******************************************************************/ |
||
| 687 | void mTouchCapPhy_ReadCTMU(WORD ChannelNumber) |
||
| 688 | { |
||
| 689 | |||
| 690 | /*************************************************************************** |
||
| 691 | --------------- Demo of API "mTouchCapAPI_ScanChannelIterative" ----------------------- |
||
| 692 | |||
| 693 | Read the channel Rawdata in an iterative fashion for the mentioned sample count using the |
||
| 694 | API "mTouchCapAPI_ScanChannelIterative". |
||
| 695 | ***************************************************************************/ |
||
| 696 | currentADCValue = mTouchCapAPI_ScanChannelIterative(ChannelNumber,NUM_HF_READS); |
||
| 697 | |||
| 698 | |||
| 699 | //For debug - channels 6&7 are not enabled. They are clk & data lines for ICSP. use dummy values |
||
| 700 | #ifdef DEBUG |
||
| 701 | if(ScanChannels[ChannelNumber] == CHANNEL_AN6 || ScanChannels[ChannelNumber] == CHANNEL_AN7) |
||
| 702 | { |
||
| 703 | currentADCValue = 0x240; |
||
| 704 | } |
||
| 705 | // ... |
||
| 706 | #endif |
||
| 707 | |||
| 708 | #ifdef GUI_USB_INTERFACE |
||
| 709 | scaledBigValue = currentADCValue * 64; // BIGVAL is current measurement left shifted 4 bits for GUI display |
||
| 710 | #else |
||
| 711 | scaledBigValue = currentADCValue; // Copy the ADC value of the channel |
||
| 712 | #endif |
||
| 713 | |||
| 714 | curRawData[ChannelNumber] = scaledBigValue; // curRawData array holds the most recent BIGVAL values |
||
| 715 | actualValue[ChannelNumber] = currentADCValue; |
||
| 716 | |||
| 717 | } //end ReadCTMU() |
||
| 718 | |||
| 719 | |||
| 720 | /****************************************************************************** |
||
| 721 | * Function: void CTMU_Current_trim_config(int channel_no) |
||
| 722 | * |
||
| 723 | * PreCondition: None |
||
| 724 | * |
||
| 725 | * Input: current Channel number that is being processed by ADC |
||
| 726 | * |
||
| 727 | * Output: None |
||
| 728 | * |
||
| 729 | * Side Effects: None |
||
| 730 | * |
||
| 731 | * Overview: This function necessary to maintain a constant default voltage levels in all the channels. |
||
| 732 | * |
||
| 733 | * Note: None |
||
| 734 | *****************************************************************************/ |
||
| 735 | void CTMU_Current_trim_config(int channel_no) |
||
| 736 | { |
||
| 737 | |||
| 738 | if(trimbitsReady) |
||
| 739 | { |
||
| 740 | mTouchCapAPI_CTMU_SetupCurrentSource(channel_IRNG_value[channel_no],channel_TRIM_value[channel_no]); |
||
| 741 | } |
||
| 742 | |||
| 743 | } //end of void CTMU_Current_trim_config(int channel_no) |
||
| 744 | |||
| 745 | |||
| 746 | |||
| 747 | /******************************************************************** |
||
| 748 | * Function : void mTouchCapPhy_AverageData(WORD Index) |
||
| 749 | * |
||
| 750 | * PreCondition : None |
||
| 751 | * |
||
| 752 | * Input : Channel Number |
||
| 753 | * |
||
| 754 | * Output : None |
||
| 755 | * |
||
| 756 | * Side Effects : None |
||
| 757 | * |
||
| 758 | * Overview : This function is used to average the Current Raw ADC value and the |
||
| 759 | * Average ADC value based on the type of the decode method that is |
||
| 760 | * selected by the application. |
||
| 761 | * |
||
| 762 | * |
||
| 763 | * |
||
| 764 | * Note : |
||
| 765 | *******************************************************************/ |
||
| 766 | |||
| 767 | void mTouchCapPhy_AverageData(WORD Index) |
||
| 768 | { |
||
| 769 | |||
| 770 | BYTE Filter_type; |
||
| 771 | |||
| 772 | /* Filtering based on channel specefic filter type */ |
||
| 773 | Filter_type = chFilterType[Index]; |
||
| 774 | |||
| 775 | /* Check the opted filter type of the channel. Copy if within limits. Else, the default type would be "1BY20" method. */ |
||
| 776 | if ( (Filter_type > FILTER_METHOD_FASTAVERAGE) ) |
||
| 777 | { |
||
| 778 | Filter_type = FILTER_METHOD_SLOWAVERAGE; |
||
| 779 | } |
||
| 780 | |||
| 781 | /* Process the average based on filter type selected by user in APi-mTouchCapAPI_SetUpChannel*/ |
||
| 782 | switch(Filter_type) |
||
| 783 | { |
||
| 784 | case FILTER_METHOD_SLOWAVERAGE: |
||
| 785 | // 7. Average in the new value (channel based delay) |
||
| 786 | // Always Average (all buttons one after the other in a loop) |
||
| 787 | |||
| 788 | if (curRawData[Index] > averageData[Index]) |
||
| 789 | { |
||
| 790 | averageData[Index] = curRawData[Index]; // If curRawData is above Average, reset to high average. |
||
| 791 | } |
||
| 792 | |||
| 793 | if(avg_delay[Index] < NUM_AVG) |
||
| 794 | { |
||
| 795 | avg_delay[Index]++; // Counting 0..8 has effect of every 9th count cycling the next button. |
||
| 796 | } |
||
| 797 | else |
||
| 798 | { |
||
| 799 | avg_delay[Index] = 0; // Counting 0..4 will average faster and also can use 0..4*m, m=0,1,2,3.. |
||
| 800 | } |
||
| 801 | |||
| 802 | if(avg_delay[Index] == NUM_AVG) |
||
| 803 | { |
||
| 804 | smallAvg[Index] = averageData[Index] / NUM_AVG; // SMALLAVG is the current average right shifted 4 bits |
||
| 805 | // Average in raw value. |
||
| 806 | averageData[Index] = averageData[Index] + ((curRawData[Index] / NUM_AVG) - smallAvg[Index]); |
||
| 807 | } |
||
| 808 | break; |
||
| 809 | |||
| 810 | |||
| 811 | case FILTER_METHOD_GATEDAVERAGE: |
||
| 812 | //Filter Method CASE: Gated Average |
||
| 813 | // 7. Average in the new value (channel based delay) |
||
| 814 | // Always Average (all buttons one after the other in a loop) |
||
| 815 | |||
| 816 | if(avg_delay[Index] < NUM_AVG) |
||
| 817 | { |
||
| 818 | avg_delay[Index]++; // Counting 0..8 has effect of every 9th count cycling the next button. |
||
| 819 | } |
||
| 820 | else |
||
| 821 | { |
||
| 822 | avg_delay[Index] = 0; // Counting 0..4 will average faster and also can use 0..4*m, m=0,1,2,3.. |
||
| 823 | } |
||
| 824 | if(avg_delay[Index] == NUM_AVG) |
||
| 825 | { |
||
| 826 | /* Gated average only of no key pressed. */ |
||
| 827 | /* Stop averaging when press is being sensed. //STD:*/ |
||
| 828 | if ( KEY_NOT_PRESSED == mTouchCapAPI_getChannelTouchStatus(Index,DECODE_METHOD_PRESS_ASSERT )) |
||
| 829 | { |
||
| 830 | smallAvg[Index] = averageData[Index] / NUM_AVG; // SMALLAVG is the current average right shifted 4 bits |
||
| 831 | // Average in raw value. |
||
| 832 | averageData[Index] = averageData[Index] + ((curRawData[Index] / NUM_AVG) - smallAvg[Index]); |
||
| 833 | } |
||
| 834 | } |
||
| 835 | |||
| 836 | break; |
||
| 837 | |||
| 838 | case FILTER_METHOD_FASTAVERAGE: |
||
| 839 | |||
| 840 | /* The latest current raw data would be the latest average data */ |
||
| 841 | if (curRawData[Index] > averageData[Index]) |
||
| 842 | { |
||
| 843 | averageData[Index] = curRawData[Index]; // If curRawData is above Average, reset to high average. |
||
| 844 | } |
||
| 845 | break; |
||
| 846 | |||
| 847 | |||
| 848 | } |
||
| 849 | |||
| 850 | /* Dynamic trip value calculation . STD: */ |
||
| 851 | DynamicTripValueCalculation(Index); |
||
| 852 | |||
| 853 | } |
||
| 854 | |||
| 855 | |||
| 856 | |||
| 857 | /******************************************************************** |
||
| 858 | * Function : void DynamicTripValueCalculation(WORD Index) |
||
| 859 | * |
||
| 860 | * PreCondition : None |
||
| 861 | * |
||
| 862 | * Input : Index- Channel Number |
||
| 863 | * |
||
| 864 | * Output : None |
||
| 865 | * |
||
| 866 | * Side Effects : None |
||
| 867 | * |
||
| 868 | * Overview : calculate the dynamic trip value. |
||
| 869 | * |
||
| 870 | * |
||
| 871 | * Note : |
||
| 872 | *******************************************************************/ |
||
| 873 | void DynamicTripValueCalculation(WORD Index) |
||
| 874 | { |
||
| 875 | #ifndef USE_STATIC_TRIP_VALUE |
||
| 876 | |||
| 877 | tripValue[Index] = (averageData[Index] / KEYTRIPDIV); |
||
| 878 | hystValue[Index] = (tripValue[Index] / HYSTERESIS_VALUE); |
||
| 879 | #endif |
||
| 880 | } |
Powered by WebSVN v2.8.3