• AVR Freaks

Helpful ReplyHot!Correct way to make the SPI Data output line HIGH

Page: 123 > Showing page 1 of 3
Author
freddy7phil
New Member
  • Total Posts : 3
  • Reward points : 0
  • Joined: 2019/07/10 01:16:38
  • Location: 0
  • Status: offline
2019/07/16 05:33:39 (permalink)
0

Correct way to make the SPI Data output line HIGH

Hi Everyone,
I need to keep the SDO pin of the SPI 'HIGH' for a certain period of time. What is the correct way to acheive this?
#1
Aussie Susan
Super Member
  • Total Posts : 3651
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2019/07/18 19:22:52 (permalink)
0
Several ways that I can think of off the top of my head - it all depends on the context of doing this.
1) Disable the SPI module and drive the pin yourself. Depending on how the SPI module is configured, this may lead to stray pulses on the SDO line
2) Reconfigure the SPI module so that the idle state is 1. This will require that the other device also expects this.
3) If the SPI module is the slave, then set the \SS\ line to the MCU as high (which will tri-state the SDO line) and use a pull-up on the SDO line
However you have not told us anywhere near enough to know if any of these are reasonable.
Susan
#2
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2019/07/18 19:27:19 (permalink)
0
Aussie Susan
2) Reconfigure the SPI module so that the idle state is 1. This will require that the other device also expects this.

You can only do that for CLK, not DATA.
DATA just stays at the level of the most recent bit that has been sent.
Agree, we need to know more about the REAL requirement.

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!
#3
Aussie Susan
Super Member
  • Total Posts : 3651
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2019/07/18 19:29:07 (permalink)
0
Thanks Ric - brain fade!!! I just hope it is temporary!
Susan
#4
du00000001
Just Some Member
  • Total Posts : 3315
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2019/07/18 19:39:00 (permalink)
2 (1)
One might also think about disconnecting the SPI clock from the pin, then transmitting one or more 0xFF. This would set the data line to '1' - and it would remain so 'til the next transmission.
Just don't forget to reconnect the clock output prior the next 'real' transmission.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#5
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/03 21:15:02 (permalink)
0
dear all ,
1. i'm working on pic16f887 SPI communication, where i've to use only SDI; and  i've to use SDO/ portc.5  to generate a high signal for some other device (st3485).
2. As per datsheet page 180-181, SDO can be configured independently as GPIO, while using SPI., however i'm unable to do it.
2. while exploring the discussion forums i've seen your discussion which is interesting. 
so, i hope from your experience you can help me out
with regards
Nirbhow
 
 
#6
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/03 21:35:22 (permalink)
5 (1)
nirbhow
2. As per datsheet page 180-181, SDO can be configured independently as GPIO, while using SPI., however i'm unable to do it.

I guess you are misunderstanding this comment:

Any serial port function that is not desired may be overridden by programming the corresponding data direction (TRIS) register to the opposite value

So, if you want to use SDO for SPI, you program it as an output.
If you want to use it as a general purpose pin, you program it to the "opposite direction", i.e. as an input.
That means it can ONLY be used as an input, not "GPIO" when the SPI peripheral is enabled.

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!
#7
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/03 21:58:28 (permalink)
5 (1)
You will have to control the state of the SDO pin via the dummy value you write to SSPBUF to trigger the transfer.
If it is vital that you output a standalone pulse on SDO without the SCK pin toggling, then the only way you can do it (on a PIC16F887) is temporarily totally disable the SSP/SPI device to give you manual control of the pins.
You don't have the benefit of PPS (available in newer PICs) which would let you control the pin independently.
 

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!
#8
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/03 22:14:44 (permalink)
3 (1)
Ahh, I just remembered the ST3485 is just a half-duplex RS485 buffer, so I assume you want to use SDO to control the direction pin. i.e. it is unrelated to the SPI input function.
Are you regularly performing SPI input?
If yes, just toggle the dummy value written to SSPBUF between 0x00 and 0xFF, and the SDO pin will update on the next SPI transfer. Naturally that does delay the transition of the output signal. Can you just do a dummy input cycle whenever you want SDO to change?
 
 

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!
#9
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3510
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/05 08:11:21 (permalink)
0
You would code it like this: (With your register names)
SPI1BUF = x;
while (!(SPI1STATbits.SPIRBF));
SPI1BUF;



From the pic16f887 datasheet:
Any serial port function that is not desired may be overridden by programming the corresponding data direction (TRIS) register to the opposite value.
 
Looks like the sdi pin can only be inverted as output and not input.
 
Newer chips do not have those restrictions.
 
 
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#10
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/11 07:34:41 (permalink)
0
dear ric,
you have got the exact point. i'm working with half duplex usart and trying to use SDO as direction control pin.
as you have clarified my doubt, now i've decided to use some other dedicated pin for direction control. because i've already tested the code by using dedicated pin and dont want to shift the direction control to spi.
 
However i', facing another problem with spi data receive. i was abel to read the data (3 bytes) by polling. but now i'm trying to read it by interrupt. The pic is not getting interrupted.  i've used he following values to enable the interrupt
 
//initilization of interrrupts usart,spi,timer2
PIE1=0X3A;
INTCON=0XC0;
 
waiting for your response
 
 
 
#11
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/11 22:54:46 (permalink)
0
HERE is my code, i'm using mikroe platform and trying to see the results using ICD
 
code:

#define cs PORTD.F0
unsigned short ca=0,in[4]={0};
void interrupt()
{
if(PIR1.SSPIF)
{
PIR1.SSPIF=0;
in[++ca]=SSPBUF;
if(ca>=3)
{
ca=0;
cs=1;
}
}
}
void port_initialise(void)
{
TRISD=0x00;
PORTD=0x00;
}
void main(void)
{
unsigned short ar[6]={0};
char buffer;
port_initialise();
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
PIE1=0X08;
INTCON=0XC0;
delay_ms(100);
dlo
{
cs=0;
//ar[0]=SPI1_Read(buffer);
//ar[1]=SPI1_Read(buffer);
//ar[2]=SPI1_Read(buffer);
////ar[3]=SPI1_Read(buffer);
//ar[4]=SPI1_Read(buffer);
//ar[5]=SPI1_Read(buffer);
//Delay_ms(1);
//cs=1;
Delay_ms(1000);
}
while(1);
}
the microcontroller (pic16f887) is not getting interrupted.
however if i uncomment the line "ar[0]=SPI1_Read(buffer);"
the microcontroller gets interrupted.
i'm unable to find the reason
 
#12
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/11 23:25:13 (permalink) ☄ Helpfulby nirbhow 2020/01/13 23:36:03
5 (1)
I suspect you don't understand how the SPI peripheral itself works.
There is no separate read or write as with a UART.
Every single transaction is an EXCHANGE of data between the Master and the Slave. i.e. a simultaneous read and write.
The library function you are calling "SPI1_Read(buffer)" will be sending a dummy data byte to trigger each exchange, then returning the byte received.
For this reason, there's usually no advantage to using interrupts at all in the Master. If your code works using the library calls, why not continue using them?
If you DID want to use the interrupt, you still need to write a dummy byte to SSPBUF to trigger each transfer, so you'd do one to get it started, then inside the ISR, you do another straight after reading SSPBUF, except when you receive the last byte.
 
 
 

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!
#13
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/11 23:40:03 (permalink) ☄ Helpfulby nirbhow 2020/01/13 23:35:45
5 (1)
Here's the documentation for the MIKROE library function
https://download.mikroe.com/documents/compilers/mikroc/pic/help/spi_library.htm#spi1_read
Note the comment about the "buffer" parameter that you are passing to the function...

Parameters :
  • buffer: dummy data for clock generation (see device Datasheet for SPI modules implementation details)


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!
#14
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/13 19:27:56 (permalink)
0
thanks ric,
1.. i'm trying to use the interrupt because, in my complete project (which involves half  duplex usart (interrupt) for RS-485+ MAX31911(spi polling )+timer2 interrupt), it appears sometimes the microcontroller gets hanged in checking SSPSTAT.BF. so i'm thinking of avoiding polling SSPSTAT.BF and go for interrupt.
2. The second point that you clarified about data exchange (by spi) makes sense and i understood it.
3. just for my further clarification, i've a question,
a. in spi is the clock pulse generated only when data is transmitted by master?
b. without transmitting  the data from master is it not possible to receive data from slave (as in case of USART)?
c. please specify a document, to clarify my basic understanding of SPI.
 
with regards,
Nirbhow    
#15
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/13 19:45:44 (permalink) ☄ Helpfulby nirbhow 2020/01/13 23:35:17
5 (1)
nirbhow
1.. i'm trying to use the interrupt because, in my complete project (which involves half  duplex usart (interrupt) for RS-485+ MAX31911(spi polling )+timer2 interrupt), it appears sometimes the microcontroller gets hanged in checking SSPSTAT.BF. so i'm thinking of avoiding polling SSPSTAT.BF and go for interrupt.

That means there's a bug in your code, so using the interrupt won't help.
Even if you did want to use the interrupt flag, you can poll SSPIF, you don't need to trigger an actual interrupt.
 

a. in spi is the clock pulse generated only when data is transmitted by master?

Yes.
 

b. without transmitting  the data from master is it not possible to receive data from slave (as in case of USART)?

Exactly. That's what I've just been trying to explain to you. There are only EXCHANGES. There is no standalone write or read in SPI.
You've been calling library functions that pretend otherwise, but you need to be aware of what the hardware is really doing.
 

c. please specify a document, to clarify my basic understanding of SPI.

You should have everything you need in the PIC datasheet, but maybe for an alternate view, look at:
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface
 

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!
#16
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/13 23:44:17 (permalink)
0
sir,
you are right it can be a bug somewhere else, but the point is by using ICD and when the system hangs, by pausing the ICD, i found that program is always in aloop to check the SSPSTAT.BF.
 
however, i've modified the code using interrupts and i'll check the code on hardware in the evening. i'm sending the code (.c file). Kindly have alook on it, and suggest wether i'm modifying it in right way

#define cs PORTD.F0
unsigned short ca=0,in[4]={0};
void interrupt()
{
if(PIR1.SSPIF)
{
PIR1.SSPIF=0;
in[++ca]=SSPBUF;
/* for transfer of 2nd and third byte*/
if(ca<3) SSPBUF=0x00;
else
{
ca=0;
cs=1;
}
}
}
void port_initialise(void)
{
TRISD=0x00;
PORTD=0x00;
}
void main(void)
{
char buffer;
port_initialise();
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_HIGH_2_LOW);
PIE1=0X08;
INTCON=0XC0;
delay_ms(100);
do
{
cs=0;
/* for shift of first byte*/
SSPBUF=0x00;
Delay_ms(1000);
}
while(1);
}
#17
ric
Super Member
  • Total Posts : 25244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/14 01:00:28 (permalink) ☄ Helpfulby nirbhow 2020/01/14 08:31:31
5 (1)
The very first line is bad.
Change
#define cs PORTD.F0
to
#define cs LATD.F0
 
Do not use the PORTx registers for output, only input.
 
Add a "volatile" qualifier for your definition of the "ca" variable.
This is required whenever you change a variable inside an interrupt service, and access it from outside the ISR.
 

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!
#18
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/14 09:11:45 (permalink)
2 (1)
LATD is not supported in mikroe
#19
nirbhow
New Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2009/10/18 03:45:26
  • Location: 0
  • Status: offline
Re: Correct way to make the SPI Data output line HIGH 2020/01/14 09:22:31 (permalink)
0
the code is working. Thanks a lot ric. now i'll integrate this program in complete project and discuss the results with you soon.
once again thanks 
 
#20
Page: 123 > Showing page 1 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5