paparapapapam ne work duty cycle :(
This commit is contained in:
+64
-65
@@ -1,83 +1,82 @@
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#define PWM_SLAVE_ADDR 9
|
||||
#define SLAVE_ADDRESS 9
|
||||
|
||||
void setup() {
|
||||
Wire.begin(SLAVE_ADDRESS);
|
||||
Wire.onReceive(receiveCommand);
|
||||
Serial.begin(9600);
|
||||
Wire.begin(PWM_SLAVE_ADDR);
|
||||
Wire.onReceive(receiveEvent);
|
||||
Serial.println("PWM Controller Slave started!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Ваш код в цикле, если необходимо
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void receiveEvent(int byteCount) {
|
||||
if (Wire.available() >= 2) {
|
||||
uint16_t data = Wire.read() << 8 | Wire.read(); // Считываем данные из буфера I2C
|
||||
|
||||
uint16_t cmd = data >> 4; // Выделяем команду из принятых данных
|
||||
float value = ((float)(data & 0x0F)) / 16.0; // Выделяем значение из принятых данных
|
||||
|
||||
Serial.print("Command: ");
|
||||
Serial.print(cmd);
|
||||
Serial.print(", Value: ");
|
||||
Serial.println(value, 4);
|
||||
void receiveCommand(int byteCount) {
|
||||
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);
|
||||
}
|
||||
// Добавьте обработку других команд, если необходимо
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// uint16_t cmd_value = cmd << 12 | (uint16_t)(value * 16.0f);
|
||||
// cmd_value |= cmd;
|
||||
// i2c_start();
|
||||
// i2c_write(PWM_SLAVE_ADDR << 1);
|
||||
// i2c_write(cmd_value >> 8); // старший байт команды
|
||||
// i2c_write(cmd_value & 0xFF); // младший байт команды
|
||||
// i2c_stop();
|
||||
// Serial.print("Sent command: 0x");
|
||||
// Serial.print(cmd, HEX);
|
||||
// Serial.print(", value: ");
|
||||
// Serial.print(value);
|
||||
// Serial.print(", data bytes: 0x");
|
||||
// Serial.print(cmd_value >> 8, HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(cmd_value & 0xFF, HEX);
|
||||
void disablePWM() {
|
||||
// Ваш код для выключения ШИМ
|
||||
// Например: analogWrite(PWM_PIN, 0);
|
||||
}
|
||||
|
||||
void setPWMFrequency(float frequency) {
|
||||
// Ваш код для установки частоты ШИМ
|
||||
}
|
||||
|
||||
// void receiveEvent(int byteCount) {
|
||||
// if (byteCount > 1) {
|
||||
// uint16_t cmd_value = (Wire.read() << 8) | Wire.read();
|
||||
// uint8_t cmd = cmd_value >> 12;
|
||||
// float value = (cmd_value & 0xFFF) / 16.0;
|
||||
// Serial.print("Received command: 0x");
|
||||
// Serial.print(cmd, HEX);
|
||||
// Serial.print(", value: ");
|
||||
// Serial.print(value);
|
||||
// Serial.print(", data bytes: 0x");
|
||||
// Serial.print(cmd_value >> 8, HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(cmd_value & 0xFF, HEX);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// void receiveEvent(int byteCount) {
|
||||
// uint16_t cmd_value = 0;
|
||||
// if (Wire.available() >= 2) {
|
||||
// cmd_value = Wire.read() << 8 | Wire.read();
|
||||
// uint8_t cmd = cmd_value >> 12;
|
||||
// uint8_t value = (cmd_value & 0xFFF) / 16.0f;
|
||||
// Serial.print("Received command: 0x");
|
||||
// Serial.print(cmd, HEX);
|
||||
// Serial.print(", value: ");
|
||||
// Serial.print(value);
|
||||
// Serial.print(", data bytes: 0x");
|
||||
// Serial.print(cmd_value >> 8, HEX);
|
||||
// Serial.print(" ");
|
||||
// Serial.println(cmd_value & 0xFF, HEX);
|
||||
// }
|
||||
// }
|
||||
void setPWMDutyCycle(float dutyCycle) {
|
||||
// Ваш код для установки коэффициента заполнения ШИМ
|
||||
}
|
||||
Reference in New Issue
Block a user