Helpful ReplyHot!XPT2046 problems.

Author
SVW1986
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/26 04:54:34
  • Location: 0
  • Status: offline
2015/04/26 05:33:55 (permalink)
0

XPT2046 problems.

Hi,
 
I'm trying to read the XPT2046 temperature and X and Y values.
 
First I would like to debug the X and Y value:
I have read the X and Y values (8 bit mode) now but there is something strange: I read the X value one command after the X value command and the Y value also one command later. is this normal? to clarify here's what I do:
 
- I send the Y value command (0x98)
- I do a dummy read
- I send the X value command (0xD8)
- I read the Y value
- I send the X value command again (0xD8)
- I read the X value
 
This part of my code is confirmed working. and the values are always correct. I just don't get why because the datasheet says the value should come right after the command. I read directly from the SPI4BUF register.
 
Secondly the temperature reading:
For the temperature reading (12 bit) I'm always getting the same value as High and Low byte. this also has the problem of being shifted one command.
 
Here's what I do:
 
- I send the 0x87 command
- do 2 dummy reads
- I send the 0x87 command again
- I read the temperature High and low bytes
- I Send a 0xD8 command
- do 2 dummy reads (these also give the temperature If I put them on the screen)
 
Also read directly from SPI4BUF. and the high and low byte always give the same value. When cooled with freezer they do shift with the temperature. I've also tried another screen with the same results.
#1
ric
Super Member
  • Total Posts : 22098
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: XPT2046 problems. 2015/04/26 15:18:20 (permalink)
0 (1)
Is this the first time you have used SPI?
What is happening is exactly as I would expect. Every SPI transaction is a simultaneous read and write.
When you read the SSPBUF after sending a byte, you get the value that was received as you were sending that byte.
If you look at the timing diagrams in the XPT2046 datasheet, you can see that the requested data does not start arriving until the next transaction after you ask for it, so you have to send another byte to get it.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
SVW1986
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/26 04:54:34
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/04/27 09:27:53 (permalink)
0
Hi,
 
Thanks for the quick reply.
 
I used the datasheet to write the code. I found that the actual output seems to be delayed though.
according to the datasheet the first 8 clocks are for the command. these are followed by another series of 8 clocks to read the data in 8 bit mode. the problem is that those 8 clocks for the data don't give me result of the command N. these give me the result of the command N-1.
 
so in total to get the data I need I have to send the following:
COMMAND N (8clocks)
DATA  of COMMAND N-1 (8 clocks)
COMMAND N+1 (8 clocks)
DATA of COMMAND N (8 clocks)
 
That's 32 clocks in total... according to the timing diagrams in the datasheet I would only need 16.
So I'm a bit confused because of this :p is there maybe a buffer in the XPT2046 then?
#3
ric
Super Member
  • Total Posts : 22098
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: XPT2046 problems. 2015/04/27 13:11:17 (permalink)
0
How exactly are you reading the data?
i.e. please show your actual code.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
SVW1986
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/26 04:54:34
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/04/28 10:18:42 (permalink)
0
Here's my SPI code:
 
int init_SPI(void){
SpiChnClose(4); //Close the spi port for configuration
SpiChnOpen(4,SPI_OPEN_MSTEN|SPI_OPEN_SMP_END|SPI_OPEN_MODE8|SPI_OPEN_CKP_HIGH,64); //Open SPI channel 4 as master in 8 bit mode)
delayms(20);
return SpiChnGetStatus(4);
};
 
int LCD_Touch_GetXY(void){
LCD_TOUCH_CS = 0; //Touch screen chip select low to activate
 
//READ Y COORDINATES NEXT AND DUMMY READ OF PREVIOUS DATA
SPI4BUF = 0x98; //Read y coordinates in 8 bit mode next
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
SPI4STATCLR = 1<<6;
delayms(1); //setteling time for touch panel
SPI4BUF = 0x00; //8 clocks for data
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
touchx = SPI4BUF;
SPI4STATCLR = 1<<6;
 
//READ X COORDINATES NEXT AND GET Y COORDINATES
SPI4BUF = 0xD8; //Read x coordinates in 8 bit mode next
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
SPI4STATCLR = 1<<6;
delayms(1); //setteling time for touch panel
SPI4BUF = 0x00; //8 clocks for data
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
touchy = SPI4BUF; //GET Y COORDINATES
SPI4STATCLR = 1<<6;
 
//DUMMY COMMAND TO GET X COORDINATES
SPI4BUF = 0xD8; //Read x coordinates in 8 bit mode next
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
SPI4STATCLR = 1<<6;
delayms(1); //setteling time for touch panel
SPI4BUF = 0x00; //8 clocks for data
while (!SPI4STATbits.SPIBUSY); //Wait for end of transmission
touchx = SPI4BUF; //GET X COORDINATES
SPI4STATCLR = 1<<6;
LCD_TOUCH_CS = 1; //Touch screen chip select high to disable
return SpiChnGetStatus(4); //Get SPI Channel status
};
 
 
#5
SVW1986
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/26 04:54:34
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/04/30 08:49:49 (permalink)
0
Might be a timing issue. If I mess around with the delays things start acting weird.
I also get correct data out of the temperature now. but this is shifted 8 clocks. having a series of 8 clocks between the command byte and the reading of the value for some reason. The actual value does seem to be correct.
#6
KR1
Senior Member
  • Total Posts : 123
  • Reward points : 0
  • Joined: 2015/05/11 17:26:44
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/12/01 22:27:25 (permalink)
0
Hello. Did you resolved the issue?? (I am having a similar problem)
 
Also did you make it work for 12bit too?
#7
NKurzman
A Guy on the Net
  • Total Posts : 14959
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/12/01 22:30:20 (permalink) ☄ Helpfulby KR1 2015/12/02 17:37:20
0 (1)
Make sure the phase settings of the slave device match the settings of the PIC
#8
KR1
Senior Member
  • Total Posts : 123
  • Reward points : 0
  • Joined: 2015/05/11 17:26:44
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/12/02 00:28:57 (permalink)
0 (1)
NKurzman
Make sure the phase settings of the slave device match the settings of the PIC



Curiously when I made them match, the LCD is dead. No response at all (in 12 bit mode- it responds in 8 bit mode)
Then just to try I altered the phase settings(I know, I shouldnt have neutral: [:-]) and then the 12 bit LCD started responding (in a totally crazy and wrong way though)
 
 
EDIT: I think that was the issue! Thanks a lot! (the settings suggested in the datasheet are wrong I think. Mode 3 worked for me)
 
EDIT2: turns out I still got problems.... the values are not entirely correct...seems slanted....
I wonder how the OP solved the issue
post edited by KR1 - 2015/12/02 22:10:05
#9
SVW1986
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2015/04/26 04:54:34
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2015/12/03 15:58:12 (permalink)
0
Hi,
 
In the end i did get the 12 bit mode to work on the temperature... the 12bit on the x and y coordinates i did not get into it further because that resolution was not needed for my project.
I also found that the XPT2046 can lock up under certain conditions. going from 12bit mode back to 8bit mode can give some problems as well... I'll see if I can find my code and I'll post it here.
 
a real solution to the shifted data I did not find though... the code that I got however does work on the temperature and on the x and y coordinates (landscape orientation of the display). the temperature data is also shifted one byte for me for some reason...
 
I tested this with 2 LCD boards and 2 different PIC32 controllers...
 
Best regards
 
 
 
#10
Purblod
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2016/01/26 08:33:23
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2016/01/27 07:31:20 (permalink)
0 (1)
Hi,
 
I have been having the same problems with XPT2046. The solution is to empty transmit buffer. Below is my code. Help yourself. :)
 
 // measure Z1 
 txdata = 0xB3;// Get Z1, 12bit,diff,no powerdown
  XPT2046_ToggleCS(ENABLE);
  HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//send command
 txdata = 0x03;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive high byte
 ftmp = rxdata;
 ftmp <<= 8;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive low byte
 ftmp += rxdata;
  ftmp >>= 3;
  fZ1raw = ftmp & 0x0FFF;
 XPT2046_waitsome(beforecslowwait);
 XPT2046_ToggleCS(DISABLE);

  XPT2046_waitsome(intermeasurementwait);
 
 // measure Z2
  txdata = 0xC3;
  XPT2046_ToggleCS(ENABLE);
  HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//send command
 txdata = 0x03;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive high byte
 ftmp = rxdata;
 ftmp <<= 8;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive low byte
 ftmp += rxdata;
  ftmp >>= 3;
  fZ2raw = ftmp & 0x0FFF;
 XPT2046_waitsome(beforecslowwait);
 XPT2046_ToggleCS(DISABLE);

  XPT2046_waitsome(intermeasurementwait);

  // measure X
  txdata = 0x93;
  XPT2046_ToggleCS(ENABLE);
  HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//send command
 txdata = 0x03;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive high byte
 ftmp = rxdata;
 ftmp <<= 8;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive low byte
 ftmp += rxdata;
  ftmp >>= 3;
  fXraw = ftmp & 0x0FFF;
 XPT2046_waitsome(beforecslowwait);
 XPT2046_ToggleCS(DISABLE);
 
 XPT2046_waitsome(intermeasurementwait);
 
  // Measure Y
 txdata = 0xD3;//no powerdown
  XPT2046_ToggleCS(ENABLE);
  HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//send command
 txdata = 0x01;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive high byte
 ftmp = rxdata;
 ftmp <<= 8;
 HAL_SPI_TransmitReceive(&hspi2, &txdata, &rxdata, 1, 10);//receive low byte
 ftmp += rxdata;
  ftmp >>= 3;
  fYraw = ftmp & 0x0FFF;
 XPT2046_waitsome(beforecslowwait);
 XPT2046_ToggleCS(DISABLE);

#11
Spottymaldoon
Super Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2010/02/05 09:25:22
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2017/04/27 06:27:54 (permalink)
0
SVW1986 - I am now where you were. If you're still following this thread I'd be grateful for an update; particularly on SPI timing that you found works best with the XPT2046. At present mine is behaving very strangely and definitely, as NKurzman says, phase is critical or otherwise the chip will hang on occasion. I'm using 32MX.
 
Purblod - I'm not familiar with those macros: whose are they please?
#12
Purblod
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2016/01/26 08:33:23
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2017/04/27 07:46:45 (permalink)
+2 (2)
Hi,
 
It has been very long since I tinkered with XPT2046 however, I dug up some code for you. Below are some parameters of the spi interface. 

Clock Frequency = 2.62MHz
Clock polarity = Low (clock idle low)
Phase = 1st edge (rising edge)
 
I have uploaded the source code to yandex. The link is below. 
https://yadi.sk/d/C1JnwcGD3HRBWS

I think the macros you have referred are part of ST's HAL library for STM32. 
#13
Spottymaldoon
Super Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2010/02/05 09:25:22
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2017/04/27 09:25:32 (permalink)
0
Purblod - this is very good of you. Thanks!
#14
Spottymaldoon
Super Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2010/02/05 09:25:22
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2017/10/06 15:24:35 (permalink)
0
I have returned to this project and finally got the touch screen working. It gives me a lot of sympathy for SVW1986 who started this thread.
 
My screen is working in 8 bit mode (good enough resolution for me) and I am controlling it with a 32MX with SPI2CON=0x260, SPI2BRG=64 (sample end; 8 bit; idle high; idle-to-active; master) these are NOT the settings in the spec sheets of the ADS7843 or its China rip-off XPT2046 - that spec is extremely misleading.
Reading those specs it looks as if all you need to do is to send the control command (eg. for X or Y) followed by a dummy and then simply read SPI2BUF and, if you need 12 bit, the four LSBs will require another 8 clocks and appropriate assembly of the MSB/LSB value. I didn't need to do this but I found that you need a total of 24 clocks to get each parameter regardless.
 
Here it is in good old MPLAB 8
 

_LATC1=0;//enable chip
SPI2BUF=0x98;//0b10011000 "Y"
while(!SPI2STATbits.SPIRBF);
SPI2BUF=0x00;//dummy
while(!SPI2STATbits.SPIRBF);
Yval =(SPI2BUF);
SPI2BUF=0x00;//dummy
while(!SPI2STATbits.SPIRBF);
Yval =(SPI2BUF);

SPI2BUF=0xD8;//0b11011000 "X"
while(!SPI2STATbits.SPIRBF);
SPI2BUF=0x00;//dummy
while(!SPI2STATbits.SPIRBF);
Xval =(SPI2BUF);
SPI2BUF=0x00;//dummy
while(!SPI2STATbits.SPIRBF);
Xval =(SPI2BUF);
_LATC1=1//disable chip

 
At one point I swapped out the XPT2046 for an ADS7843 but, as so often, the problem wasn't hardware - it took a beating though.
 
 
 
 
 
#15
Spottymaldoon
Super Member
  • Total Posts : 166
  • Reward points : 0
  • Joined: 2010/02/05 09:25:22
  • Location: 0
  • Status: offline
Re: XPT2046 problems. 2017/10/07 08:08:52 (permalink)
0
Just to add something interesting - SVW 1986 commented that he/she got some results using the following:
 
 
- I send the Y value command (0x98)
- I do a dummy read
- I send the X value command (0xD8)
- I read the Y value
- I send the X value command again (0xD8)
- I read the X value

 
Although he had some trouble with SPI setup this sequence is equivalent to the one which worked for me - i.e. 24 clock pulses need to be sent to capture each parameter - the first X command replacing my dummy and also initiating the X parameter 24 clocks later - his second X command (unnecessary) replacing my dummy. His code however saves eight clocks.
 
By the way, I am running my peripheral clock at 80MHz and in the 32MX baud rate is set differently from, say, 24FJ where you set this as part of SPIxCON - you simply put the baud rate into SPIxBRG and your SPI frequency will be PCF/2(BR + 1).
#16
Jump to:
© 2017 APG vNext Commercial Version 4.5