Communicating with a ADS7843 Touchscreen Controller

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
huub8
Posts: 34
Joined: 07 Jun 2012 12:40

Communicating with a ADS7843 Touchscreen Controller

#1 Post by huub8 » 23 Jun 2012 00:44

I'm trying to communicate/get data form a ADS7843 Touchscreen Controller, but all I the data I get from it consists of zero's, so I must be doing something wrong.

The driver code is shown below:

Code: Select all

// Touch module module connections
extern sfr sbit ADS7843_CS;
extern sfr sbit  SoftSpi_SDI;
extern sfr sbit  SoftSpi_SDO;
extern sfr sbit  SoftSpi_CLK;

extern sfr sbit  ADS7843_CS_Direction;
extern sfr sbit  SoftSpi_SDI_Direction;
extern sfr sbit  SoftSpi_SDO_Direction;
extern sfr sbit  SoftSpi_CLK_Direction;
// End touch module module connections


#include "built_in.h"
unsigned int ads7843_read()
{
  char lsb, msb;
  unsigned int retval;

  ADS7843_CS = 0;
  Soft_SPI_Write(0x90);
  ADS7843_CS = 1;
  
  delay_us(2);       // Tacq delay time
  
  ADS7843_CS = 0;
  delay_us(2);       // Tacq delay time
  msb = Soft_SPI_Read(0);

  Soft_SPI_Write(0xd0);
  ADS7843_CS = 1;

  delay_us(2);       // Tacq delay time

  ADS7843_CS = 0;
  delay_us(2);       // Tacq delay time
  lsb = Soft_SPI_Read(0);
  ADS7843_CS = 1;

  hi(retval) = msb;  // Combine bytes into 16 bit word
  lo(retval) = lsb;
  
  return retval;
}
And the rest of the code function:

Code: Select all

// Touch module module connections
sbit  ADS7843_CS at latC0_bit;
sbit  SoftSpi_SDI at latC4_bit;
sbit  SoftSpi_SDO at latC2_bit;
sbit  SoftSpi_CLK at latC6_bit;

sbit  ADS7843_CS_Direction at TRISC0_bit;
sbit  SoftSpi_SDI_Direction at TRISC4_bit;
sbit  SoftSpi_SDO_Direction at TRISC2_bit;
sbit  SoftSpi_CLK_Direction at TRISC6_bit;
// End touch module module connections

void main() {
   unsigned int result;

  ADCON0 = 0x0F;           // turn off analog inputs
  ADCON1 = 0x0F;           // turn off analog inputs
  ANSELB = 0;                                    // Configure PORTB pins as digital
  ANSELC = 0;                                    // Configure PORTC pins as digital
  ANSELE = 0;                        // Configure PORTE pins as digital


    TRISA = 0;
    TRISB = 0;
    TRISD = 0;
    TRISE = 0;
    latA = 0x00;
    latB = 0x00;
    latD = 0x00;
    latE = 0x00;

   UART1_Init(38400);               // Initialize UART module at 9600 bps
   Delay_ms(100);                  // Wait for UART module to stabilize


  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);

  ADS7843_CS = 1;                       // Deselect
  ADS7843_CS_Direction = 0;             // Set CS# pin as Output
  Soft_SPI_Init();                       // Initialize Soft_SPI

while(1)
  {
   result = ads7843_read();
   IntToStr(result, txt);
   UART1_Write_Text(txt);
   delay_ms(500);
  }

}

So all I get in the usart terminal is "Start" and then a stream of zero's (even though I'm touching the panel).

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: Communicating with a ADS7843 Touchscreen Controller

#2 Post by jtemples » 23 Jun 2012 01:01

In the main loop, after you obtain the results from reading ads7843, manually assigned a number to variable "result" and see if your code will transmit that number correctly.

This allows you to know if the problem is with the usart part or with the ads7843 reading part.

If the issue is with the ads7843, read the datasheet and see if your code generates the right pulses to the controller and go from there.

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: Communicating with a ADS7843 Touchscreen Controller

#3 Post by jtemples » 23 Jun 2012 01:36

Having read the datasheet, I can say with a high degree of confidence that your code is not even remotely close to workable.

If you want to code to that chip, read its datasheet.

huub8
Posts: 34
Joined: 07 Jun 2012 12:40

Re: Communicating with a ADS7843 Touchscreen Controller

#4 Post by huub8 » 23 Jun 2012 01:38

I tested that, and the uart part seems to work fine. I based the code on a bit of code i found on the internet, but I will rewrite it myself then.

huub8
Posts: 34
Joined: 07 Jun 2012 12:40

Re: Communicating with a ADS7843 Touchscreen Controller

#5 Post by huub8 » 23 Jun 2012 01:56

I found this code:

Code: Select all

#include <18F4620.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define ADS7843_CS  PIN_C2

#define ADS7843_MODE  0x90   

//----------------------------------------
// Send the mode byte to the ADS7843 and then
// read the 12-bit result.
int16 ads7843_read(int8 mode)
{
int8 lsb, msb;
int16 retval;

output_low(ADS7843_CS);
spi_write(ADS7843_MODE);
delay_us(2);       // Tacq delay time
msb = spi_read(0);
lsb = spi_read(0);
output_high(ADS7843_CS);

retval = make16(msb, lsb);  // Combine bytes into 16 bit word
retval >>= 4;     // Right-justify the 12-bit result
return(retval);
}

//--------------------------------------
void ads7843_spi_init(void)
{
output_high(ADS7843_CS);   // Inactive state

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);

delay_ms(10);
}

//======================================
void main(void)
{
int16 result;

ads7843_spi_init();

while(1)
  {
   result = ads7843_read(ADS7843_MODE);
   printf("%LX \n\r", result);
   delay_ms(500);
  }

}
I found it here http://www.ccsinfo.com/forum/viewtopic.php?t=44786 , and converted it into C:

Code: Select all

//----------------------------------------
// read the 12-bit result.
unsigned int ads7843_read()
{
char lsb, msb;
unsigned int retval;

ADS7843_CS = 0;
Soft_SPI_Write(0x90);
delay_us(2);       // Tacq delay time
msb = Soft_SPI_Read(0);
lsb = Soft_SPI_Read(0);
ADS7843_CS = 1;

lo(retval) = msb;
hi(retval) = lsb;

return retval ;
}
It still doesn't work though. I seems right to me though, because on page 8 in the datasheet the diagram shows: 8 bit input signal (command), and then 2 8 bit output signals. The command byte (0x90) seems right too.

jtemples
Posts: 258
Joined: 22 Jan 2012 05:46

Re: Communicating with a ADS7843 Touchscreen Controller

#6 Post by jtemples » 23 Jun 2012 10:57

That looks more like it, though I would simply return (msb << 8) | lsb;

1) you should check to see if the mcu is sending out the right pulses - scope or logic analyzer.
2) you should also check to see if the controller is performing adc correctly: apply a known voltage to the adc input pins for example.

huub8
Posts: 34
Joined: 07 Jun 2012 12:40

Re: Communicating with a ADS7843 Touchscreen Controller

#7 Post by huub8 » 23 Jun 2012 14:25

I only have an analogue scope, so i can't check the spi signals. I have added a delay in the read function to test the cs signal (to check the output), which seemed to work fine. There are also signals on the other spi pins.

The only mistake i could find is that one of the uart pins is used as a spi pin, so i changed that (i deleted the uart part, and display the numbers on the lcd screen itself).

The ADS7843 is part of a lcd screen, so I think that point 2 won't be a problem.

huub8
Posts: 34
Joined: 07 Jun 2012 12:40

Re: Communicating with a ADS7843 Touchscreen Controller

#8 Post by huub8 » 23 Jun 2012 17:16

I have rewritten the code, so this is now all of the code:

Code: Select all

// Software SPI module connections
sbit SoftSpi_CS at RB0_bit;
sbit SoftSpi_SDI at RB2_bit;
sbit SoftSpi_SDO at RB4_bit;
sbit SoftSpi_CLK at RB6_bit;

sbit SoftSpi_CS_Direction  at TRISB0_bit;
sbit SoftSpi_SDI_Direction at TRISB2_bit;
sbit SoftSpi_SDO_Direction at TRISB4_bit;
sbit SoftSpi_CLK_Direction at TRISB6_bit;
// End Software SPI module connections


//----------------------------------------
// read the 12-bit result.
unsigned int ads7843_read()
{
  char lsb, msb;

  SoftSpi_CS = 0;
  Soft_SPI_Write(0x90);
  delay_us(2);       // Tacq delay time
  msb = Soft_SPI_Read(0);
  lsb = Soft_SPI_Read(0);
  SoftSpi_CS = 1;

  return (msb << 8) | lsb;
}


char txt[7];

void main() {
  ADCON1 = 0x0F;                         // Configure all ports with analog function as digital
  CMCON = 7;                            // Disable comparators

  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);

  SoftSpi_CS = 1;
  SoftSpi_CS_Direction = 0;             // Set CS# pin as Output
  Soft_SPI_Init();                       // Initialize Soft_SPI
  Delay_ms(100);
  
   while(1)
    {
     IntToStr(ads7843_read(), txt);
     UART1_Write_Text(txt);
     delay_ms(500);
    }
}
I can't find anything wrong with it, but it still doesn't give me the x coordinates I expected.

edgarmedina20
Posts: 1
Joined: 24 Apr 2013 22:31

Re: Communicating with a ADS7843 Touchscreen Controller

#9 Post by edgarmedina20 » 24 Apr 2013 22:42

is possible for someone to put a test code to the ADS7843 as examples there is not one simple just four is a library I think. perhaps one already described above. :D

thanks :)

I have this library but not sure if it will be well done.
with that its possible make a little example.

but i prefer a example with tha touch screen please :D thanks...
Attachments
TFT.rar
Library Touch Screen and TFT_LCD.. i think are good! :)
(4.38 KiB) Downloaded 313 times

ferrcol
Posts: 3
Joined: 20 Mar 2013 01:41

Re: Communicating with a ADS7843 Touchscreen Controller

#10 Post by ferrcol » 22 May 2013 02:43

edgarmedina20 wrote:is possible for someone to put a test code to the ADS7843 as examples there is not one simple just four is a library I think. perhaps one already described above. :D

thanks :)

I have this library but not sure if it will be well done.
with that its possible make a little example.

but i prefer a example with tha touch screen please :D thanks...
You tested that code? it doesn't work for me :(

Post Reply

Return to “mikroC PRO for PIC General”