void main(void)
{
   const unsigned char *mas="\r\n---------------MASTER DEVICE-----------------\r\n";

   const unsigned char * arr1 = "\r\nTaking in the text \r\n";

   const unsigned char *arr2="\r\nEnter your choice \r\n 1.Slave 1(Address:0xAA\r\n2.Slave 2(Address:0xBB)\r\n3.Slave 3(Address:0xCC\r\n";

   const unsigned char *arr3= "You have entered:\r\n";

   const unsigned char *arr4= "\r\nUART Initialised\r\n";

   const unsigned char *arr5= "\r\nI2C initialised:\r\n";

   const unsigned char *msg1="\r\nSending to Slave 1 (Address 0xAA)\r\n";

   const unsigned char *msg2="\r\nSending to Slave 2 (Address 0xBB)\r\n";

   const unsigned char *msg3="\r\nSending to Slave 3 (Address 0xCC)\r\n";

   const unsigned char *msg4="\r\nAddress sent\r\n";

   const unsigned char *msg5="\r\nData sent\r\n";

    

   const unsigned char *err="\r\nNo message will be sent since no slave no entered\r\n";
     const unsigned char *fin="\r\nClosing Communication!\r\n";

   const unsigned char *msgm="\r\nYou have entered choice number:\r\n";

   unsigned char choice;

   OSCCONbits.IRCF = 0x07;  // Configure Internal OSC for 8MHz Clock

    while(!OSCCONbits.HTS);   // Wait Until Internal Osc is Stable

    INTCON=0;   // purpose of disabling the interrupts.

    UART_Init(baud_rate);

    UART_Write_Text(mas);

    UART_Write_Text(arr4);

    delay_ms(500);

    I2C_init();

    UART_Write_Text(arr5);
     //Initialisation done

    while(1)
    {
        UART_Write_Text(arr1);

        i2c_idle();

  //receive the characters until ENTER is pressed (ASCII for ENTER = 13)

         is=UART_Read_Text();

         UART_Write_Text(arr3);

         UART_Write_Text(is);

         UART_Write_Text(arr2);

         choice=UART_Read();

         UART_Write_Text(msgm);

         UART_Write(choice);

    switch(choice)
    {
        case 0x31:
        {
            UART_Write_Text(msg1);
            
            I2C_Start();

            if(I2C_address_send())//device address
           {   
           	delay_us(20);//clock settle and then send
                I2C_Write_Text(is);
           }
            
              else
                  break;
             I2C_Stop();
              break;
        }

        case 0x32:

          {
            UART_Write_Text(msg2);

            I2C_Start();

            if(I2C_address_send1())//device address
           {  
			 delay_us(20);//clock settle and then send
                 I2C_Write_Text(is);

             }

              else
                  break;
             I2C_Stop();
              break;
        }

       case 0x33:
           {
               UART_Write_Text(msg3);
               
               I2C_Start();
              
               if(I2C_address_send2())//device address
               {  
			 delay_us(20);//clock settle and then send
                 I2C_Write_Text(is);

               }
              else
                  break;
                I2C_Stop();

              break;
        }
        default:

            UART_Write_Text(err);
            break;
    }
    //Choice entered data sent respectively to slaves now stop
    //i2c_SendAcknowledge(I2C_LAST);
    PIR1bits.SSPIF = 0;

    
    UART_Write_Text(fin);

    }

}
示例#2
0
void main(void)
{
   //const unsigned char *msg;

   const unsigned char * arr1 = "\r\ntaking in the text \r\n";

   const unsigned char *arr2="\r\nAcknowledged\r\n";

   const unsigned char *arr3= "You have entered:\r\n";

   const unsigned char *arr4= "UART Initialised\r\n";

   const unsigned char *arr5= "Sending TO Led\r\n";

   const unsigned char *arr6= "Tx of 1 byte complete\r\n";

   const unsigned char *arr7= "Tx completed all bytes\r\n";

   unsigned int j;

   unsigned char LED_Output;

  TRISB=0x00;


   OSCCONbits.IRCF = 0x07;  // Configure Internal OSC for 8MHz Clock

    while(!OSCCONbits.HTS);   // Wait Until Internal Osc is Stable

    INTCON=0;   // purpose of disabling the interrupts.

    UART_Init(baud_rate);

    UART_Write_Text(arr4);

    delay_ms(500);

    while(1)
    {


        UART_Write_Text(arr1);

         is=UART_Read_Text();
         
         UART_Write_Text(arr2);
         
         UART_Write_Text(arr3);
         
         UART_Write_Text(is);
         //sending to led
         UART_Write_Text(arr5);

       while(*is)
         {
              for(j=0;j<=7;j++)
              {
                LED_Output= (*is&0x01)==1?1:0;
                RB0=LED_Output;
                delay_ms(125);
                *is=*is>>1;
             }
              is++;
            UART_Write_Text(arr6);
        }
         UART_Write_Text(arr7);
       
     }

         
     
         


 }