I am using two pic18f4550 for SPI communication but when I use for loop for following line
for(loop=0;loop<=x;loop++)
{
spidatawrite(send[loop]);
}
then I am unable to receive data on slave side but when same line with do-while loop is implemented then I receive data on slave side. I am using mikroc compiler. My complete code is given below;
MASTER SIDE CODE:
/*MASTER CODE*/
#include
#include
sbit chipselect at RA5_bit;
sbit chipselect_Direction at TRISA5_bit;
sbit SDO at RC7_bit;
sbit SDO_Direction at TRISC7_bit;
sbit SDI at RB0_bit;
sbit SDI_Direction at TRISB0_bit;
sbit SCK at RB1_bit;
sbit SCK_Direction at TRISB1_bit;
char send[]="HI MY NAME IS UMAIR";
int x=0;
int loop;
char txt[]=" ";
void main() {
UART1_Init(9600);
delay_ms(200);
spiinitmaster();
delay_ms(100);
/*ADCON0 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON2 = 0b1111;*/
/*CMCON = 0b11111111; // Disabling the COMPARATOR module which is multiplexed with SPI pins
LVDCON = 0b11111111;
INTCON = 0b00000000;
SPPCON = 0; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins*/
ADCON0 = 0x3C; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0x0F; // Disabling the ADC module which is multiplexed with SPI pins
CMCON = 0x00; // Disabling the COMPARATOR module which is multiplexed with SPI pins
SPPCON = 0x00; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
//TRISC &= 0x7F; // clear 7th bit keeping all other bits unchanged (SDO output)
//TRISB &= 0xFD; // clear 1th bit keeping all other bits unchanged (SCK output)
//TRISA |= 0x20;
chipselect_Direction=0;
chipselect=0;
chipselect=1;
chipselect=0;
SDO_Direction=0;
SDO=0;
SDI_Direction=1;
SCK_Direction=0;
SCK=0;
SSPBUF=0;
x=strlen(send);
inttostr(x,txt);
UART1_Write_Text(txt);
for(loop=0;loop<=x;loop++)
{
spidatawrite(send[loop]);
}
HEADER FILES:
1: INITIATE MASTER:
void spiinitmaster(){
SSPSTAT = 0x00; // SMP=0(slave mode),CKE=0(transmission on Idle to active clock state), all other bits 0
SSPCON1 = 0x22;
}
2: READ DATA:
unsigned char spidataread ()
{
char dataread;
dataread = SSPBUF; // read the received data from the buffer
return dataread;
}
3:WRITE DATA:
void spidatawrite( char writedata )
{
while ( SSPSTAT.BF!=1 ){
SSPBUF = writedata; // put the data in the SSPBUF register which going to be send
}
}
SLAVE SIDE CODE:
/*SLAVE CODE*/
#include
#include
sbit chipselect at RA5_bit;
sbit chipselect_Direction at TRISA5_bit;
sbit SDO at RC7_bit;
sbit SDO_Direction at TRISC7_bit;
sbit SDI at RB0_bit;
sbit SDI_Direction at TRISB0_bit;
sbit SCK at RB1_bit;
sbit SCK_Direction at TRISB1_bit;
char readdata;
char read;
void main() {
UART1_Init(9600);
delay_ms(200);
spislaveinit();
delay_ms(200);
/*ADCON0 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON2 = 0b1111;
LVDCON= 0b11111111;
CMCON = 0b11111111; // Disabling the COMPARATOR module which is multiplexed with SPI pins
INTCON= 0b00000000;
//SPPCON = 0; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
//LATC.B3 = 1;*/
ADCON0 = 0x3C; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0x0F; // Disabling the ADC module which is multiplexed with SPI pins
CMCON = 0x00; // Disabling the COMPARATOR module which is multiplexed with SPI pins
SPPCON = 0x00; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
/*TRISC &= 0x7F; // clear 7th bit keeping all other bits unchanged (SDO output)
TRISB &= 0xFD; // clear 1th bit keeping all other bits unchanged (SCK output)
TRISA |= 0x20;*/
chipselect_Direction=1;
SDI_Direction=1;
SDO_Direction=0;
SDO=0;
SCK_Direction=1;
SSPBUF=0;
do{
readdata=spidataread();
UART1_Write(readdata);
}while(1);
}
SLAVE HEADER FILES:
1: SLAVE INITIATE:
void spislaveinit(){
SSPSTAT = 0x00; // SMP=0(slave mode),CKE=0(transmission on Idle to active clock state), all other bits 0
SSPCON1 = 0x24; // SSPEN=1(Enable SPI),SSPM[3-0]=0100(SPI Slave mode,clock=SCKpin,SSpin control enabled)
}
2: READ DATA
char spidataread ()
{
char dataread;
dataread = SSPBUF; // read the received data from the buffer
return dataread;
}
for(loop=0;loop<=x;loop++)
{
spidatawrite(send[loop]);
}
then I am unable to receive data on slave side but when same line with do-while loop is implemented then I receive data on slave side. I am using mikroc compiler. My complete code is given below;
MASTER SIDE CODE:
/*MASTER CODE*/
#include
#include
sbit chipselect at RA5_bit;
sbit chipselect_Direction at TRISA5_bit;
sbit SDO at RC7_bit;
sbit SDO_Direction at TRISC7_bit;
sbit SDI at RB0_bit;
sbit SDI_Direction at TRISB0_bit;
sbit SCK at RB1_bit;
sbit SCK_Direction at TRISB1_bit;
char send[]="HI MY NAME IS UMAIR";
int x=0;
int loop;
char txt[]=" ";
void main() {
UART1_Init(9600);
delay_ms(200);
spiinitmaster();
delay_ms(100);
/*ADCON0 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON2 = 0b1111;*/
/*CMCON = 0b11111111; // Disabling the COMPARATOR module which is multiplexed with SPI pins
LVDCON = 0b11111111;
INTCON = 0b00000000;
SPPCON = 0; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins*/
ADCON0 = 0x3C; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0x0F; // Disabling the ADC module which is multiplexed with SPI pins
CMCON = 0x00; // Disabling the COMPARATOR module which is multiplexed with SPI pins
SPPCON = 0x00; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
//TRISC &= 0x7F; // clear 7th bit keeping all other bits unchanged (SDO output)
//TRISB &= 0xFD; // clear 1th bit keeping all other bits unchanged (SCK output)
//TRISA |= 0x20;
chipselect_Direction=0;
chipselect=0;
chipselect=1;
chipselect=0;
SDO_Direction=0;
SDO=0;
SDI_Direction=1;
SCK_Direction=0;
SCK=0;
SSPBUF=0;
x=strlen(send);
inttostr(x,txt);
UART1_Write_Text(txt);
for(loop=0;loop<=x;loop++)
{
spidatawrite(send[loop]);
}
HEADER FILES:
1: INITIATE MASTER:
void spiinitmaster(){
SSPSTAT = 0x00; // SMP=0(slave mode),CKE=0(transmission on Idle to active clock state), all other bits 0
SSPCON1 = 0x22;
}
2: READ DATA:
unsigned char spidataread ()
{
char dataread;
dataread = SSPBUF; // read the received data from the buffer
return dataread;
}
3:WRITE DATA:
void spidatawrite( char writedata )
{
while ( SSPSTAT.BF!=1 ){
SSPBUF = writedata; // put the data in the SSPBUF register which going to be send
}
}
SLAVE SIDE CODE:
/*SLAVE CODE*/
#include
#include
sbit chipselect at RA5_bit;
sbit chipselect_Direction at TRISA5_bit;
sbit SDO at RC7_bit;
sbit SDO_Direction at TRISC7_bit;
sbit SDI at RB0_bit;
sbit SDI_Direction at TRISB0_bit;
sbit SCK at RB1_bit;
sbit SCK_Direction at TRISB1_bit;
char readdata;
char read;
void main() {
UART1_Init(9600);
delay_ms(200);
spislaveinit();
delay_ms(200);
/*ADCON0 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0b1111; // Disabling the ADC module which is multiplexed with SPI pins
ADCON2 = 0b1111;
LVDCON= 0b11111111;
CMCON = 0b11111111; // Disabling the COMPARATOR module which is multiplexed with SPI pins
INTCON= 0b00000000;
//SPPCON = 0; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
//LATC.B3 = 1;*/
ADCON0 = 0x3C; // Disabling the ADC module which is multiplexed with SPI pins
ADCON1 = 0x0F; // Disabling the ADC module which is multiplexed with SPI pins
CMCON = 0x00; // Disabling the COMPARATOR module which is multiplexed with SPI pins
SPPCON = 0x00; // Disabling the SERIAL PERIPHERAL CONTROL module which is multiplexed with SPI pins
/*TRISC &= 0x7F; // clear 7th bit keeping all other bits unchanged (SDO output)
TRISB &= 0xFD; // clear 1th bit keeping all other bits unchanged (SCK output)
TRISA |= 0x20;*/
chipselect_Direction=1;
SDI_Direction=1;
SDO_Direction=0;
SDO=0;
SCK_Direction=1;
SSPBUF=0;
do{
readdata=spidataread();
UART1_Write(readdata);
}while(1);
}
SLAVE HEADER FILES:
1: SLAVE INITIATE:
void spislaveinit(){
SSPSTAT = 0x00; // SMP=0(slave mode),CKE=0(transmission on Idle to active clock state), all other bits 0
SSPCON1 = 0x24; // SSPEN=1(Enable SPI),SSPM[3-0]=0100(SPI Slave mode,clock=SCKpin,SSpin control enabled)
}
2: READ DATA
char spidataread ()
{
char dataread;
dataread = SSPBUF; // read the received data from the buffer
return dataread;
}