Society of Robots - Robot Forum

Software => Software => Topic started by: jsmoker on October 30, 2007, 10:39:59 AM

Title: Why oh why, my SPI!
Post by: jsmoker on October 30, 2007, 10:39:59 AM
I'm new to SPI and I'm having problems getting it to work on the dsPIC30F3012.  I've included the code below.  Using an oscilloscope the clock seems to work fine, but all I get for the signal is a single short pulse, shorter than a clock pulse.  Weirder yet.  I have a LED set up to blink (like a chip status LED) and when I include the SPI code framed by the forward slashes below, the LED sops working correctly and continually pulses with an extremely short pulse (about 1/16 the length of the clock pulse).  The usual Slave Select pin is physically in a location needed for something else so I've enable the interrupt pit to turn on an output pin in it's place.  I've included the main function as well as the SPI initiation function:

(Background:  I have 2 inputs (analog 0 and 1) and the SPI is connecting to a DAC (using 16bit word).  I'm also using a UART but I have the alternate UART pins (as to not over lap with the SPI pins) but that's all moot, since the UART is commented out at this point)
 
Code: [Select]
int main( void )
{
  PORTD = 0x0001;
  PORTB = 0x03FC;
  TRISD = 0x0000;
  TRISB = 0x0003;
_LATB7 = 1;
_LATD0 = 0;
//Init_ADC();
Init_SPI();
//Init_UART(15);//56kbps = 15 = (7370000*8/4)/57600/16-1
//Init_Timer1();

while( 1 )
{
if(DelayInc == DelayLim)
{
DelayInc = 0;

if(LEDInc == LEDLim)
{
LEDInc = 0;
if(_LATD0 == 1)
_LATD0 = 0;
else
_LATD0 = 1;   
}
else
LEDInc++;


DAC_Out = LEDInc;

                                                                                                                                                                                                                                                                 
/////////////////////////////SPI Problem Code//////////////////////////

while(SPI1STATbits.SPITBF){};

_LATB7 = 0;
SPI1BUF = (2 << 12) & (DAC_Out & 0x0FFF);

while(!IFS0bits.SPI1IF){};

IFS0bits.SPI1IF = 0;
_LATB7 = 1;

//UART_Out = (DAC_Out & 0x00FF);
//Send_Dec(LEDInc);//Send_Dec((unsigned long)UART_Out);

//////////////////////////////////////////////////////////////////////////////
}
else
DelayInc++;



}
}

void Init_SPI(void)
{
IFS0bits.SPI1IF = 0;/* reset SPI 1 interrupt flag */
IEC0bits.SPI1IE = 1;/* enable SPI 1 interrupt */
SPI1CONbits.MODE16 = 1;//16 bit word
SPI1CONbits.SPRE = 0;//x8  Secondary Prescaler
SPI1CONbits.PPRE = 1;//x16 Primary Prescaler
SPI1CONbits.CKE = 0;//rising edge
SPI1CONbits.CKP = 0;
SPI1CONbits.MSTEN = 1; //master enable
SPI1STATbits.SPIEN = 1; //SPI enable
}