Відповісти

Введіть повідомлення і натисніть Надіслати
Параметри

Назад

Огляд теми (нові повідомленні вгорі)

0z0n
2020-12-24 14:50:06

Получилось. Пасиб!

0z0n
2020-12-24 14:33:59
Honey пише:

Wire.requestFrom(8, 16);

Пасиб огромное

А вот здесь на 16 менять не надо в операции сравнения?

   myInts1 = Wire.read ();
   myInts1 <<= 8; /// 16 здесь не надо?
   myInts1 |= Wire.read ();
Honey
2020-12-24 14:26:57

Wire.requestFrom(8, 16);

0z0n
2020-12-24 14:06:07

Помогите плиз взят пример:
Мастер прием:

#include <Wire.h>
int result1 = 2;
int result2 = 2;
int result3 = 2;
int result4 = 2;

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 8);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
   result1 = Wire.read ();
   result1 <<= 8;
   result1 |= Wire.read ();
   
   result2 = Wire.read ();
   result2 <<= 8;
   result2 |= Wire.read ();
   
   result3 = Wire.read ();
   result3 <<= 8;
   result3 |= Wire.read ();
   
   result4 = Wire.read ();
   result4 <<= 8;
   result4 |= Wire.read ();
 
      Serial.print(result1);Serial.print("  ");Serial.print(result2);Serial.print("  ");Serial.print(result3);Serial.print("  ");Serial.println(result4);
 
  }

  delay(500);
}

Слейв отдача:

#include <Wire.h>
    int value1 = -12350;  // ie. 0x04 0xD2
    int value2 = -1350; 
    int value3 = 2350;     
    int value4 = 12350; 

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
  value1 = value1 + 1 ;
  value2 = value2 + 1 ;
  value3 = value3 + 1 ;
  value4 = value4 + 1 ;
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write (highByte (value1));   
  Wire.write (lowByte (value1)); 
  
  Wire.write (highByte (value2));   
  Wire.write (lowByte (value2));

  Wire.write (highByte (value3));   
  Wire.write (lowByte (value3)); 

  Wire.write (highByte (value4));   
  Wire.write (lowByte (value4)); 
  // as expected by master
}

Переделал отдачу

for (int i = 0; i < 8; i++) {  
  Wire.write (highByte (myInts[i]));   
  Wire.write (lowByte (myInts[i])); 
}
}

Прием:

void WareReadSlave() {  // request 8 bytes from slave device #8
Wire.requestFrom(8, 8);

while (Wire.available()) { // slave may send less than requested

   myInts1 = Wire.read ();
   myInts1 <<= 8;
   myInts1 |= Wire.read ();

   myInts2 = Wire.read ();
   myInts2 <<= 8;
   myInts2 |= Wire.read ();

   myInts3 = Wire.read ();
   myInts3 <<= 8;
   myInts3 |= Wire.read ();
 
   myInts4 = Wire.read ();
   myInts4 <<= 8;
   myInts4 |= Wire.read ();
 
   myInts5 = Wire.read ();
   myInts5 <<= 8;
   myInts5 |= Wire.read ();

   myInts6 = Wire.read ();
   myInts6 <<= 8;
   myInts6 |= Wire.read ();

   myInts7 = Wire.read ();
   myInts7 <<= 8;
   myInts7 |= Wire.read ();

   myInts8 = Wire.read ();
   myInts8 <<= 8;
   myInts8 |= Wire.read ();

}  
}

В последних четырех значениях билиберда.
Как изменить код. Подскажите?

Підвал форуму