4 Replies Latest reply on Mar 29, 2017 1:08 PM by userc_38834

    I2C communication between PSOC 5LP and Attiny85

      Hi, I'm new here.

         

      I'm trying to establish I2C communication between my PSOC 5LP (as master) and one attiny85 (as slave) that is sending one float. I programmed attiny with arduino (library TinyWireS.h) and I want to know if it is possible to read the value via PSOC.

         

      Master code:

         


      #include <project.h>
      #define SLAVE_ADDR 0x26
      union v{
          float val;
          char8 data[5];
      } temp;
      volatile uint8 status;
      uint8 i;
      int main()
      {
          CyGlobalIntEnable; /* Enable global interrupts. */
         
          LCD_Start();
          LCD_Position(0,1);
          LCD_PrintString("Temp: ");
         
          I2C_1_Start();
               
          status = I2C_1_MasterSendStart(SLAVE_ADDR, I2C_1_READ_XFER_MODE);
         
          if (status == I2C_1_MSTR_NO_ERROR){
              CyDelay(1);
              for (i=0; i<5; i++){
                  if(i < 4) {
                      temp.data[i] = I2C_1_MasterReadByte(I2C_1_ACK_DATA);
                  }
                 
                  else temp.data[i] = I2C_1_MasterReadByte(I2C_1_NAK_DATA);
             
              }
              LCD_Position(1,1);
              LCD_PrintNumber(temp.val);
          }
         
          I2C_1_MasterSendStop();
          I2C_1_MasterClearReadBuf();
          
      }
       

         

      slave code:

         


      #include <TinyWireS.h>
      #include <OneWire.h>
      #include <DallasTemperature.h>
      #define SLAVE_ADDR 0x26
      #define ONE_WIRE_BUS 4
      OneWire oneWire(ONE_WIRE_BUS);
      DallasTemperature sensors(&oneWire);
      void setup()
      {
        sensors.begin();
        TinyWireS.begin(SLAVE_ADDR);
        TinyWireS.onRequest(requestEvent);
      }
      union v{
        float val;
        unsigned char data[4];
      } t1, t2;
      void loop()
      {
      }
      void requestEvent()
      {
        //sensors.requestTemperatures();
        t1.val = 123.45;
        TinyWireS.send (t1.data[reg_position]);
         
      }