Author Topic: What am i doing wrong ? ( PIC16 SPI touch screen )  (Read 834 times)

0 Members and 1 Guest are viewing this topic.

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
What am i doing wrong ? ( PIC16 SPI touch screen )
« on: August 25, 2019, 03:09:49 pm »
I try a touch screen controller ( XPT2046 ) with a PIC16F1704 ( master mode )

I see data coming in with the oscilloscope, only the PIC dont change a value.
What am i doing wrong ?

Code: [Select]
#pragma config CLKOUTEN = OFF, WDTE = OFF, PWRTE = OFF, CP = ON, BOREN = OFF, FCMEN = OFF, MCLRE = OFF, IESO = OFF, FOSC = INTOSC
#pragma config PPS1WAY = OFF, STVREN = ON, LPBOR = OFF, BORV = LO, ZCDDIS = ON, config LVP = OFF, WRT = OFF, PLLEN = ON
#define _XTAL_FREQ 32000000
#include<xc.h>
#define DACOUT DAC1CON1bits.DAC1R
void main( void )
{
TRISA = 0b001000;
TRISC = 0b101001;
RC1PPS = 0b10000; // SCK/SCL
RC2PPS = 0b10010; // SDO
SSPDATPPS = 0b10011; // RC3
DAC1CON0 = 0b10010000;
ANSELA = 0b00000;
ANSELC = 0b0000;
OSCCONbits.SCS = 0b00;
OSCCONbits.IRCF = 0b1110;
CM1CON0 = 0;
CM2CON0 = 0;
OPTION_REG = 0b10000111;
SSP1STAT = 0b00000000;
SSP1CON1 = 0b00100010;
__delay_ms( 100 );
for( ;; )ReadTouchScreen();
}
#define CS RC4
#define IRQ !RC0
unsigned char dummy,temp1,temp2;
void ReadTouchScreen()
{
if( IRQ )
 {
 CS = 0;
 
 SSPBUF = 0b10011001;
 while( !BF );
 dummy = SSPBUF;
 
 SSPBUF = 0;
 while( !BF );
 temp1 = SSPBUF;
 
 SSPBUF = 0;
 while( !BF );
 temp2 = SSPBUF;
 
 DACOUT = temp1;
 
 CS = 1;
 }
}

thank you
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2522
  • Country: us
Re: What am i doing wrong ? ( PIC16 SPI touch screen )
« Reply #1 on: August 25, 2019, 05:13:26 pm »
I read in an errata that the PIC had issues with the 'BF' bit and suggested another procedure.
I don't know if the PIC you're using has the issue (I haven't look for its errata.).
It was a different PIC from yours.

You might want to try what I have adopted for all my SPI I/O.
Code: [Select]
   uint8_t buf, tmp;

   // Read SPI byte (buf)
   PIR1bits.SSPIF = 0;
   SSPBUF = 0;
   while ( !PIR1bits.SSPIF );
   buf = SSPBUF;

   // Write SPI byte (buf)
   PIR1bits.SSPIF = 0;
   SSPBUF = buf;
   while ( !PIR1bits.SSPIF );
   tmp = SSPBUF;
« Last Edit: August 25, 2019, 05:27:31 pm by MarkF »
 
The following users thanked this post: JPortici

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Re: What am i doing wrong ? ( PIC16 SPI touch screen )
« Reply #2 on: August 26, 2019, 10:59:29 am »
Thank you its already solved :
The PPS have to be the same pin for input and output on the clock.

See this topic : https://www.microchip.com/forums/m1109944.aspx

@ Mark, it is better to use the BF flag, you dont have to clear it, it clears with reading the buffer.

I go try it now, thanks to davekw7x at microchip forum, i have been trying for 2 or 3 days.
« Last Edit: August 26, 2019, 11:02:00 am by Jan Audio »
 

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Re: What am i doing wrong ? ( PIC16 SPI touch screen )
« Reply #3 on: August 26, 2019, 02:05:28 pm »
And it works.

I keep inventing new problems, have to figure out some more about this touch screen.
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2522
  • Country: us
Re: What am i doing wrong ? ( PIC16 SPI touch screen )
« Reply #4 on: August 26, 2019, 06:50:07 pm »
I thought I mentioned that the BF flag did not work on the PIC I was using.
Something to look out for.

Glad you have it working.


Edit:  Found it.  It is the PIC18F2550 Errata paragraph #18.
    "18. Module: MSSP

        When the MSSP is configured for SPI mode, the
    Buffer Full bit, BF (SSPSTAT<0>), should not be
    polled in software to determine when the transfer
    is complete.

    Work around

        Copy the SSPSTAT register into a variable and
    perform the bit test on the variable. In Example 5,
    SSPSTAT is copied into the working register
    where the bit test is performed.

        A second option is to poll the Master Synchronous
    Serial Port Interrupt Flag bit, SSPIF (PIR1<3>).
    This bit can be polled and will set when the transfer
    is complete."
« Last Edit: August 29, 2019, 07:05:50 am by MarkF »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf