/************************************************************************************************** * ADS7843 touch screen Driver * File name : touch.h * Programmer : jaruwit supa * Web presence : www.circuitidea.com * Note : BL-TFT240320PLUS using ADS7843 driver * Language : avrGCC * Hardware : arduino * Date : 20/12/2009 ************************************************************************************************ * DESCRIPTION * =========== BL-TFT240320PLUS contain touch screen ADS7843. driver touch screen using 10 bit(1024) to calibrate(not include in api) Calibrate routine use for calibrate and save matrix to eeprom. touch screenclass wil read back on init class. Change eeprom location by change #define EE_MATRIX_ADDR Because of no hardware PENIRQ so lowest point require for test touch press or not. Change #define ADC_MIN_Y for untouch condition. becare full 10 bit(1024) is base. ************************************************************************************************/ /* _____STANDARD INCLUDES____________________________________________________ */ extern "C" { #include #include #include } /* _____PROJECT INCLUDES_____________________________________________________ */ #include "WProgram.h" #include "touch.h" /* _____PRIVATE DEFINES______________________________________________________ */ #define ADC_MIN_Y (100) // Modified, orig 100 /* _____HARDWARE DEFINES_____________________________________________________ */ #define TS_SPI_DDR DDRB #define TS_SPI_PORT PORTB #define TS_SPI_PIN PINB #define TS_SS_BIT 2 #define TS_MOSI_BIT 3 #define TS_MISO_BIT 4 #define TS_SCK_BIT 5 #define TS_CS_DDR DDRB #define TS_CS_PORT PORTB #define TS_CS_PIN PINB #define TS_CS_BIT 1 /* _____Constructor__________________________________________________________ */ touchscreen::touchscreen() { getMatrix(); } /* _____Private _____________________________________________________________ */ unsigned char touchscreen::readADC(unsigned char axis) { SPDR = axis; // transfer axis while((SPSR & 0x80) == 0); // wait for complete return (SPDR); // get data } void touchscreen::readTouch(POINT * pADC_point) { unsigned int x,y; unsigned char buf_data[4]; // enable touch screen TS_CS_PORT &= ~(1<> 3; // Shift 5 bit low y = (unsigned int)buf_data[2] << 5; // Shift 7 bit High y |= (unsigned int)buf_data[3] >> 3; // Shift 5 bit low // using 10 bit for 3 point calibrate. by integrate 4 point(0,1,2,3) in to 1 point pADC_point->x = x >> 2; // Modified orig 2 pADC_point->y = (4095L-y) >> 2; // " } void touchscreen::sampling(POINT * pScreen, unsigned char nTest) { POINT adcPoint; long avgX, avgY; unsigned char i; // scan counter unsigned char nInvalid; // Invalid counter pScreen->x = 0; pScreen->y = 0; readTouch(&adcPoint); // Read value ADC Touch X-Y if (adcPoint.y < ADC_MIN_Y) // inactive touch return; avgX = adcPoint.x; avgY = adcPoint.y; nInvalid = 0; i = nTest; do { if (++nInvalid > 20) // too many error. touch not long enought return; readTouch(&adcPoint); // Read value ADC Touch X-Y if (adcPoint.y < ADC_MIN_Y) // inactive touch continue; // average with avgX += adcPoint.x; avgX >>= 1; avgY += adcPoint.y; avgY >>= 1; nInvalid = 0; // reset invalid } while (--i); pScreen->x = avgX; pScreen->y = avgY; } /* _____Public _______________________________________________________________ */ void touchscreen::Reset(void) { char temp; // init port TS_CS_PORT |= (1<