Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

ADS7843 Touch Screen Controller with SPI - data problem

Status
Not open for further replies.

Micro9900

New Member
Hey guys I'm having a bit of trouble understanding the data I am receiving. I took a bit of time to do the other portions of my project and then came back to the touch screen. So far, I am able to read data upon the second pass through for x and y. The main problem is that the expected values should be up to 12 bits of data (max 4096), and instead I am reading values as high a 32,000.

I am using unsigned shot (UINT16) to read the data from the 16bit SPI receive buffer. The good news is that as I press up, down, left, or right, the values do increase/decrease (sometimes it can be glitchy and it gives me a lower value when I am expecting to read a higher one).

Datasheet for ADS7843

I am using C32, MPLAB X, and a pic32mx795F512H. Here is my code:

Code:
void Touch_Init(void)		// Initialises the touch screen
{
	Touch_CS = 1;				// Deasserts the CS line.
	Touch_PenIRQ_TRIS = 1;

        // Initialize SPI hardware module pg. 206/314 of C32 user guide
        // SPI Channel (set to 4), Set uC as Master, clock polarity cotrol, baud rate
        // Baud Rate = BR = Fpb/(2*(SPIBRG+1)), Fpb is 1:1 => 80MHz
        // Baud RAte = 80MHz/(2*(2MHz+1))=>20
        SpiChnOpen(SpiChn, SPICON_MSTEN|SPICON_CKP|SPICON_MODE16|SPICON_ON,20);

	Touch_CS = 0;
        TDelay(); //200ns

        // See pg. 6 and 9 of 18 in ADS7843 datasheet/ pg.21 of XPT2046
        // S = start bit, A=addressing-input for multiplexer, MODE=selects ADC resolution PD = power down bits for idle
        // Bits 0 -> 7 = S A2 A1 A0 MODE SFR/DFRnot PD1 PD0
	SpiChnPutC(SpiChn, 0b10010100);				// Sends the initialisation byte
        dummyReadSPI=SPI4BUF;                                       // clears the receive buffer
	Touch_CS = 1;
}

void ReadTouchXY(void)
{
    UINT16 readX, readY,sampleCount=0,avgCount=0; 
	
		Touch_PenIRQ_TRIS = 0;                          // Sets the PenIRQ to an output
		Touch_PenIRQ_LAT = 0;                           // Drives the PenIRQ low so the diode is not forward biased

		Touch_CS = 0;
                TDelay();                                       // Asserts the CS line and gives a required delay

                // Read twice to ensure data is recieved
                for(sampleCount=1;sampleCount<=6;sampleCount++)
                {
                    SpiChnPutC(SpiChn, 0b11010100);	   // Sends the control byte
                    SpiChnGetC(SpiChn);                             // Reads the dummy data to clear the receive buffer
       
                    SpiChnPutC(SpiChn, 0x00);                   // Sends a dummy byte in order to clock the data out
                    readY = SpiChnGetC(SpiChn);               // Reads Y coordinates
          
                    SpiChnPutC(SpiChn, 0b10010100);      // Sends the next control byte to read the other axis
                    readX = SpiChnGetC(SpiChn);              // Reads X coordinates
                }
                
                coordXresult=readX;
                coordYresult=readY;

             

		Touch_CS = 1;                                           // Deasserts the chip select line

		Touch_PenIRQ_TRIS = 1;					// Sets the PenIRQ to an input again

}

I also have a 200ms delay after I read to prevent the interrupt from the ADS7843 from triggering again. However, if it is held down longer it triggers the interrupt again.


Basically my questions are as follows:
_____________________________
1. Why am I getting higher values (higher than 4096) when I receive the 12bit data?
2. Is there anything that I am forgetting/not including when reading/writing to the ADS7843?

Thank you.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top