/****************************************************************************** TPfast_2.c - open source - written by www.RomanBlack.com NEW!!! note for SG v1.5 they changed the driveA and driveB pins to RG3 and RG4 (used to be RA5 and RA2) This file "TPfast_2.c" is the version for SG v1.5 only! My orig code for SG v1 is saved as "TPfast.c" TPfast are functions for TouchPanel reading on MikroE SmartGLCD240x128 module. These are high performance stripped down functions for reading X or Y of the Touch Panel, and they do not have any of the refined features of the MikroE TP library functions. Only a single byte value is returned for the X or Y, which represents ADC of 0-255 for the X or Y axis. These functions are very small and fast and can be called in the middle of time sensitive code to get a TP X or Y value. NOTE! Add a small delay (500uS?) after changing SetX or SetY to allow the PIC ADC to settle. ******************************************************************************/ //============================================================================= // TPFAST SETX //============================================================================= void TPfast_SetX(void) { //------------------------------------------------------- // RomanBlack's high performance TouchPanel read functions for // SmartGLCD240x128 module; Jan 2011 - open source. // Call this function to set up the TP to read the X axis. // Note! If you have changed axis, there must be a delay of at least // 500uS before reading the new selected axis (to allow ADC to settle). //------------------------------------------------------- // set the PIC pins so it can read the TP X axis LATG.F3 = 1; // set DRIVEA and DRIVEB pins to TP LATG.F4 = 0; // set the ADC to read from the now floating pin (TP BOTTOM on RA0) ADCON0 = 0b00000001; // adc channel AN0 is selected } //----------------------------------------------------------------------------- //============================================================================= // TPFAST SETY //============================================================================= void TPfast_SetY(void) { //------------------------------------------------------- // RomanBlack's high performance TouchPanel read functions for // SmartGLCD240x128 module; Jan 2011 - open source. // Call this function to set up the TP to read the Y axis. // Note! If you have changed axis, there must be a delay of at least // 500uS before reading the new selected axis (to allow ADC to settle). //------------------------------------------------------- // set the PIC pins so it can read the TP Y axis LATG.F3 = 0; // set DRIVEA and DRIVEB pins to TP LATG.F4 = 1; // // set the ADC to read from the now floating pin (TP LEFT on RA1) ADCON0 = 0b00000101; // adc channel AN1 is selected } //----------------------------------------------------------------------------- //============================================================================= // TPFAST STARTREAD //============================================================================= void TPfast_StartRead(void) { //------------------------------------------------------- // RomanBlack's high performance TouchPanel read functions for // SmartGLCD240x128 module; Jan 2011 - open source. // Call this function to start an ADC read of the TP. //------------------------------------------------------- // read either X or Y axis of the touchpanel, based on // the TP axis which was last set up. ADCON0.F1 = 1; // initiate a ADC read } //----------------------------------------------------------------------------- //============================================================================= // TPFAST READ //============================================================================= unsigned char TPfast_Read(void) { //------------------------------------------------------- // RomanBlack's high performance TouchPanel read functions for // SmartGLCD240x128 module; Jan 2011 - open source. // Call this function to get the ADC value of the TP. //------------------------------------------------------- // get either X or Y axis of the touchpanel, based on // the TP axis which was last set up. // wait for adc conversion to finish if it is still going! while(ADCON0.F1) continue; // then get the adc result (0-255) and return it return(ADRESH); } //----------------------------------------------------------------------------- //============================================================================= // TPfast RELEASE //============================================================================= void TPfast_release(void) { //------------------------------------------------------- // just loop and do nothing until TP is released, we only // need to test tp_y. It needs no pressed for 20mS before it // will exit. //------------------------------------------------------- unsigned char tp_debounce; TPfast_SetY(); // set to read Y tp_debounce = 10; while(tp_debounce) { RDelay_mS(2); // get TouchPanel Y value TPfast_StartRead(); // start TP adc conversion tp_y = TPfast_Read(); if(tp_y < 19) tp_debounce--; // if NOT pressed else tp_debounce = 10; } } //-----------------------------------------------------------------------------