Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Explain SPI Working Code, Two PIC Communication - MikroC

Status
Not open for further replies.

timcam

Newbie level 1
Joined
May 26, 2015
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
6
Reference Previous Thread: https://www.edaboard.com/thread219637.html

Hello RuH_iranga,

Thank you for posting this code. It is very helpful in understanding the PIC spi UART.

I am confused in how we can read more than one byte. Suppose we want to read 16 bits. The data sheet says the buffer holds 8 bits.

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

Ho do we get the next byte?


Regards,

Tim








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);
                     // }

             }
}
 
Last edited by a moderator:

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top