frederic blanc / TS_STMPE811

Dependents:   Touch_Screen_MCB

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TS_I2C.cpp Source File

TS_I2C.cpp

00001 /**
00002 * @file STM811.c
00003 * @brief library TouchScreen STM811
00004 * @author Frederic BLANC
00005 */
00006 
00007 
00008 #include "TS.h"
00009 I2C i2c(P0_10, P0_11);//LPC1768 P0_10 SDA2 P0_11 SCL2 + pull up 3.3k 3.3VCC
00010 
00011 int TS_Init (void) {
00012     int err=0;
00013 //  err+=TS_Write (TS_TSC_CTRL, 0x03);      /* TSC_CTRL register   X, Y only mode, enable             */    
00014     err+=TS_Write (TS_SYS_CTRL1, 0x02);      /* Reset Touch-screen controller      */
00015    wait_ms(10);                             /* Wait minimum of 10ms               */
00016     err+=TS_Write (TS_SYS_CTRL2, SYS_CTRL2_TS_OFF | SYS_CTRL2_GPIO_OFF);      /* 1.Enable TSC and ADC                 */
00017     err+=TS_Write (TS_INT_EN, INT_TOUCH_DET);           /* 2.Enable Touch detect, FIFO          */
00018     err+=TS_Write (TS_ADC_CTRL1, 0x69);      /* 3.Set sample time , 12-bit mode      */
00019     wait_ms(2);                                 /* Wait minimum of 2ms                */
00020     err+=TS_Write (TS_ADC_CTRL2, 0x01);      /* 4.ADC frequency 3.25 MHz             */
00021     err+=TS_Write (TS_GPIO_AF, 0x00);        /* 5. ALL IO in ADC/TS mode            */
00022     err+=TS_Write (TS_TSC_CFG, 0xF5);           /* 6. Set TSC_CFG register               */
00023     err+=TS_Write (TS_FIFO_TH, 0x01);           /* 6a.Threshold for FIFO                 */
00024     err+=TS_Write (TS_FIFO_STA, 0x01);       /* 7.FIFO reset                         */
00025     err+=TS_Write (TS_FIFO_STA, 0x00);       /* 8.FIFO not reset                     */
00026     err+=TS_Write (TS_TSC_FRACTION_Z, 0x07); /* 9.Fraction z                         */
00027     err+=TS_Write (TS_TSC_I_DRIVE, 0x01);    /* 10.Drive 50 mA typical                */
00028     err+=TS_Write (TS_TSC_CTRL, 0x01);       /* 11.Enable TSC                         */
00029     err+=TS_Write (TS_INT_STA, 0xFF);           /* 12.Clear interrupt status             */
00030     err+=TS_Write (TS_INT_CTRL, 0x01);       /* 13.Enable global interrupt            */
00031 
00032     return err;
00033 }
00034 
00035 int TS_Write (unsigned char reg,unsigned int val) {
00036     int err=0;
00037     int addr =0x82;
00038     char cmd[2];
00039     cmd[0] = reg;            // Register to be written
00040     cmd[1] = val;
00041     err+=i2c.write(addr, cmd,2); // Send addr cmd val
00042     return err;
00043 }
00044 
00045 unsigned int TS_Read (unsigned char reg, int num) {
00046     int addr =(0x82 | 0);
00047     unsigned int idata;
00048     char* data=(char*) &idata;
00049     data[1] =data[2] =data[3] =0;
00050     data[0] = reg;            // Register to be written
00051     i2c.write(addr, data,1); // Send command string
00052     do{
00053         i2c.read (addr, &data[--num], 1);
00054     }while (num);
00055     return idata;
00056 
00057 }
00058 
00059 int ts_XY( int *x,int *y,int *z) {
00060     int  tch_z   = 0;
00061     int tch_int  =  0;
00062     int tch_smpl =  0;
00063     tch_int = TS_Read (TS_INT_STA, 1);    /* Read Touch-screen interrupt status */
00064         if (tch_int & 2) {            /* If FIFO is above threshold         */
00065             tch_smpl = TS_Read (TS_FIFO_SIZE, 1);
00066             while (--tch_smpl) {
00067                 TS_Read (TS_TSC_DATA_NAI, 4);
00068             }
00069         }
00070             tch_z = TS_Read (TS_TSC_DATA_NAI, 4);  /* Read coordinates                   */
00071             *x = (tch_z >> 20) & 0x00000FFF;
00072             *y = (tch_z >>  8) & 0x00000FFF;
00073             *z = (tch_z >>  0) & 0x0000000F;
00074     return 0;
00075 
00076 }