Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

[SOLVED] [Moved] SPI communication between two PICs in MikroC (PIC18F452)

Status
Not open for further replies.

amila133

Member level 4
Joined
Oct 13, 2009
Messages
72
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Sri Lanka
Activity points
1,746
These are my successful mikroC program codes.

Pls find the attachments.

FOR Master


Code:
unsigned char count=0;
unsigned short receive;
void send_data();


void main(){
    TRISD=0x00;
    //TRISB=0xff;
    PORTD=0x00;
    ADCON1=0x06;
    TRISA=0xff;
    Spi_Init_Advanced(MASTER_OSC_DIV4,DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
/*while(1){
           if(PORTB.f0){

            count=count+1;
            Delay_ms(250);
            if(count==10){
            PORTD=count;
            
          send_data();
          }
            }


            } */
            
    while(1){
        if(PORTA.f0){

              count=count+1;
              PORTD.f0=1;
              Delay_ms(750);
              PORTD.f0=0;
              if(count==10){
                    PORTD=count;
                    Delay_ms(750);
                    send_data();
                    PORTD=0x00;

              }
              do{
          
              }while(PORTA.f0);
          }
      }
            
}

void send_data(){

     TRISC = 0x10;
     ADCON1=0x06;
     TRISA=0x00;
     TRISE=0x00;
    PORTE.F2=1;
     Delay_ms(200);

    PORTE.F2=0;
     Delay_ms(100);
     Spi_Write(count);
     while(SSPSTAT.F0==1){

      }
      receive=SSPBUF;
     // PORTE.F2=1;
      Delay_ms(200);
      count=0;
}



For Slave

Code:
void receive_data();
void send_sms();
unsigned short receive,buffer;
int i;
const int count[3]={0x31,0x30};
void main(){
      ADCON1=0x06;
      TRISD=0x00;
      TRISA=0xff;
      TRISB=0x00;
      PORTC=0x00;
      PORTD=0x00;
      PORTB=0x00;
      Spi_Init_Advanced(SLAVE_SS_ENABLE, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
      
      while(1){
            PORTB.F0=1;
            Delay_ms(500);
            PORTB.F0=0;
            receive_data();

            send_sms();
      }
}

void receive_data(){
        TRISC=0x18;
        Delay_ms(50);

        Delay_ms(50);
      //  TRISC=0x98;
        while(SSPSTAT.F0==1){

        }
        receive=Spi_Read(buffer);
        PORTD=receive;
        Delay_ms(1000);
        PORTD=0x00;
}
              
void send_sms(){
     const unsigned char para[5]="AT\r\n";
          const unsigned char textmode[12]="AT+CMGF=1\r\n"; //select pdu mode (o is given ; 1 is given for text mode)
          const unsigned char smsc[23] ="AT+CSCA=\"+947100003\"\r\n" ;    //message center no
          const unsigned char recepient[24]="AT+CMGS=\"+94712870330\"\r";
          const unsigned char message[3]="12";
       //const unsigned char message1[3] =count;
         // const unsigned char dial[17] ="ATD0712870330;\r\n";

           Usart_Init(9600);
           TRISB=0;
           PORTB=0;



        /*  for( i=0;i<10;i++){
           count++;
           }    */

                    for(i=0;i<5; i++){
                            Usart_Write(para[i]);      //type at
                            //Delay_ms(200);

                      }
                       //Usart_Write(0x0D);  //enter
                       //Usart_Write(0x0A);   //new line

                        PORTB=1;
                        Delay_ms(3000);
                        PORTB=0;                      //set textmode
                        for(i=0;i<12;i++) {

                          Usart_Write(textmode[i]);
                          //Delay_ms(200);

                        }
                        //Usart_Write(0x0D);
                        //Usart_Write(0x0A);

                              PORTB=1;
                              Delay_ms(4000);
                              PORTB=0;

                       for(i=0;i<23; i++){      //SERVICE CENTER NO
                            Usart_Write(smsc[i]);
                            //Delay_ms(200);

                      }
                       //Usart_Write(0x0D);  //enter
                       //Usart_Write(0x0A);   //new line

                              PORTB=1;
                              Delay_ms(4000);
                              PORTB=0;


                               for(i=0;i<24;i++) {
                                    //if(command1[i]=='\0') break;
                                    Usart_Write(recepient[i]);     //recepinet no
                                    //Delay_ms(50);

                                    }
                                   // Usart_Write(0x0D);  //enter
                                    Delay_ms(400);
                                   // Usart_Write(count);
                                    //Usart_Write(0x31);//1

                                    for(i=0;i<3;i++){             //send message
//                                         if(command1[i]=='\0') break;
                                            Usart_Write(count[i]);
                                            Delay_ms(50);

                                          }

                                     //Usart_Write(count);

                                     Usart_Write(0x1A);    //ctrl+z

                                      PORTB=1;
                                      Delay_ms(4000);
                                      PORTB=0;
                                           Usart_Write(0x0D);  //enter
                                           //for(i=0;i<17;i++){        //dial really starting
//                                              if(dial[i]=='\0') break;
//                                              Usart_Write(dial[i]);
//                                             // Delay_ms(200);
//
//                                           }
                                           PORTB=1;
                                           Delay_ms(1000);
                                           PORTB=0;



}
 
Last edited by a moderator:

thank you very much but you can add the schematic of the circuit
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top