What am i doing wrong ? ( PIC16 SPI touch screen ) - Page 1
Forum section: electronics / general

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 25, 2019, 03:09:49 pm
content: 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:#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.DAC1Rvoid main( void ){TRISA = 0b001000;TRISC = 0b101001;RC1PPS = 0b10000; // SCK/SCLRC2PPS = 0b10010; // SDOSSPDATPPS = 0b10011; // RC3DAC1CON0 = 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 !RC0unsigned 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

author: MarkF
author_rank: Super Contributor
timestamp: August 25, 2019, 05:13:26 pm
content: 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: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;

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 26, 2019, 10:59:29 am
content: 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.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 26, 2019, 02:05:28 pm
content: And it works.I keep inventing new problems, have to figure out some more about this touch screen.

author: MarkF
author_rank: Super Contributor
timestamp: August 26, 2019, 06:50:07 pm
content: 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: MSSPWhen the MSSP is configured for SPI mode, theBuffer Full bit, BF (SSPSTAT<0>), should not bepolled in software to determine when the transferis complete.Work aroundCopy the SSPSTAT register into a variable andperform the bit test on the variable. In Example 5,SSPSTAT is copied into the working registerwhere the bit test is performed.A second option is to poll the Master SynchronousSerial Port Interrupt Flag bit, SSPIF (PIR1<3>).This bit can be polled and will set when the transferis complete."