pampampam
This commit is contained in:
+15
-70
@@ -1,82 +1,27 @@
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#define SLAVE_ADDRESS 9
|
||||
|
||||
void setup() {
|
||||
Wire.begin(SLAVE_ADDRESS);
|
||||
Wire.onReceive(receiveCommand);
|
||||
Wire.begin(9); // Устанавливаем адрес слейва
|
||||
Wire.onReceive(receiveEvent); // Указываем функцию для обработки приема данных
|
||||
Serial.begin(9600);
|
||||
Serial.println("PWM Controller Slave started!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Ваш код в цикле, если необходимо
|
||||
// Здесь можно добавить другие действия слейва, если необходимо
|
||||
}
|
||||
|
||||
void receiveCommand(int byteCount) {
|
||||
void receiveEvent(int numBytes) {
|
||||
while (Wire.available()) {
|
||||
byte cmd = Wire.read();
|
||||
if (cmd == 0x01) {
|
||||
// Включить ШИМ
|
||||
float dutyCycle = receiveFloat();
|
||||
enablePWM(dutyCycle);
|
||||
// Ваш код, обрабатывающий команду включения ШИМ
|
||||
Serial.print("Received command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", duty cycle: ");
|
||||
Serial.println(dutyCycle);
|
||||
} else if (cmd == 0x02) {
|
||||
// Выключить ШИМ
|
||||
disablePWM();
|
||||
// Ваш код, обрабатывающий команду выключения ШИМ
|
||||
Serial.print("Received command: 0x");
|
||||
Serial.println(cmd, HEX);
|
||||
} else if (cmd == 0x03) {
|
||||
// Установить частоту ШИМ
|
||||
float frequency = receiveFloat();
|
||||
setPWMFrequency(frequency);
|
||||
// Ваш код, обрабатывающий команду установки частоты ШИМ
|
||||
Serial.print("Received command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", frequency: ");
|
||||
Serial.println(frequency);
|
||||
} else if (cmd == 0x04) {
|
||||
// Установить коэффициент заполнения ШИМ
|
||||
float dutyCycle = receiveFloat();
|
||||
setPWMDutyCycle(dutyCycle);
|
||||
// Ваш код, обрабатывающий команду установки коэффициента заполнения ШИМ
|
||||
Serial.print("Received command: 0x");
|
||||
Serial.print(cmd, HEX);
|
||||
Serial.print(", duty cycle: ");
|
||||
Serial.println(dutyCycle);
|
||||
}
|
||||
// Добавьте обработку других команд, если необходимо
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
float receiveFloat() {
|
||||
uint8_t upperByte = Wire.read();
|
||||
uint8_t lowerByte = Wire.read();
|
||||
uint16_t cmdValue = (upperByte << 8) | lowerByte;
|
||||
float value = (float)cmdValue / 16.0;
|
||||
return value;
|
||||
}
|
||||
|
||||
void enablePWM(float dutyCycle) {
|
||||
// Ваш код для включения ШИМ
|
||||
// Например: analogWrite(PWM_PIN, dutyCycle * 255);
|
||||
}
|
||||
|
||||
void disablePWM() {
|
||||
// Ваш код для выключения ШИМ
|
||||
// Например: analogWrite(PWM_PIN, 0);
|
||||
}
|
||||
|
||||
void setPWMFrequency(float frequency) {
|
||||
// Ваш код для установки частоты ШИМ
|
||||
}
|
||||
|
||||
void setPWMDutyCycle(float dutyCycle) {
|
||||
// Ваш код для установки коэффициента заполнения ШИМ
|
||||
}
|
||||
Reference in New Issue
Block a user