problem with ili9341 touch interface using stm32f103ret6
Hi, I successfully used a HAL library to drive a TFT ili9341 LCD and tried drawing a line on the LCD by touching the LCD and the code was perfectly accurate then I rewrote the code with registers. the touch is not accurate now and when I touch the LCD the line shows up at a point far from the place which is touched I attached the code. what might be the problem? the pins are defined with maximum frequency.
#include "ILI9341_Touchscreen.h"
#include "stm32f10x.h"
#include "main.h"
//Internal Touchpad command, do not call directly
uint16_t TP_Read(void)
{
uint8_t i = 16;
uint16_t value = 0;
while(i > 0x00)
{
value <<= 1;
TP_CLK_PORT->BSRR=TP_CLK_PIN;
TP_CLK_PORT->BRR=TP_CLK_PIN;
if ((TP_MISO_PORT->IDR&TP_MISO_PIN)!=0)
{
value++;
}
i--;
};
return value;
}
//Internal Touchpad command, do not call directly
void TP_Write(uint8_t value)
{
uint8_t i = 0x08;
TP_CLK_PORT->BRR=TP_CLK_PIN;
while(i > 0)
{
if((value & 0x80) != 0x00)
{
TP_MOSI_PORT->BSRR=TP_MOSI_PIN;
}
else
{
TP_MOSI_PORT->BRR=TP_MOSI_PIN;
}
value <<= 1;
TP_CLK_PORT->BSRR=TP_CLK_PIN;
TP_CLK_PORT->BRR=TP_CLK_PIN;
i--;
};
}
//Read coordinates of touchscreen press. Position[0] = X, Position[1] = Y
uint8_t TP_Read_Coordinates(uint16_t Coordinates[2])
{
TP_CLK_PORT->BSRR=TP_CLK_PIN;
TP_MOSI_PORT->BSRR=TP_MOSI_PIN;
TP_CS_PORT->BSRR=TP_CS_PIN;
uint32_t avg_x, avg_y = 0;
uint16_t rawx, rawy = 0;
uint32_t calculating_x, calculating_y = 0;
uint32_t samples = NO_OF_POSITION_SAMPLES;
uint32_t counted_samples = 0;
TP_CS_PORT->BRR=TP_CS_PIN;
while((samples > 0)&&((TP_IRQ_PORT->IDR)& TP_IRQ_PIN) == 0)
{
TP_Write(CMD_RDY);
rawy = TP_Read();
avg_y += rawy;
calculating_y += rawy;
TP_Write(CMD_RDX);
rawx = TP_Read();
avg_x += rawx;
calculating_x += rawx;
samples--;
counted_samples++;
};
TP_CS_PORT->BSRR=TP_CS_PIN;
if((counted_samples == NO_OF_POSITION_SAMPLES)&&((TP_IRQ_PORT->IDR)&TP_IRQ_PIN) == 0)
{
calculating_x /= counted_samples;
calculating_y /= counted_samples;
rawx = calculating_x;
rawy = calculating_y;
rawx *= -1;
rawy *= -1;
//CONVERTING 16bit Value to Screen coordinates
// 65535/273 = 240!
// 65535/204 = 320!
Coordinates[0] = ((240 - (rawx/X_TRANSLATION)) - X_OFFSET)*X_MAGNITUDE;
Coordinates[1] = ((rawy/Y_TRANSLATION)- Y_OFFSET)*Y_MAGNITUDE;
return TOUCHPAD_DATA_OK;
}
else
{
Coordinates[0] = 0;
Coordinates[1] = 0;
return TOUCHPAD_DATA_NOISY;
}
}
//Check if Touchpad was pressed. Returns TOUCHPAD_PRESSED (1) or TOUCHPAD_NOT_PRESSED (0)
uint8_t TP_Touchpad_Pressed(void)
{
if((TP_IRQ_PORT->IDR&TP_IRQ_PIN) == 0)
{
return TOUCHPAD_PRESSED;
}
else
{
return TOUCHPAD_NOT_PRESSED;
}
}
