SPI Working Code! two PIC - MikroC

Status
Not open for further replies.

RuH_iranga

Junior Member level 3
Joined
Nov 4, 2010
Messages
31
Helped
7
Reputation
14
Reaction score
6
Trophy points
1,288
Location
sri lanka
Activity points
1,480
Hi all

This code is working on Proteus as well as tested on 18F452 (Master) and 16F877a (Slave).
Compiler- MikroC
I tried this one several times but it didn't work. When I changed the inbuilt "Spi_read()" command and used direct Registry reading as in the data sheet. Then it worked.

Master
Code:
// Mikroc codes for SPI master device


void main()

{    INTCON.GIE = 1;
     //ADCON1=0b00000111;
     trisc=0b00010000;
     trisd=0b00000000;
     //trisa=0b00000000;
     portd.f1=1;
     
     SSPSTAT.SMP=0;
     SSPSTAT.CKE=0;
     SSPCON.SSPEN=1;
     SSPCON.CKP=1;
     SSPCON.SSPM3=0;
     SSPCON.SSPM2=0;
     SSPCON.SSPM1=0;
     SSPCON.SSPM0=0;
     
     //Spi_Init_Advanced(MASTER_OSC_DIV4, DATA_SAMPLE_END, CLK_Idle_HIGH, LOW_2_HIGH);

     while(1)
     {
       portd.f1=0;
       SSPCON.WCOL=0;
       Spi_Write(102);
       portd.f1=1;

       portd.f2=0;
       SSPCON.WCOL=0;
       Spi_Write(254);
       delay_ms(100);
       portd.f2=1;
       portd.f2=0;
       SSPCON.WCOL=0;
       Spi_Write(1);
       delay_ms(100);
       portd.f2=1;
     }
}

Slave
Code:
// Mikroc codes for SPI slave divice
// SIMULATION SUCCEED DSN IN My Doc PLATFORM FOLDER

void main()
{
     unsigned int buffer, gdata =0;
     trisa=0b00100000;
     trisc=0b00011000;
     trisd=0b00000000;
     trisb=0b00000000;
     INTCON.GIE = 1;
     SSPSTAT.SMP=0;
     SSPSTAT.CKE=0;
     //SSPCON.SSPOV=0;
     SSPCON.SSPEN=1;
     SSPCON.CKP=1;
     SSPCON.SSPM3=0;
     SSPCON.SSPM2=1;
     SSPCON.SSPM1=0;
     SSPCON.SSPM0=0;
     //Spi_Init_Advanced(SLAVE_SS_ENABLE,DATA_SAMPLE_END,CLK_Idle_HIGH,LOW_2_HIGH);

     while(1)
             {

             if(SSPSTAT.BF)
             {
                gdata=SSPBUF;
                SSPCON.SSPOV=0;
             }



                     // gdata=Spi_Read(0);
                      if(gdata>100)
                      {
                      portb.f6=1;
                      portb.f7=0;
                      //delay_ms(20);
                      }
                      else
                      {
                      portb.f6=0;
                      portb.f7=1;
                      //delay_ms(20);
                      }

             }
}

Slave 2
Code:
// Mikroc codes for SPI slave divice
// SIMULATION SUCCEED DSN IN My Doc PLATFORM FOLDER

void main()
{
     unsigned int buffer, gdata =0;
     trisa=0b00100000;
     trisc=0b00011000;
     trisd=0b00000000;
     trisb=0b00000000;
     INTCON.GIE = 1;
     SSPSTAT.SMP=0;
     SSPSTAT.CKE=0;
     //SSPCON.SSPOV=0;
     SSPCON.SSPEN=1;
     SSPCON.CKP=1;
     SSPCON.SSPM3=0;
     SSPCON.SSPM2=1;
     SSPCON.SSPM1=0;
     SSPCON.SSPM0=0;
     //Spi_Init_Advanced(SLAVE_SS_ENABLE,DATA_SAMPLE_END,CLK_Idle_HIGH,LOW_2_HIGH);

     while(1)
             {

             if(SSPSTAT.BF)
             {
                gdata=SSPBUF;
                SSPCON.SSPOV=0;
             }



                     // gdata=Spi_Read(0);
                      //if(gdata>103)
                      //{
                      portb=gdata;
                      //portb.f6=0;
                      //portb.f7=1;
                      //delay_ms(20);
                      //}
                      //else
                      //{
                     // portb.f6=1;
                     // portb.f7=0;
                      //delay_ms(20);
                     // }

             }
}
 

please add the schematic...

Hi, I did this before several months. Slave 2 is the bottom PIC (U3).
please ask for further clarifications.




this will work!
 
thank you RuH_iranga , it's really helpful
 

Thank you very much proves it and will comment.
 

Hi
Thank you for your post but I don't understand the part where u say " I changed the inbuilt "Spi_read()" command and used direct Registry reading as in the data sheet. Then it worked"

Can you clarify that more.

Regards.
 
In place of " Spi_read() " he used

if(SSPSTAT.BF)
{
gdata=SSPBUF;
SSPCON.SSPOV=0;
}
in slave side.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…