# The 4-wire resistive touch is connected to the ADC's input but it doesn't give correct value

I've an STM32F103 MCU and a 4-wire resistive touch. this is the circuit I've wired up:

I've written this code:

#include "stm32f10x.h"
#include "ili9320.h"
#include "stdio.h"

#define SYSCLK_FREQ_72MHz

uint8_t A = 3,L;
char str[15];

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Private functions ---------------------------------------------------------*/

void Pins(void);
/*******************************************************************************
* Function Name  : main
* Description    : Main Programme
* Input          : None
* Output         : None
* Return         : None
* Attention      : None
*******************************************************************************/

int main(void)
{
/* Initialize the LCD */
ili9320_Initializtion();

/* Clear the LCD */
ili9320_Clear(Yellow);
Pins();
SysTick_Config(SystemCoreClock/100);
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
/* Infinite loop */
while (1)
{
if(L){
ili9320_DisplayStringLine(Line0,(u8 *)"000",White,Blue);
ili9320_DisplayStringLine(Line0,(u8 *)str,White,Blue);
L = 0;
}

}
}

void Pins(void){

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC , ENABLE );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init( GPIOC , &GPIO_InitStructure );

}

{
/* ADC1 regular channels configuration */

}

uint8_t i = 0;
/* Changes the pins config

PC0 = +X
PC2 = -X
PC1 = +Y
PC3 = -Y

*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_SetBits( GPIOC , GPIO_Pin_1 );

for( i=0 ; i<5 ; ++i ){
/* Start ADC1 Software Conversion */
}
}


Everything is working very well save one part! the ADC input! it sounds like the ADC goes crazy! I just get wrong value. either I push the screen or I don't push, it gives me a wrong value. I debugged the program and I found out the ADC's input gives wrong value. I think the problem is in the configuration of GPIOs. I mean this part:

/* Changes the pins config

    PC0 = +X
PC2 = -X
PC1 = +Y
PC3 = -Y

*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //GPIO_Mode_IN_FLOATING
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC , &GPIO_InitStructure );

GPIO_SetBits( GPIOC , GPIO_Pin_1 );


For ADC input(PC0) I configured it as GPIO_Mode_AIN (analog input) that I think it's correct but I doubt about other parts. in this function I just want to read the X axis then I configured PC1(+Y) as output push-pull and PC3(-Y) input pull-down and PC2(-X) as input floating. are they correct?

I got the idea from here. What do you think?

Edit1: or maybe the problem is the ADC because even when I don't touch the screen, it gives me some strange value. shouldn't it give me 0 at input?

Edit2: I added this line ili9320_DisplayStringLine(Line0,(u8 *)"000",White,Blue); within the main while and now it's a little better. just changing between 1 to 9. I did two simple test. I connected +X to the GND and it showed me just 0. and gain I connected +Y to 3v3 and it showed me between 169 to 200 on the LCD depends on where I pushed the LCD. [I changed the code]

Edit3: I changed the code and added GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; to each line of GPIO configuration and now it's working just like when I connected +Y to 3v3 and -Y to GND but I don't know why it shows me the result between 169 to 200 on the LCD depends on where I pushed the LCD the voltage on the input pin is correct.(it changes depends on where I pushed the LCD)

• Not sure of the naming of the various wires, but I would say that to read X position you should configure both Y as output PP and both X as either AIN or IN_FLOATING. – Vladimir Cravero Jul 4 '16 at 15:56
• @VladimirCravero Oh! Hi Vladimir! how you doing, man? so glad to see you again! for reading/wiring touch screens, I think there are several approaches. here is a link: shukra.cedt.iisc.ernet.in/edwiki/… – Roh Jul 4 '16 at 16:08
• Hi Roh, everything is fine, thanks. I hope you are well too. The link you gave states what I suspected, to measure the position along an axis you need to connect the axis terminals to Vdd, gnd and then measure the voltage on any of the remaining terminals, or both. If you connect a pull down instead of a strong gnd, and connect the other input to gnd through a pull down as well, you will get erroneous results. Just stick to wikipedia and you should get it. There also are ICs out there designed to drive resistive touch screens, with a convenient i2c interface. – Vladimir Cravero Jul 4 '16 at 19:54
• Also have a look here. Thankyou texas instruments! – Vladimir Cravero Jul 4 '16 at 20:25
• If you have the necessary stuff, it may be helpful to test the touch-screen with a power supply and a volt meter. Then you will get how it works at a more intuitive level. Apply voltage across X, and put the meter on one of the Y wires. Then drag your finger slowly in the X direction. Then in the Y direction. – mkeith Jul 5 '16 at 7:55