unsigned char * UART_Read_Text()
{
  unsigned const char *a="Keyed in \r\n";
  unsigned static char string[20];
  unsigned char x, i = 0;

//receive the characters until ENTER is pressed (ASCII for ENTER = 13)
while((x = UART_Read()) != 13)
{
 //and store the received characters into the array string[] one-by-one
string[i++] = x;
}

//insert NULL to terminate the string
string[i] = '\0';
UART_Write_Text(a);

//return the received string
return(string);
}
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);

    }

}
void main()
{
unsigned char i;

unsigned char array[80];

const char *main1="SLAVE 1(0XAA)";

const char *main2="SLAVE 2(0XBB)";

const char *main3="SLAVE 3(0XCC)";

const char *msg1="\r\nnWaiting for Master\r\n";

const char *t1="\r\n before start\r\n";

const char *t2="\r\n address detected\r\n";

const char *t3="\r\n data recieved??\r\n";

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

const unsigned char *arr2= "\r\nSlave I2C initialised:\r\n";

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

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

    UART_Init(baud_rate);

    UART_Write_Text(main3);//DISPLAYS SLAVE 1 AS THE SLAVE DEVICE

    UART_Write_Text(arr1);

    delay_ms(500);

    UART_Write_Text(msg1);

    i2c_slave_init();

     UART_Write_Text(arr2);

     while(1)
     {
         for(i=0;i<=80;i++)
         {
             array[i]=0;
         }
         i=0;

         UART_Write(datain);

         UART_Write_Text("\r\n next line\r\n");

         UART_Write_Text(array);

         UART_Write_Text("\r\n");

//start i2c routines

            i2c_start_detect();

            PIR1bits.SSPIF=0;

            i2c_address_detect();

              UART_Write_Text("Address check returns true\r\n");

            i=0;

            delay_us(50);
            do
            {
                i2c_data_detect();
            
                array[i]=datain;

                i++;
            }while(!(SSPSTATbits.P));

            UART_Write_Text("\r\nDATA READ\r\n");

            UART_Write_Text(array);
            
     }
}
Beispiel #4
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);
       
     }

         
     
         


 }
Beispiel #5
0
void interrupt high_isr (void)
{
    if ((PIR1bits.TMR1IF==1)&&(PIE1bits.TMR1IE == 1))
    {
        if (distance<25)
        {
            TRISBbits.RB3=0;
            TMR1H = 172;             // preset for timer1 MSB register
            TMR1L = 176;             // preset for timer1 LSB register
            //0 sec
        }
        else if ((distance>=25)&&(distance<40))
        {
            TRISBbits.RB3=~TRISBbits.RB3;
            TMR1H = 137;             // preset for timer1 MSB register
            TMR1L = 250;             // preset for timer1 LSB register
            // 0.1s
        }
        else if ((distance>=40)&&(distance<70))
        {
            TRISBbits.RB3=~TRISBbits.RB3;
            TMR1H = 58;             // preset for timer1 MSB register
            TMR1L = 36;             // preset for timer1 LSB register

            // 0.1s
        }
        else
        {
            j=0;
            TRISBbits.RB3=0;

            do {
                j++;
            } while (j < 16000);

            TRISBbits.RB3=1;
            TMR1H = 0;             // preset for timer1 MSB register
            TMR1L = 222;             // preset for timer1 LSB register
        }

        T1CONbits.TMR1ON  = 1;
        PIR1bits.TMR1IF = 0;    // Timer1 interrupt Flag cleared
    }

    if(RCIF) {
        if(RCREG == '²') {
            UART_Write_Text(EnterMenu);
            Mode = 0;
            index = 0;
        }
        else if(RCREG == 27 && Mode == 0) {
            Mode = 1;
            UART_Write_Text(quit);
            UART_Write_Text(Retour);
        }
        else if(RCREG == 0x0D) {
            ScrollBuffer[index] = '\0';
            index = 0;
            BufferReady = 1;
            UART_Write_Text(Retour);
        }
        else {
            BufferReady = 0;
            ScrollBuffer[index] = RCREG;
            TXREG = RCREG;
            index++;

            if(10 < index) {
                UART_Write_Text(error);
                index = 0;
            }
        }
    }
}