Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TS_I2C.c@0:f69f52bd2222, 2012-03-12 (annotated)
- Committer:
- fblanc
- Date:
- Mon Mar 12 11:57:30 2012 +0000
- Revision:
- 0:f69f52bd2222
Rev 1.0a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:f69f52bd2222 | 1 | /** |
fblanc | 0:f69f52bd2222 | 2 | * @file STM811.c |
fblanc | 0:f69f52bd2222 | 3 | * @brief library TouchScreen STM811 |
fblanc | 0:f69f52bd2222 | 4 | * @author Frederic BLANC |
fblanc | 0:f69f52bd2222 | 5 | */ |
fblanc | 0:f69f52bd2222 | 6 | |
fblanc | 0:f69f52bd2222 | 7 | |
fblanc | 0:f69f52bd2222 | 8 | #include "TS.h" |
fblanc | 0:f69f52bd2222 | 9 | I2C i2c(P0_10, P0_11);//P0_10 SDA2 P0_11 SCL2 |
fblanc | 0:f69f52bd2222 | 10 | int TS_Init (void) { |
fblanc | 0:f69f52bd2222 | 11 | int err=0; |
fblanc | 0:f69f52bd2222 | 12 | // err+=TS_Write (TS_TSC_CTRL, 0x03); /* TSC_CTRL register X, Y only mode, enable */ |
fblanc | 0:f69f52bd2222 | 13 | // err+=TS_Write (TS_SYS_CTRL1, 0x02); /* Reset Touch-screen controller */ |
fblanc | 0:f69f52bd2222 | 14 | // wait_ms(10); /* Wait minimum of 10ms */ |
fblanc | 0:f69f52bd2222 | 15 | err+=TS_Write (TS_SYS_CTRL2, SYS_CTRL2_TS_OFF | SYS_CTRL2_GPIO_OFF); /* 1.Enable TSC and ADC */ |
fblanc | 0:f69f52bd2222 | 16 | err+=TS_Write (TS_INT_EN, INT_TOUCH_DET); /* 2.Enable Touch detect, FIFO */ |
fblanc | 0:f69f52bd2222 | 17 | err+=TS_Write (TS_ADC_CTRL1, 0x69); /* 3.Set sample time , 12-bit mode */ |
fblanc | 0:f69f52bd2222 | 18 | wait_ms(2); /* Wait minimum of 2ms */ |
fblanc | 0:f69f52bd2222 | 19 | err+=TS_Write (TS_ADC_CTRL2, 0x01); /* 4.ADC frequency 3.25 MHz */ |
fblanc | 0:f69f52bd2222 | 20 | err+=TS_Write (TS_GPIO_AF, 0x00); /* 5. ALL IO in ADC/TS mode */ |
fblanc | 0:f69f52bd2222 | 21 | err+=TS_Write (TS_TSC_CFG, 0xF5); /* 6. Set TSC_CFG register */ |
fblanc | 0:f69f52bd2222 | 22 | err+=TS_Write (TS_FIFO_TH, 0x01); /* 6a.Threshold for FIFO */ |
fblanc | 0:f69f52bd2222 | 23 | err+=TS_Write (TS_FIFO_STA, 0x01); /* 7.FIFO reset */ |
fblanc | 0:f69f52bd2222 | 24 | err+=TS_Write (TS_FIFO_STA, 0x00); /* 8.FIFO not reset */ |
fblanc | 0:f69f52bd2222 | 25 | err+=TS_Write (TS_TSC_FRACTION_Z, 0x07); /* 9.Fraction z */ |
fblanc | 0:f69f52bd2222 | 26 | err+=TS_Write (TS_TSC_I_DRIVE, 0x01); /* 10.Drive 50 mA typical */ |
fblanc | 0:f69f52bd2222 | 27 | err+=TS_Write (TS_TSC_CTRL, 0x01); /* 11.Enable TSC */ |
fblanc | 0:f69f52bd2222 | 28 | err+=TS_Write (TS_INT_STA, 0xFF); /* 12.Clear interrupt status */ |
fblanc | 0:f69f52bd2222 | 29 | err+=TS_Write (TS_INT_CTRL, 0x01); /* 13.Enable global interrupt */ |
fblanc | 0:f69f52bd2222 | 30 | |
fblanc | 0:f69f52bd2222 | 31 | return err; |
fblanc | 0:f69f52bd2222 | 32 | } |
fblanc | 0:f69f52bd2222 | 33 | |
fblanc | 0:f69f52bd2222 | 34 | int TS_Write (unsigned char reg,unsigned int val) { |
fblanc | 0:f69f52bd2222 | 35 | int err=0; |
fblanc | 0:f69f52bd2222 | 36 | int addr =0x82; |
fblanc | 0:f69f52bd2222 | 37 | char cmd[2]; |
fblanc | 0:f69f52bd2222 | 38 | cmd[0] = reg; // Register to be written |
fblanc | 0:f69f52bd2222 | 39 | cmd[1] = val; |
fblanc | 0:f69f52bd2222 | 40 | err+=i2c.write(addr, cmd,2); // Send addr cmd val |
fblanc | 0:f69f52bd2222 | 41 | return err; |
fblanc | 0:f69f52bd2222 | 42 | } |
fblanc | 0:f69f52bd2222 | 43 | |
fblanc | 0:f69f52bd2222 | 44 | unsigned int TS_Read (unsigned char reg, int num) { |
fblanc | 0:f69f52bd2222 | 45 | int addr =(0x82 | 0); |
fblanc | 0:f69f52bd2222 | 46 | unsigned int idata; |
fblanc | 0:f69f52bd2222 | 47 | char* data=(char*) &idata; |
fblanc | 0:f69f52bd2222 | 48 | data[1] =data[2] =data[3] =0; |
fblanc | 0:f69f52bd2222 | 49 | data[0] = reg; // Register to be written |
fblanc | 0:f69f52bd2222 | 50 | i2c.write(addr, data,1); // Send command string |
fblanc | 0:f69f52bd2222 | 51 | do{ |
fblanc | 0:f69f52bd2222 | 52 | i2c.read (addr, &data[--num], 1); |
fblanc | 0:f69f52bd2222 | 53 | }while (num); |
fblanc | 0:f69f52bd2222 | 54 | return idata; |
fblanc | 0:f69f52bd2222 | 55 | |
fblanc | 0:f69f52bd2222 | 56 | } |
fblanc | 0:f69f52bd2222 | 57 | |
fblanc | 0:f69f52bd2222 | 58 | int ts_XY( int *x,int *y,int *z) { |
fblanc | 0:f69f52bd2222 | 59 | int tch_z = 0; |
fblanc | 0:f69f52bd2222 | 60 | int tch_int = 0; |
fblanc | 0:f69f52bd2222 | 61 | int tch_smpl = 0; |
fblanc | 0:f69f52bd2222 | 62 | tch_int = TS_Read (TS_INT_STA, 1); /* Read Touch-screen interrupt status */ |
fblanc | 0:f69f52bd2222 | 63 | if (tch_int & 2) { /* If FIFO is above threshold */ |
fblanc | 0:f69f52bd2222 | 64 | tch_smpl = TS_Read (TS_FIFO_SIZE, 1); |
fblanc | 0:f69f52bd2222 | 65 | while (--tch_smpl) { |
fblanc | 0:f69f52bd2222 | 66 | TS_Read (TS_TSC_DATA_NAI, 4); |
fblanc | 0:f69f52bd2222 | 67 | } |
fblanc | 0:f69f52bd2222 | 68 | } |
fblanc | 0:f69f52bd2222 | 69 | tch_z = TS_Read (TS_TSC_DATA_NAI, 4); /* Read coordinates */ |
fblanc | 0:f69f52bd2222 | 70 | *x = (tch_z >> 20) & 0x00000FFF; |
fblanc | 0:f69f52bd2222 | 71 | *y = (tch_z >> 8) & 0x00000FFF; |
fblanc | 0:f69f52bd2222 | 72 | *z = (tch_z >> 0) & 0x0000000F; |
fblanc | 0:f69f52bd2222 | 73 | return 0; |
fblanc | 0:f69f52bd2222 | 74 | |
fblanc | 0:f69f52bd2222 | 75 | } |