8.Master_I2C_PWM/slave/slave.cpp
2023-06-23 12:41:18 +03:00

27 lines
832 B
C++

#include <Wire.h>
void setup() {
Wire.begin(9); // Устанавливаем адрес слейва
Wire.onReceive(receiveEvent); // Указываем функцию для обработки приема данных
Serial.begin(9600);
}
void loop() {
// Здесь можно добавить другие действия слейва, если необходимо
}
void receiveEvent(int numBytes) {
while (Wire.available()) {
uint8_t cmd = Wire.read();
uint8_t upper_byte = Wire.read();
uint8_t lower_byte = Wire.read();
uint16_t cmd_value = (upper_byte << 8) | lower_byte;
float value = cmd_value / 16.0;
Serial.print("Received command: 0x");
Serial.print(cmd, HEX);
Serial.print(", value: ");
Serial.println(value);
}
}