I currently have a 16F18326 programmed as a slave device and an Arduino Leonadro as the master. Everything works fine if I print the received character on the Arduino but becomes garbage if the prints are removed. Replacing the print with a 4uS delay fixes the problem again. The pic is running at 32MHz and is using the SSP interrupt to process everything. I don't understand why these delays are necessary.
Arduino send routine,
And the Pic interrupt code,
Note RA2 (Slave select) is setup to interrupt on rising edges only so I can make sure it stays in sync. I first thought that the problem may only be with the first byte due to the RA2 interrupt but it happens with all bytes. Note the pic receives all data correctly with or without the delays.
Mike.
Arduino send routine,
Code:
void sendBuff(){
uint8_t i,dummy;
SPI.beginTransaction (SPISettings (1000000, MSBFIRST, SPI_MODE0)); // 1 MHz clock, MSB first, mode 0
digitalWrite(SS,LOW);
dummy=(SPI.transfer(buff[0]));
delayMicroseconds(4);
for(i=0;i<sizeof(buff);i++){
buff[i]=(SPI.transfer(buff[i+1]));
delayMicroseconds(4);
}
Serial.println(buff);
digitalWrite(SS,HIGH);
SPI.endTransaction ();
}
And the Pic interrupt code,
Code:
void __interrupt() inter(void){
static char buffCount;
char temp;
if(SSP1IF && SSP1IE){
if(buffCount>=32){ // stop buffer overflowing
temp=SSP1BUF; //throw away
}else{
temp=SSP1BUF;
SSP1BUF=buff[buffCount];
buff[buffCount++]=temp;
}
SSP1IF=0;
}
if(IOCAF2){ //A2 is Slave Select input
IOCAF2=0; //interrupt only on falling edge
buffCount=0;
dummy=SSP1BUF; //prepare to receive data
SSP1IF=0; //clear pending interrupt
SSP1IE=1; //allow SSP to interrupt
}
}
Note RA2 (Slave select) is setup to interrupt on rising edges only so I can make sure it stays in sync. I first thought that the problem may only be with the first byte due to the RA2 interrupt but it happens with all bytes. Note the pic receives all data correctly with or without the delays.
Mike.