Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | |
2 | /***************************************************************************** |
||
3 | * FileName: mTouchCap_PIC24_CTMU_Physical.c |
||
4 | * Dependencies: |
||
5 | * Processor: PIC24 |
||
6 | * Compiler: C30 |
||
7 | * Linker: MPLAB LINK30 |
||
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 | * Sasha. M / Naveen. M 4 May 2009 Version 0.2 Updates |
||
37 | * Sasha. M / Naveen. M 11 Nov 2009 Version 1.0 Release |
||
38 | * Sasha. M / Nithin. 10 April 2010 Version 1.20 Release |
||
39 | *****************************************************************************/ |
||
40 | |||
41 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
42 | /* ~~~~~~~~~~~~~~~~~~~~~ Includes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
43 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
44 | |||
45 | #include "mTouchCap_PIC24_CTMU_Physical.h" |
||
46 | |||
47 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
48 | /* ~~~~~~~~~~~~~~~~~~~~~ Variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
49 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
50 | WORD curRawData [MAX_ADC_CHANNELS]; //Storage for CTMU channel values |
||
51 | WORD tripValue [MAX_ADC_CHANNELS]; //Storage for the trip point for each channel |
||
52 | WORD hystValue [MAX_ADC_CHANNELS]; //Storage for the hysterisis value for each channel |
||
53 | WORD pressedCount [MAX_ADC_CHANNELS]; //Storage for count of pressed value for each channel |
||
54 | WORD unpressedCount [MAX_ADC_CHANNELS]; //Storage for count of unpressed value for each channel |
||
55 | WORD avg_delay [MAX_ADC_CHANNELS];//Storage for count for average update for each channel |
||
56 | WORD averageData [MAX_ADC_CHANNELS]; // running average of CTMU channels |
||
57 | WORD smallAvg [MAX_ADC_CHANNELS]; // current button smallavg |
||
58 | WORD actualValue [MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
59 | WORD channel_TRIM_value [MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
60 | WORD channel_IRNG_value [MAX_ADC_CHANNELS]; // actual raw A/D counts for each channel |
||
61 | BYTE chFilterType [MAX_ADC_CHANNELS]; // Channel filter type |
||
62 | BYTE Press_State [MAX_ADC_CHANNELS]; |
||
63 | BYTE Channel_Pressed_Status[MAX_ADC_CHANNELS]; |
||
64 | extern CHAR trimbitsReady; |
||
65 | |||
66 | SHORT startupCount; // variable to 'discard' first N samples |
||
67 | WORD currentADCValue; // current button value |
||
68 | WORD scaledBigValue; // current button bigval |
||
69 | SHORT loopCount = CTMU_CHARGE_TIME_COUNT; |
||
70 | |||
71 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
72 | /* ~~~~~~~~~~~~~~~~~~~~~ Function Prototypes ~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
73 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
74 | |||
75 | |||
76 | |||
77 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
78 | /* ~~~~~~~~~~~~~~~~~~~~~ Declarations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
79 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
80 | |||
81 | |||
82 | |||
83 | |||
84 | |||
85 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
86 | /* ~~~~~~~~~~~~~~~~~~~~~ Function Definitions ~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
87 | /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ |
||
88 | |||
89 | |||
90 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
91 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
92 | // Helper Functions |
||
93 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
94 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
||
95 | |||
96 | |||
97 | /******************************************************************** |
||
98 | * Function : void mTouchCapPhy_StablizeChannelData(void) |
||
99 | * |
||
100 | * PreCondition : None |
||
101 | * |
||
102 | * Input : None |
||
103 | * |
||
104 | * Output : None |
||
105 | * |
||
106 | * Side Effects : None |
||
107 | * |
||
108 | * Overview : This function will ignore the first few samples of data before |
||
109 | * processing the data for determining the press and unpressed data. |
||
110 | * |
||
111 | * |
||
112 | * Note : Stablizes the CTMU channel data for the time defined by User. |
||
113 | * Change the count "INITIAL_STARTUP_COUNT" in mTouch_config.h file. |
||
114 | *******************************************************************/ |
||
115 | void mTouchCapPhy_StablizeChannelData(void) |
||
116 | { |
||
117 | WORD Index; |
||
118 | startupCount = INITIAL_STARTUP_COUNT; |
||
119 | |||
120 | while (startupCount > 0) |
||
121 | { |
||
122 | while(!dataReadyCTMU); |
||
123 | |||
124 | dataReadyCTMU = 0; //clear flag |
||
125 | |||
126 | Set_ScanTimer_IF_Bit_State(DISABLE); //Clear timer 4 SHORT flag |
||
127 | Set_ScanTimer_IE_Bit_State(DISABLE); //Disable interrupt |
||
128 | Set_ScanTimer_ON_Bit_State(DISABLE); //Stop timer 4 |
||
129 | |||
130 | |||
131 | startupCount--; // Decr. N # times to establish startup |
||
132 | |||
133 | |||
134 | for(Index=0; Index<ScanChannelIndex; Index++) |
||
135 | { |
||
136 | averageData[ScanChannels[Index]] = curRawData[ScanChannels[Index]]; // During start up time, set Average each pass. |
||
137 | } |
||
138 | |||
139 | |||
140 | Set_ScanTimer_IF_Bit_State(DISABLE); //Clear flag |
||
141 | Set_ScanTimer_IE_Bit_State(ENABLE); //Enable interrupt |
||
142 | Set_ScanTimer_ON_Bit_State(ENABLE); //Run timer |
||
143 | } |
||
144 | } |
||
145 | |||
146 | |||
147 | /******************************************************************** |
||
148 | * Function : void mTouchCapPhy_CTMUSetup(void) |
||
149 | * |
||
150 | * PreCondition : None |
||
151 | * |
||
152 | * Input : None |
||
153 | * |
||
154 | * Output : None |
||
155 | * |
||
156 | * Side Effects : None |
||
157 | * |
||
158 | * Overview : This function will setup the CTMU control registers. |
||
159 | * |
||
160 | * |
||
161 | * Note : Does the necessary CTMU setup. |
||
162 | *******************************************************************/ |
||
163 | |||
164 | void mTouchCapPhy_CTMUSetup(void) |
||
165 | { |
||
166 | |||
167 | |||
168 | Set_CTMU_ENABLE_Status(DISABLE); //make sure CTMU is disabled |
||
169 | Idle_Mode_CTMU_Operation(DISABLE); //CTMU continues to run in idle mode |
||
170 | Set_CTMU_Time_Generation(DISABLE); //disable edge delay generation mode of the CTMU |
||
171 | Block_CTMU_Edge(DISABLE); //edges are blocked |
||
172 | Set_Edge_Sequence(DISABLE); //edge sequence not needed |
||
173 | Disble_CTMU_CurrentDischarge; //Do not ground the current source |
||
174 | Set_CTMU_Trigger_Control(DISABLE); //Trigger Output is disabled |
||
175 | Enble_CTMU_Negative_Edge2_Polarity; |
||
176 | CTMU_Edge2_Source_Select(TRIGGER_SOURCE_EXTERNAL1); |
||
177 | Enble_CTMU_Negative_Edge1_Polarity; |
||
178 | CTMU_Edge1_Source_Select(TRIGGER_SOURCE_EXTERNAL1); |
||
179 | |||
180 | |||
181 | /*************************************************************************** |
||
182 | --------------- Demo of API "mTouchCapAPI_CTMU_SetupCurrentSource" ----------------------- |
||
183 | Setup the current source using the API "mTouchCapAPI_SetUpCTMU_Default". Pass the current source |
||
184 | range and Trim value as parameters. |
||
185 | ***************************************************************************/ |
||
186 | mTouchCapAPI_CTMU_SetupCurrentSource(CURRENT_RANGE_100XBASE_CURRENT,0x00); |
||
187 | |||
188 | } |
||
189 | |||
190 | /********* End of Function *********************/ |
||
191 | |||
192 | |||
193 | /******************************************************************** |
||
194 | * Function : void mTouchCapPhy_ChannelSetup(WORD ChannelNum) |
||
195 | * |
||
196 | * PreCondition : None |
||
197 | * |
||
198 | * Input : ChannelNum |
||
199 | CHANNEL_AN0 , |
||
200 | CHANNEL_AN1 , |
||
201 | CHANNEL_AN2 , |
||
202 | CHANNEL_AN3 , |
||
203 | CHANNEL_AN4 , |
||
204 | CHANNEL_AN5 , |
||
205 | CHANNEL_AN6 , |
||
206 | CHANNEL_AN7 , |
||
207 | CHANNEL_AN8 , |
||
208 | CHANNEL_AN9 , |
||
209 | CHANNEL_AN10 , |
||
210 | CHANNEL_AN11 , |
||
211 | CHANNEL_AN12 , |
||
212 | CHANNEL_AN13 , // for PIC24. Not available in PIC18 |
||
213 | CHANNEL_AN14 , // for PIC24. Not available in PIC18 |
||
214 | CHANNEL_AN15 // for PIC24. Not available in PIC18 |
||
215 | * |
||
216 | * Output : None |
||
217 | * |
||
218 | * Side Effects : None |
||
219 | * |
||
220 | * |
||
221 | * Overview : This function will setup the ADC channels that are used by the CTMU |
||
222 | * Eval Board. |
||
223 | * |
||
224 | * Note : Does the necessary CTMU port setup. |
||
225 | *******************************************************************/ |
||
226 | void mTouchCapPhy_ChannelSetup(WORD ChannelNum) |
||
227 | { |
||
228 | #ifdef ADC_LEGACY |
||
229 | Adc_Port_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
230 | #endif |
||
231 | |||
232 | #ifdef ADC_NEW |
||
233 | Adc_PortA_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
234 | Adc_PortB_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
235 | Adc_PortC_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
236 | Adc_PortE_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
237 | Adc_PortG_Configuration(MAKE_ALL_PINS_DIGITAL); // A/D mux must connect to channel for CTMU to drain charge |
||
238 | #endif |
||
239 | |||
240 | //all pins need not be output. Change as per application. |
||
241 | |||
242 | #ifdef ADC_LEGACY |
||
243 | |||
244 | TRISB = 0x0000; |
||
245 | LATB = 0x0000; |
||
246 | PORTB = 0x0000; |
||
247 | |||
248 | #endif |
||
249 | |||
250 | #ifdef ADC_NEW |
||
251 | |||
252 | TRISA = TRISA & !(0x00C0); |
||
253 | TRISB = TRISB & !(0xFFFF); |
||
254 | TRISC = TRISC & !(0x0010); |
||
255 | TRISE = TRISE & !(0x0200); |
||
256 | TRISG = TRISG & !(0x03C0); |
||
257 | |||
258 | LATA = LATA & !(0x00C0); |
||
259 | LATB = LATB & !(0xFFFF); |
||
260 | LATC = LATC & !(0x0010); |
||
261 | LATE = LATE & !(0x0200); |
||
262 | LATG = LATG & !(0x03C0); |
||
263 | |||
264 | PORTA = PORTA & !(0x00C0); |
||
265 | PORTB = PORTB & !(0xFFFF); |
||
266 | PORTC = PORTC & !(0x0010); |
||
267 | PORTE = PORTE & !(0x0200); |
||
268 | PORTG = PORTG & !(0x03C0); |
||
269 | |||
270 | #endif |
||
271 | Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); Nop(); |
||
272 | |||
273 | |||
274 | switch (ChannelNum) |
||
275 | { |
||
276 | |||
277 | case CHANNEL_AN0: |
||
278 | TRIS_CHANNEL_AN0 = INPUT; //make the channel 0 as input based on the Index |
||
279 | ADPCF_CHANNEL_AN0(ANALOG); |
||
280 | |||
281 | break; |
||
282 | |||
283 | case CHANNEL_AN1: |
||
284 | TRIS_CHANNEL_AN1 = INPUT; //make the channel 0 as input based on the Index |
||
285 | ADPCF_CHANNEL_AN1(ANALOG); |
||
286 | break; |
||
287 | |||
288 | case CHANNEL_AN2: |
||
289 | TRIS_CHANNEL_AN2 = INPUT; //make the channel 0 as input based on the Index |
||
290 | ADPCF_CHANNEL_AN2(ANALOG); |
||
291 | break; |
||
292 | |||
293 | case CHANNEL_AN3: |
||
294 | TRIS_CHANNEL_AN3 = INPUT; //make the channel 0 as input based on the Index |
||
295 | ADPCF_CHANNEL_AN3(ANALOG); |
||
296 | break; |
||
297 | |||
298 | case CHANNEL_AN4: |
||
299 | TRIS_CHANNEL_AN4 = INPUT; //make the channel 0 as input based on the Index |
||
300 | ADPCF_CHANNEL_AN4(ANALOG); |
||
301 | break; |
||
302 | |||
303 | case CHANNEL_AN5: |
||
304 | TRIS_CHANNEL_AN5 = INPUT; //make the channel 0 as input based on the Index |
||
305 | ADPCF_CHANNEL_AN5(ANALOG); |
||
306 | break; |
||
307 | |||
308 | case CHANNEL_AN6: |
||
309 | TRIS_CHANNEL_AN6 = INPUT; //make the channel 0 as input based on the Index |
||
310 | ADPCF_CHANNEL_AN6(ANALOG); |
||
311 | break; |
||
312 | |||
313 | case CHANNEL_AN7: |
||
314 | TRIS_CHANNEL_AN7 = INPUT; //make the channel 0 as input based on the Index |
||
315 | ADPCF_CHANNEL_AN7(ANALOG); |
||
316 | break; |
||
317 | |||
318 | case CHANNEL_AN8: |
||
319 | TRIS_CHANNEL_AN8 = INPUT; //make the channel 0 as input based on the Index |
||
320 | ADPCF_CHANNEL_AN8(ANALOG); |
||
321 | break; |
||
322 | |||
323 | case CHANNEL_AN9: |
||
324 | TRIS_CHANNEL_AN9 = INPUT; //make the channel 0 as input based on the Index |
||
325 | ADPCF_CHANNEL_AN9(ANALOG); |
||
326 | break; |
||
327 | |||
328 | case CHANNEL_AN10: |
||
329 | TRIS_CHANNEL_AN1O = INPUT; //make the channel 0 as input based on the Index |
||
330 | ADPCF_CHANNEL_AN10(ANALOG); |
||
331 | break; |
||
332 | |||
333 | case CHANNEL_AN11: |
||
334 | TRIS_CHANNEL_AN11 = INPUT; //make the channel 0 as input based on the Index |
||
335 | ADPCF_CHANNEL_AN11(ANALOG); |
||
336 | break; |
||
337 | |||
338 | case CHANNEL_AN12: |
||
339 | TRIS_CHANNEL_AN12 = INPUT; //make the channel 0 as input based on the Index |
||
340 | ADPCF_CHANNEL_AN12(ANALOG); |
||
341 | break; |
||
342 | |||
343 | case CHANNEL_AN13: |
||
344 | TRIS_CHANNEL_AN13= INPUT; //make the channel 0 as input based on the Index |
||
345 | ADPCF_CHANNEL_AN13(ANALOG); |
||
346 | break; |
||
347 | |||
348 | case CHANNEL_AN14: |
||
349 | TRIS_CHANNEL_AN14 = INPUT; //make the channel 0 as input based on the Index |
||
350 | ADPCF_CHANNEL_AN14(ANALOG); |
||
351 | break; |
||
352 | |||
353 | case CHANNEL_AN15: |
||
354 | TRIS_CHANNEL_AN15= INPUT; //make the channel 0 as input based on the Index |
||
355 | ADPCF_CHANNEL_AN15(ANALOG); |
||
356 | break; |
||
357 | |||
358 | #if defined(__PIC24FJ_DAXXX__) |
||
359 | |||
360 | case CHANNEL_AN16: |
||
361 | TRIS_CHANNEL_AN16 = INPUT; //make the channel 0 as input based on the Index |
||
362 | ADPCF_CHANNEL_AN16(ANALOG); |
||
363 | break; |
||
364 | |||
365 | case CHANNEL_AN17: |
||
366 | TRIS_CHANNEL_AN17 = INPUT; //make the channel 0 as input based on the Index |
||
367 | ADPCF_CHANNEL_AN17(ANALOG); |
||
368 | break; |
||
369 | |||
370 | case CHANNEL_AN18: |
||
371 | TRIS_CHANNEL_AN18 = INPUT; //make the channel 0 as input based on the Index |
||
372 | ADPCF_CHANNEL_AN18(ANALOG); |
||
373 | break; |
||
374 | |||
375 | case CHANNEL_AN19: |
||
376 | TRIS_CHANNEL_AN19 = INPUT; //make the channel 0 as input based on the Index |
||
377 | ADPCF_CHANNEL_AN19(ANALOG); |
||
378 | break; |
||
379 | |||
380 | case CHANNEL_AN20: |
||
381 | TRIS_CHANNEL_AN20 = INPUT; //make the channel 0 as input based on the Index |
||
382 | ADPCF_CHANNEL_AN20(ANALOG); |
||
383 | break; |
||
384 | |||
385 | case CHANNEL_AN21: |
||
386 | TRIS_CHANNEL_AN21= INPUT; //make the channel 0 as input based on the Index |
||
387 | ADPCF_CHANNEL_AN21(ANALOG); |
||
388 | break; |
||
389 | |||
390 | case CHANNEL_AN22: |
||
391 | TRIS_CHANNEL_AN22 = INPUT; //make the channel 0 as input based on the Index |
||
392 | ADPCF_CHANNEL_AN22(ANALOG); |
||
393 | break; |
||
394 | |||
395 | case CHANNEL_AN23: |
||
396 | TRIS_CHANNEL_AN23= INPUT; //make the channel 0 as input based on the Index |
||
397 | ADPCF_CHANNEL_AN23(ANALOG); |
||
398 | break; |
||
399 | |||
400 | |||
401 | |||
402 | #endif |
||
403 | default: |
||
404 | TRIS_CHANNEL_AN0 = INPUT; //make the channel 0 as input based on the Index |
||
405 | ADPCF_CHANNEL_AN0(ANALOG); |
||
406 | break; |
||
407 | |||
408 | } |
||
409 | |||
410 | /* Connect the selected channel to ADC MUX */ |
||
411 | //load the channel number that has to be read |
||
412 | mTouchCapADC_SetChannelADC(ChannelNum); |
||
413 | |||
414 | } |
||
415 | |||
416 | /********* End of Function *********************/ |
||
417 | |||
418 | /******************************************************************** |
||
419 | * Function : void mTouchCapPhy_AdcSetup(void) |
||
420 | * |
||
421 | * PreCondition : None |
||
422 | * |
||
423 | * Input : None |
||
424 | * |
||
425 | * Output : None |
||
426 | * |
||
427 | * Side Effects : None |
||
428 | * |
||
429 | * Overview : This function will setup the ADC module |
||
430 | * |
||
431 | * Note : Does the necessary ADC peripheral setup. |
||
432 | *******************************************************************/ |
||
433 | void mTouchCapPhy_AdcSetup(void) |
||
434 | { |
||
435 | mTouchCapADC_SetChannelADC (0x00); |
||
436 | mTouchCapADC_OpenADC(); |
||
437 | |||
438 | } |
||
439 | |||
440 | /********* End of Function *********************/ |
||
441 | |||
442 | /******************************************************************** |
||
443 | * Function : void mTouchCapPhy_SetupCurrentSourceRange(WORD CurrentSourceRange); |
||
444 | * |
||
445 | * PreCondition : None |
||
446 | * |
||
447 | * Input : Analog Current Source Range |
||
448 | * CURRENT_RANGE_100XBASE_CURRENT //Current source Range is 100*Base current (55uA) |
||
449 | * CURRENT_RANGE_10XBASE_CURRENT //Current source Range is 10*Base current (5.5uA) |
||
450 | * CURRENT_RANGE_BASE_CURRENT //Current source Range is Base current (0.55uA) |
||
451 | * CURRENT_SRC_DISABLED //Current source disabled |
||
452 | * |
||
453 | * Output : None |
||
454 | * |
||
455 | * Side Effects : None |
||
456 | * |
||
457 | * Overview : This function will select the required Analog Current Source Range. |
||
458 | * |
||
459 | * Note : Sets up the CTMU current source. |
||
460 | *******************************************************************/ |
||
461 | void mTouchCapPhy_SetupCurrentSourceRange(WORD CurrentSourceRange) |
||
462 | { |
||
463 | CTMU_Select_Current_Source(CurrentSourceRange); |
||
464 | } |
||
465 | |||
466 | /********* End of Function *********************/ |
||
467 | |||
468 | /******************************************************************** |
||
469 | * Function : void mTouchCapPhy_SetTrimValue( WORD TrimValue); |
||
470 | * PreCondition : None |
||
471 | * |
||
472 | * Input : TrimValue : CTMU TRIM bits Settings |
||
473 | * |
||
474 | * Output : None |
||
475 | * |
||
476 | * Side Effects : None |
||
477 | * |
||
478 | * Overview : This function will setup the trim values for the selected current source range |
||
479 | * |
||
480 | * Note : Set the proper Trim value of the current source as passed in the argument. |
||
481 | *******************************************************************/ |
||
482 | |||
483 | void mTouchCapPhy_SetTrimValue( WORD TrimValue) |
||
484 | { |
||
485 | |||
486 | CTMU_Trim_Value (TrimValue); |
||
487 | |||
488 | } |
||
489 | |||
490 | /******************************************************************** |
||
491 | * Function : void mTouchCapPhy_Discharge_Current (void) |
||
492 | * |
||
493 | * PreCondition : None |
||
494 | * |
||
495 | * Input : None |
||
496 | * |
||
497 | * Output : None |
||
498 | * |
||
499 | * Side Effects : None |
||
500 | * |
||
501 | * Overview : Discharges the current source. |
||
502 | * |
||
503 | * |
||
504 | * Note : |
||
505 | *******************************************************************/ |
||
506 | |||
507 | void mTouchCapPhy_Discharge_Current (void) |
||
508 | { |
||
509 | Enble_CTMU_CurrentDischarge; |
||
510 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
511 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
512 | Nop(); // Delay for CTMU charge time |
||
513 | |||
514 | Disble_CTMU_CurrentDischarge; |
||
515 | |||
516 | } |
||
517 | |||
518 | /********* End of Function *********************/ |
||
519 | |||
520 | /******************************************************************** |
||
521 | * Function : void mTouchCapPhy_Charge_Current (void) |
||
522 | * |
||
523 | * PreCondition : None |
||
524 | * |
||
525 | * Input : None |
||
526 | * |
||
527 | * Output : None |
||
528 | * |
||
529 | * Side Effects : None |
||
530 | * |
||
531 | * Overview : Charges the cusrrent for the time as per the value given to "CTMU_CHARGE_TIME_COUNT" |
||
532 | * in mTouchCap_Config.h file. |
||
533 | * |
||
534 | * |
||
535 | * Note : |
||
536 | *******************************************************************/ |
||
537 | |||
538 | void mTouchCapPhy_Charge_Current (void) |
||
539 | { |
||
540 | WORD loopIndex; |
||
541 | |||
542 | ADC1_Clear_Intr_Status_Bit; |
||
543 | Enable_ADC_Sampling; |
||
544 | Disbl_CTMUEdge2; |
||
545 | Enble_CTMUEdge1; |
||
546 | |||
547 | if (0 != loopCount) //n:ref |
||
548 | { |
||
549 | /* When we require to enaable EDGE1 or EDGE2, should we pass as a param*/ |
||
550 | for (loopIndex = 0; loopIndex < loopCount; loopIndex++) |
||
551 | Nop(); // Delay for CTMU charge time // PIC18 - 4 TCY to execute Nop(); //PIC24 - 2 TCY to execute Nop(); |
||
552 | } |
||
553 | Disbl_CTMUEdge1; |
||
554 | |||
555 | } |
||
556 | |||
557 | /********************************************************************* |
||
558 | * Function : WORD mTouchCapPhy_Read_ADC(void) |
||
559 | * |
||
560 | * PreCondition : None |
||
561 | * |
||
562 | * Input : None |
||
563 | * |
||
564 | * Output : Resultant ADC Buffer value |
||
565 | * |
||
566 | * Side Effects : None |
||
567 | * |
||
568 | * Overview : Reads the ADC data of CTMU Channel. |
||
569 | * |
||
570 | * |
||
571 | * Note : |
||
572 | *******************************************************************/ |
||
573 | |||
574 | WORD mTouchCapPhy_Read_ADC(void) |
||
575 | { |
||
576 | |||
577 | WORD Channel_Data; |
||
578 | |||
579 | ADC1_Clear_Intr_Status_Bit; |
||
580 | Disable_ADC_Sampling; |
||
581 | while(!ADC1_IF_Bit); // Wait for the A/D conversion to finish |
||
582 | |||
583 | Channel_Data = mTouchCapADC_ReadADCBuff(0); // Read the value from the A/D conversion |
||
584 | |||
585 | Disable_ADC_Sampling; |
||
586 | ADC1_Clear_Intr_Status_Bit; |
||
587 | ADC_Done_Bit = 0; |
||
588 | |||
589 | return Channel_Data; |
||
590 | |||
591 | |||
592 | } |
||
593 | |||
594 | /********* End of Function *********************/ |
||
595 | |||
596 | |||
597 | |||
598 | /******************************************************************** |
||
599 | * Function : void mTouchCapPhy_InitCTMU(void) |
||
600 | * |
||
601 | * PreCondition : None |
||
602 | * |
||
603 | * Input : None |
||
604 | * |
||
605 | * Output : None |
||
606 | * |
||
607 | * Side Effects : None |
||
608 | * |
||
609 | * Overview : Does the initialization of all peripherals/ports necessary for CTMU operation. |
||
610 | * |
||
611 | * |
||
612 | * Note : |
||
613 | *******************************************************************/ |
||
614 | void mTouchCapPhy_InitCTMU(void) |
||
615 | { |
||
616 | /* Initialize PORTs */ |
||
617 | mTouchCapApp_PortSetup(); |
||
618 | |||
619 | /* Initialize CTMU */ |
||
620 | mTouchCapPhy_CTMUSetup(); |
||
621 | |||
622 | /* Initialize ADC */ |
||
623 | mTouchCapPhy_AdcSetup(); |
||
624 | |||
625 | Set_Adc_Enable_State(ENABLE); |
||
626 | |||
627 | Enable_ADC_Sampling; |
||
628 | |||
629 | Set_CTMU_ENABLE_Status (ENABLE); |
||
630 | |||
631 | Enble_CTMU_CurrentDischarge; // Drain any charge on the circuit |
||
632 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
633 | Disble_CTMU_CurrentDischarge; |
||
634 | Nop(); Nop(); Nop(); Nop(); Nop(); |
||
635 | } |
||
636 | |||
637 | /******************************************************************** |
||
638 | * Function : void mTouchCapPhy_ChargeTimeInit(void) |
||
639 | * |
||
640 | * PreCondition : None |
||
641 | * |
||
642 | * Input : None |
||
643 | * |
||
644 | * Output : None |
||
645 | * |
||
646 | * Side Effects : None |
||
647 | * |
||
648 | * Overview : Initializes the charge time. User can change this by modifying the value given for "CTMU_CHARGE_TIME_COUNT" |
||
649 | * in Config.h file. |
||
650 | * |
||
651 | * Note : |
||
652 | *******************************************************************/ |
||
653 | void mTouchCapPhy_ChargeTimeInit(void) |
||
654 | { |
||
655 | loopCount = CTMU_CHARGE_TIME_COUNT; //Loop counter - determines charge time for each |
||
656 | |||
657 | } |
||
658 | |||
659 | |||
660 | /******************************************************************** |
||
661 | * Function : void mTouchCapPhy_ReadCTMU(WORD ChannelNumber) |
||
662 | * |
||
663 | * PreCondition : None |
||
664 | * |
||
665 | * Input : ChannelNumber |
||
666 | CHANNEL_AN0 , |
||
667 | CHANNEL_AN1 , |
||
668 | CHANNEL_AN2 , |
||
669 | CHANNEL_AN3 , |
||
670 | CHANNEL_AN4 , |
||
671 | CHANNEL_AN5 , |
||
672 | CHANNEL_AN6 , |
||
673 | CHANNEL_AN7 , |
||
674 | CHANNEL_AN8 , |
||
675 | CHANNEL_AN9 , |
||
676 | CHANNEL_AN10 , |
||
677 | CHANNEL_AN11 , |
||
678 | CHANNEL_AN12 , |
||
679 | CHANNEL_AN13 , // for PIC24. Not available in PIC18 |
||
680 | CHANNEL_AN14 , // for PIC24. Not available in PIC18 |
||
681 | CHANNEL_AN15 // for PIC24. Not available in PIC18 |
||
682 | * Output : None |
||
683 | * |
||
684 | * Side Effects : None |
||
685 | * |
||
686 | * Overview : Scans the CTMU channel for ADC voltage. It updates the "curRawData" and "actualValue" buffers. |
||
687 | * |
||
688 | * |
||
689 | * Note : |
||
690 | *******************************************************************/ |
||
691 | void mTouchCapPhy_ReadCTMU(WORD ChannelNumber) |
||
692 | { |
||
693 | |||
694 | #ifdef DETAILED_CALCULATION |
||
695 | DWORD total = 0; |
||
696 | SHORT chrd; |
||
697 | // Get the raw sensor reading. |
||
698 | for(chrd=0; chrd< NUM_HF_READS; chrd++) |
||
699 | { |
||
700 | |||
701 | /*************************************************************************** |
||
702 | --------------- Demo of API "mTouchCapAPI_CTMU_GetChannelReading" ----------------------- |
||
703 | Get the channdel ADC data using the API "mTouchCapAPI_CTMU_GetChannelReading". |
||
704 | ***************************************************************************/ |
||
705 | currentADCValue = mTouchCapAPI_CTMU_GetChannelReading(ChannelNumber); |
||
706 | //End of CTMU read |
||
707 | total = total + currentADCValue; |
||
708 | } |
||
709 | |||
710 | |||
711 | #ifdef GROUND_TEST // Note: A/D conversion not used |
||
712 | /* CHANGE_MCHP */ // A/D mux must connect to channel for CTMU to drain charge |
||
713 | mTouchCapPhy_Discharge_Current (); |
||
714 | #endif |
||
715 | |||
716 | /* Error check */ |
||
717 | if (total != 0) |
||
718 | { |
||
719 | currentADCValue = total/NUM_HF_READS; |
||
720 | } |
||
721 | else |
||
722 | { |
||
723 | currentADCValue = 0; |
||
724 | } |
||
725 | |||
726 | #else /* One shot calculation using Iterative method */ |
||
727 | /*************************************************************************** |
||
728 | --------------- Demo of API "mTouchCapAPI_ScanChannelIterative" ----------------------- |
||
729 | |||
730 | Read the channel Rawdata in an iterative fashion for the mentioned sample count using the |
||
731 | API "mTouchCapAPI_ScanChannelIterative". |
||
732 | ***************************************************************************/ |
||
733 | currentADCValue = mTouchCapAPI_ScanChannelIterative(ChannelNumber,NUM_HF_READS); |
||
734 | #endif |
||
735 | |||
736 | //For debug - channels 6&7 are not enabled. They are clk & data lines for ICSP. use dummy values |
||
737 | #ifdef DEBUG |
||
738 | if(ScanChannels[ChannelNumber] == CHANNEL_AN6 || ScanChannels[ChannelNumber] == CHANNEL_AN7) |
||
739 | { |
||
740 | currentADCValue = 0x240; |
||
741 | } |
||
742 | // ... |
||
743 | #endif |
||
744 | |||
745 | #ifdef GUI_USB_INTERFACE |
||
746 | scaledBigValue = currentADCValue * 64; // BIGVAL is current measurement left shifted 4 bits for GUI display |
||
747 | #else |
||
748 | scaledBigValue = currentADCValue; // Copy the ADC value of the channel |
||
749 | #endif |
||
750 | |||
751 | curRawData[ChannelNumber] = scaledBigValue; // curRawData array holds the most recent BIGVAL values |
||
752 | actualValue[ChannelNumber] = currentADCValue; |
||
753 | |||
754 | } //end ReadCTMU() |
||
755 | |||
756 | |||
757 | |||
758 | |||
759 | /****************************************************************************** |
||
760 | * Function : void CTMU_Current_trim_config(int channel_no) |
||
761 | * |
||
762 | * PreCondition : None |
||
763 | * |
||
764 | * Input : current Channel number that is being processed by ADC |
||
765 | * |
||
766 | * Output : None |
||
767 | * |
||
768 | * Side Effects : None |
||
769 | * |
||
770 | * Overview : This function will set the trim bits for 2 channel slider |
||
771 | * 4 channel slider, Direct key and MAtrix key board |
||
772 | * |
||
773 | * Note : None |
||
774 | *****************************************************************************/ |
||
775 | void CTMU_Current_trim_config(int channel_no) |
||
776 | { |
||
777 | |||
778 | if(trimbitsReady) |
||
779 | { |
||
780 | mTouchCapAPI_CTMU_SetupCurrentSource(channel_IRNG_value[channel_no],channel_TRIM_value[channel_no]); |
||
781 | } |
||
782 | |||
783 | } |
||
784 | |||
785 | /******************************************************************** |
||
786 | * Function : void mTouchCapPhy_AverageData(WORD Index) |
||
787 | * |
||
788 | * PreCondition : None |
||
789 | * |
||
790 | * Input : Channel Number |
||
791 | * |
||
792 | * Output : None |
||
793 | * |
||
794 | * Side Effects : None |
||
795 | * |
||
796 | * Overview : This function is used to average the Current Raw ADC value and the |
||
797 | * Average ADC value based on the type of the decode method that is |
||
798 | * selected by the application. |
||
799 | * Note : |
||
800 | *******************************************************************/ |
||
801 | |||
802 | void mTouchCapPhy_AverageData(WORD Index) |
||
803 | { |
||
804 | |||
805 | BYTE Filter_type; |
||
806 | |||
807 | /* Filtering based on channel specefic filter type */ |
||
808 | Filter_type = chFilterType[Index]; |
||
809 | |||
810 | /* Check the opted filter type of the channel. Copy if within limits. Else, the default type would be "1BY20" method. */ |
||
811 | if ( (Filter_type > FILTER_METHOD_FASTAVERAGE) ) |
||
812 | { |
||
813 | Filter_type = FILTER_METHOD_SLOWAVERAGE; |
||
814 | } |
||
815 | |||
816 | /* Process the average based on filter type selected by user in APi-mTouchCapAPI_SetUpChannel*/ |
||
817 | switch(Filter_type) |
||
818 | { |
||
819 | case FILTER_METHOD_SLOWAVERAGE: |
||
820 | // 7. Average in the new value (channel based delay) |
||
821 | // Always Average (all buttons one after the other in a loop) |
||
822 | |||
823 | if (curRawData[Index] > averageData[Index]) |
||
824 | { |
||
825 | averageData[Index] = curRawData[Index]; // If curRawData is above Average, reset to high average. |
||
826 | } |
||
827 | |||
828 | if(avg_delay[Index] < NUM_AVG) |
||
829 | { |
||
830 | avg_delay[Index]++; // Counting 0..8 has effect of every 9th count cycling the next button. |
||
831 | } |
||
832 | else |
||
833 | { |
||
834 | avg_delay[Index] = 0; // Counting 0..4 will average faster and also can use 0..4*m, m=0,1,2,3.. |
||
835 | } |
||
836 | |||
837 | if(avg_delay[Index] == NUM_AVG) |
||
838 | { |
||
839 | smallAvg[Index] = averageData[Index] / NUM_AVG; // SMALLAVG is the current average right shifted 4 bits |
||
840 | // Average in raw value. |
||
841 | averageData[Index] = averageData[Index] + ((curRawData[Index] / NUM_AVG) - smallAvg[Index]); |
||
842 | } |
||
843 | |||
844 | break; |
||
845 | |||
846 | |||
847 | case FILTER_METHOD_GATEDAVERAGE: |
||
848 | //Filter Method CASE: Gated Average |
||
849 | // 7. Average in the new value (channel based delay) |
||
850 | // Always Average (all buttons one after the other in a loop) |
||
851 | |||
852 | if(avg_delay[Index] < NUM_AVG) |
||
853 | { |
||
854 | avg_delay[Index]++; // Counting 0..8 has effect of every 9th count cycling the next button. |
||
855 | } |
||
856 | else |
||
857 | { |
||
858 | avg_delay[Index] = 0; // Counting 0..4 will average faster and also can use 0..4*m, m=0,1,2,3.. |
||
859 | } |
||
860 | if(avg_delay[Index] == NUM_AVG) |
||
861 | { |
||
862 | /* Gated average only of no key pressed. */ |
||
863 | /* Stop averaging when press is being sensed. //STD:*/ |
||
864 | if ( KEY_NOT_PRESSED == mTouchCapAPI_getChannelTouchStatus(Index,DECODE_METHOD_PRESS_ASSERT )) |
||
865 | { |
||
866 | smallAvg[Index] = averageData[Index] / NUM_AVG; // SMALLAVG is the current average right shifted 4 bits |
||
867 | // Average in raw value. |
||
868 | averageData[Index] = averageData[Index] + ((curRawData[Index] / NUM_AVG) - smallAvg[Index]); |
||
869 | } |
||
870 | } |
||
871 | break; |
||
872 | |||
873 | case FILTER_METHOD_FASTAVERAGE: |
||
874 | /* The latest current raw data would be the latest average data */ |
||
875 | if (curRawData[Index] > averageData[Index]) |
||
876 | { |
||
877 | averageData[Index] = curRawData[Index]; // If curRawData is above Average, reset to high average. |
||
878 | } |
||
879 | |||
880 | break; |
||
881 | |||
882 | } |
||
883 | |||
884 | /* Dynamic trip value calculation . STD: */ |
||
885 | DynamicTripValueCalculation(Index); |
||
886 | |||
887 | } |
||
888 | |||
889 | |||
890 | |||
891 | /******************************************************************** |
||
892 | * Function : void DynamicTripValueCalculation(WORD Index) |
||
893 | * |
||
894 | * PreCondition : None |
||
895 | * |
||
896 | * Input : Index- Channel Number |
||
897 | * |
||
898 | * Output : None |
||
899 | * |
||
900 | * Side Effects : None |
||
901 | * |
||
902 | * Overview : calculate the dynamic trip value. |
||
903 | * |
||
904 | * |
||
905 | * Note : |
||
906 | *******************************************************************/ |
||
907 | void DynamicTripValueCalculation(WORD Index) |
||
908 | { |
||
909 | #ifndef USE_STATIC_TRIP_VALUE |
||
910 | |||
911 | tripValue[Index] = (averageData[Index] / KEYTRIPDIV); |
||
912 | hystValue[Index] = (tripValue[Index] / HYSTERESIS_VALUE); |
||
913 | |||
914 | #endif |
||
915 | } |
||
916 | |||
917 |
Powered by WebSVN v2.8.3