• AVR Freaks

AnsweredHot!SPI input wont work

Author
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
2019/08/23 08:24:10 (permalink)
0

SPI input wont work

Hi, i have a small problem with my PIC16F1704 in SPI master mode :
The SPI input gives no values, while on the oscilloscope the data should be there.
 
I have the PPS configured for :
SSPDATPPS = some pin.
 
That should be the SDI am i right ?, i see nothing else.
I have the ANSEL to zero.
 
What more can there be to not have the SPI input ?
thanks
post edited by Jan Audio - 2019/08/23 08:27:42
#1
katela
Super Member
  • Total Posts : 1382
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: SPI input wont work 2019/08/23 08:30:42 (permalink)
+3 (3)
Maybe you could share more of your code including config bits. SPI communication is more  than just pin declaration. There are other registers as well and nobody can guess how you have configured them to give you an informed advice. 

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#2
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/23 09:25:05 (permalink)
0
The SPI is working, i have data on the scope.
The SPI sends to the XP2046 touch controller, and is getting data back.
 
I will look to the config bits thanks, i dont know how that can change anything ?
If it is not solved i will get the code soon.
#3
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/24 05:59:34 (permalink)
0
Here is the 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
TRISA = 0b001000;
TRISC = 0b101001;
RC1PPS = 0b10000; // SCK/SCL
RC2PPS = 0b10010; // SDO
SSPDATPPS = 0b10011; // RC3
ANSELA = 0b00000;
ANSELC = 0b0000;
OSCCONbits.SCS = 0b00;
OSCCONbits.IRCF = 0b1110;
CM1CON0 = 0;
CM2CON0 = 0;
OPTION_REG = 0b10000111;
SSP1STAT = 0b00000000;
SSP1CON1 = 0b00100010;
#define CS RC4
#define IRQ !RC0
unsigned short tX = 0,tY = 0;
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;
}
}

#4
pcbbc
Super Member
  • Total Posts : 1252
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/24 07:22:44 (permalink)
+2 (2)
Not a complete program. The problem is almost certainly not where you think it is, otherwise you would have found it by yourself by now. Post a complete example.
#5
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/24 07:34:44 (permalink)
0
Ok the main loop is missing.
Nothing special there.
 
And why the manual dont says what SSPDATPPS is for ?, must be the SDI right ?
post edited by Jan Audio - 2019/08/24 07:36:59
#6
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/24 08:10:19 (permalink)
0
Here is the entire code as requested :

#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;
}
}

#7
davekw7x
Entropy++
  • Total Posts : 1814
  • Reward points : 0
  • Joined: 2012/01/16 12:01:07
  • Location: Second star on the right, straight on till morning
  • Status: offline
Re: SPI input wont work 2019/08/25 16:52:35 (permalink) ☼ Best Answerby Jan Audio 2019/08/26 07:03:00
+4 (4)
Jan Audio
 
RC1PPS = 0b10000; // SCK/SCL
RC2PPS = 0b10010; // SDO
SSPDATPPS = 0b10011; // RC3




Have not analyzed or tested your code, but I note a common gotcha:  In addition to PPS output assignment for SCK, you need PPS input assignment for SCK.
 
It's very easy to overlook that necessary little detail.  (Been there, done that.  Really!)
 
See Attachment: (From the MSSP section of the Data sheet.)
 
Also: In addition to SSP assignments, for PIC16F170x devices...
  • ANSEL bit = 0 for SCK and SDI. ANSEL bit is irrelevant for SDO (I usually just leave it at 1 for SDO, but it makes no difference in the SPI functionality)
  • TRIS bit = 0 for SCK and SDO. TRIS bit = 1 for SDI
 
Regards,
 
Dave
 
Footnote: As you deduced: in SPI mode, the SSPDATPPS is for SDI.  The Data Sheet might have been a little more explanatory, but you did get it right.
post edited by davekw7x - 2019/08/25 17:20:51

Attached Image(s)


Sometimes I just can't help myself...
#8
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/26 03:57:20 (permalink)
0
Ah, thank you for looking.
Now that i read that part i understand, the input and output PPS have to be at the same pin.
I go try, many thanks!, this is something i could not find, reading is one thing, understanding another.
 
I assume this also is the same for DSPIC and PIC32MX ?
thanks again, it will work now, reporting back later.
#9
ric
Super Member
  • Total Posts : 23581
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: SPI input wont work 2019/08/26 04:57:10 (permalink)
+1 (1)
Jan Audio
I assume this also is the same for DSPIC and PIC32MX ?

Not necessarily. You need to check the datasheet of each device.
 

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!
#10
Jan Audio
Starting Member
  • Total Posts : 80
  • Reward points : 0
  • Joined: 2018/09/24 08:12:24
  • Location: 0
  • Status: offline
Re: SPI input wont work 2019/08/26 07:03:44 (permalink)
+1 (1)
Thanks it works.
#11
Jump to:
© 2019 APG vNext Commercial Version 4.5