Ive been trying really hard to get the SPI bus working on a DSPIC33EP128GM604. Ive configured SPI3 as a standard 8 bit master module and have designated a chip select pin so I can toggle slave devices.
I cannot get the chip select line to work properly, it seems as if the DSPIC does not wait for transmission to complete before toggling the chip select line from LOW to HIGH. Ive checked the Erratas and nothing seems relevant to this problem. Furthermore ive tried to swap the CS line to some other pin. Ive also tried SPI module 1, but problem persists.
Here is my code:
SPI 3 configuration:
void Init_SPI3 ( void )
{
IFS5bits.SPI3IF = 0; // Clear the Interrupt flag
IEC5bits.SPI3IE = 0; // Disable the interrupt
// SPI1CON1 Register Settings
SPI3CON1bits.DISSCK = 0; // Internal serial clock is enabled
SPI3CON1bits.DISSDO = 0; // SDOx pin is controlled by the module
SPI3CON1bits.MODE16 = 0; // Communication is word-wide (16 bits)
SPI3CON1bits.SSEN = 0; //No CS
SPI3CON1bits.MSTEN = 1; // Master mode disabled
SPI3CON1bits.PPRE = 2; //Pre-scaler
SPI3CON1bits.SPRE = 8;//Pre-scaler 2
SPI3CON1bits.SMP =1; // Input data is sampled at the middle of data output time
SPI3CON1bits.CKE =0; // Serial output data changes on transition from
SPI3CON1bits.CKP = 0; // Idle state for clock is a low level;
SPI3STATbits.SPIEN = 1; // Enable SPI module
// Interrupt Controller Settings
IFS5bits.SPI3IF = 0; // Clear the Interrupt flag
IEC5bits.SPI3IE = 1; // Enable the interrupt
}
SPI 3 Pin configuration:
TRISBbits.TRISB10 = 0; //Set SPI3CLK as output
TRISBbits.TRISB11 = 0; //Set SPI3DATAOUT as output
RPOR4bits.RP42R=32; //SPI3_CLK out
RPOR4bits.RP43R=31; //SPI3_DATA out
Chip select line pin configuration:
TRISBbits.TRISB4 = 0; //SPI3 CS is on Pin B4, set it to Output
Simple main routine:
while(1==1){
PORTBbits.RB4 = 0;//Set CS line to low
WriteSPI3(0xaa); //This is from the peripheral library to be safe....
PORTBbits.RB4 = 1; //Set CS line to high
}
But here is the output: