View previous topic :: View next topic |
Author |
Message |
tranz
Joined: 10 Feb 2007 Posts: 78
|
SPI Problem 16F877A |
Posted: Tue Aug 05, 2008 1:15 pm |
|
|
Hi all,
I am trying to receive data from the SPI on the 16F877A via SDI pin. Although I am able to see the data being received on the pin via oscilloscope, I am not able to get the " spi_data_is_in() " to work.
Here is the simple code that I am trying to run
Code: |
#include <16F877A.h>
#include <25640.h>
#fuses HS,NOLVP,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, STREAM=HOSTPC)
#include <stdio.h>
#include "lcd2.c"
#define TRUE 1
void main()
{
int8 data;
lcd_init();
setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_CLK_DIV_16);
output_low(PIN_A1);//pic connected to DSP
output_low(PIN_A0);//DSP connected to PIC
while (1)
{
if(spi_data_is_in())
{
data=spi_read();
lcd_putc(data);
delay_ms(100);
}
}} |
The data is coming from a DSP controller board, which is set as master.
Any suggestions? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 05, 2008 1:23 pm |
|
|
The default is for \SS to be enabled on the PIC. You don't have it
disabled in your setup_spi() statement, so it is enabled. The SPI won't
work unless the \SS signal goes low during the SPI transaction.
Does the DSP have a \SS or \CS signal ? Normally an SPI master will
have one. Do you have it connected to the \SS pin on your PIC ? |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 05, 2008 1:43 pm |
|
|
Yes, the DSP controller is connected to the SS pin of the PIC, but unfortunately it selects the slave by pulling the pin high. :(
Any suggestions how to rectify the problem? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Aug 05, 2008 2:07 pm |
|
|
You could disable the slave select pin:
Quote: | setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_SS_DISABLED); |
Also, slaves don't supply a clock. They don't need a divisor. You can
remove the divisor constant as I have shown above.
I assume you have checked the SPI mode used by the DSP. Do you
know for sure that that it uses Mode 2 ? That's what you're using.
Code: |
#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) |
|
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 05, 2008 2:23 pm |
|
|
I did the necessary changes as you had mentioned, the code is still skipping the like condition "spi_data_is_in()".
Since the DSP sends the data based on MSB first and LSB last, I presumed "SPI_H_TO_L" will do the trick.
But I also tested the other modes, its still skipping the data in condition. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 05, 2008 2:48 pm |
|
|
Going to 'exceptional possibilities'. You mention data, but not the clock. spi_data_is_in, requires eight active transitions of the clock. It couldn't really care about the data...
Have you checked the voltages involved?. Both the clock line, and the data line, need to see voltages going below 1v, and above 4v (for a 5v supply). T former is 'easy', but a lot of logic outputs will not reliably supply the latter.
Best Wishes |
|
|
tranz
Joined: 10 Feb 2007 Posts: 78
|
|
Posted: Tue Aug 05, 2008 6:14 pm |
|
|
Well this is what I see,
The DSP is programmed to send a series of 1's and 0's to the PIC, the clock always seems to be high. Even in normal communication, the DSP seems to send only high as clock pulse, I think that is where the problem lies.
I will debug it further and let you guys know with the results.
Thanks |
|
|
|