From ae7b4fffba764107c3a5c4fab6df893457bae647 Mon Sep 17 00:00:00 2001 From: zloihach <72695485+zloihach@users.noreply.github.com> Date: Wed, 21 Jun 2023 17:22:44 +0300 Subject: [PATCH] added working ino scripts --- .gitignore | 327 +++++++++++++++++++++- .idea/.gitignore | 8 - .idea/modules.xml | 8 - .idea/pmw.iml | 2 - .idea/vcs.xml | 6 - CMakeLists.txt | 13 - i2c.c | 48 ---- main.c | 99 ------- main.cpp | 211 +++++++++++++++ main2.cpp | 183 +++++++++++++ pwm.c | 29 -- slave/slave.cpp | 83 ++++++ test.cpp | 5 - test.h | 678 ---------------------------------------------- 14 files changed, 803 insertions(+), 897 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/modules.xml delete mode 100644 .idea/pmw.iml delete mode 100644 .idea/vcs.xml delete mode 100644 CMakeLists.txt delete mode 100644 i2c.c delete mode 100644 main.c create mode 100644 main.cpp create mode 100644 main2.cpp delete mode 100644 pwm.c create mode 100644 slave/slave.cpp delete mode 100644 test.cpp delete mode 100644 test.h diff --git a/.gitignore b/.gitignore index d50fdb7..cf5045a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -### JetBrains template +### CLion+iml template # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 @@ -77,6 +77,173 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +### JetBrains+all template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### C++ template +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +### C template +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + ### CLion+all template # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 @@ -156,3 +323,161 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +### CLion template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### JetBrains+iml template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 932074b..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/pmw.iml b/.idea/pmw.iml deleted file mode 100644 index f08604b..0000000 --- a/.idea/pmw.iml +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 6a6f9a8..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,13 +0,0 @@ -cmake_minimum_required(VERSION 3.22) -project(untitled2 C) - -set(CMAKE_C_STANDARD 17) - -set(CMAKE_SYSTEM_NAME Linux) -set(CMAKE_SYSTEM_PROCESSOR avr) -set(CMAKE_C_COMPILER avr-gcc) -set(CMAKE_CXX_COMPILER avr-g++) -set(CMAKE_C_FLAGS "-mmcu=atmega328p") -set(CMAKE_CXX_FLAGS "-mmcu=atmega328p") - -add_executable(8.PMW main.c i2c.h pwm.h) diff --git a/i2c.c b/i2c.c deleted file mode 100644 index e3a3322..0000000 --- a/i2c.c +++ /dev/null @@ -1,48 +0,0 @@ -#include "i2c.h" - -#define F_CPU 16000000UL -#define F_SCL 100000UL - -#define I2C_READ 1 -#define I2C_WRITE 0 - -#define SCL_CLOCK ((F_CPU/F_SCL)-16)/2 - -void i2c_init(void) -{ - TWBR = (uint8_t)SCL_CLOCK; -} - -void i2c_start(void) -{ - // transmit START condition - TWCR = (1< -#include "i2c.h" -#include "pwm.h" - -#define I2C_ADDRESS 0x20 - -void send_data(void) -{ - // read state of PWM from master and send to slave - i2c_start(); - i2c_write_byte(I2C_ADDRESS | I2C_WRITE); - i2c_write_byte(0x00); - i2c_start(); - i2c_write_byte(I2C_ADDRESS | I2C_READ); - uint8_t state = i2c_read_byte(0); - i2c_stop(); - // set PWM state - if (state) { - TCCR1B |= (1< 1.0) ? 1.0 : frequency; - set_frequency(frequency); -} - -void decrease_frequency(void) -{ - // decrease PWM frequency by 20% - float frequency = ICR1 * 0.8 / PWM_MAX_FREQ; - set_frequency(frequency); -} - -void set_duty_cycle(float duty_cycle) -{ - // set PWM duty cycle - pwm_set_duty_cycle(duty_cycle); -} - -void increase_duty_cycle(void) -{ - // increase PWM duty cycle by 10% - float duty_cycle = OCR1A * 1.1 / ICR1; - duty_cycle = (duty_cycle > 1.0) ? 1.0 : duty_cycle; - set_duty_cycle(duty_cycle); -} - -void decrease_duty_cycle(void) -{ - // decrease PWM duty cycle by 10% - float duty_cycle = OCR1A * 0.9 / ICR1; - set_duty_cycle(duty_cycle); -} - -int main(void) -{ - // initialize I2C and PWM - i2c_init(); - pwm_init(); - // set default PWM frequency and duty cycle - set_frequency(1000); - set_duty_cycle(0.5); - // main loop - while (1) { - send_data(); - // read buttons and execute actions - if (!(PIND & (1< + + +void pwm_init() { + TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non-inverting mode, Fast PWM (mode 14) + TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM (mode 14), prescaler = 1 + DDRB |= (1 << PB1); // set pin PB1 as output +} + +void pwm_set_frequency(uint16_t frequency) { + uint16_t prescaler = 1; + uint32_t top = F_CPU / (prescaler * frequency) - 1; + ICR1 = top; +} + +void pwm_set_duty_cycle(uint8_t dutyCycle) { + uint16_t value = ICR1 * dutyCycle / 100.0; + OCR1A = value; +} + +void pwm_enable() { + pwm_set_duty_cycle(50); // Начальная скважность +} + +void pwm_disable() { + pwm_set_duty_cycle(0); // Выключение ШИМ +} + +void pwm_init(); +void pwm_set_frequency(uint16_t frequency); +void pwm_set_duty_cycle(uint8_t dutyCycle); +void pwm_enable(); +void pwm_disable(); + +#define F_CPU 16000000UL +#define I2C_FREQ 100000UL +#define I2C_PRESCALER 1 +#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) + +void i2c_init() { + TWBR = I2C_BITRATE; +} + +void i2c_start() { + // отправляем START bit + TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); + + // ожидаем пока START bit будет успешно отправлен + while (!(TWCR & (1 << TWINT))); +} + +void i2c_stop() { + // отправляем STOP bit + TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); + + // ожидаем пока STOP bit будет успешно отправлен + while (TWCR & (1 << TWSTO)); +} + +void i2c_write(uint8_t data) { + // загружаем данные в регистр TWDR + TWDR = data; + + // отправляем данные + TWCR = (1 << TWINT) | (1 << TWEN); + + // ожидаем пока данные будут успешно отправлены + while (!(TWCR & (1 << TWINT))); +} + +uint8_t i2c_read_ack() { + // разрешаем отправку ACK после прочтения байта + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); + + // ожидаем пока данные будут успешно прочитаны + while (!(TWCR & (1 << TWINT))); + + // возвращаем прочитанный байт + return TWDR; +} + +uint8_t i2c_read_nack() { + // запрещаем отправку ACK после прочтения байта + TWCR = (1 << TWINT) | (1 << TWEN); + + // ожидаем пока данные будут успешно прочитаны + while (!(TWCR & (1 << TWINT))); + + // возвращаем прочитанный байт + return TWDR; +} + +void i2c_init(); +void i2c_start(); +void i2c_stop(); +void i2c_write(uint8_t data); +uint8_t i2c_read_ack(); +uint8_t i2c_read_nack(); + +const uint8_t BUTTON_PIN[] = {0, 1, 2, 3, 4, 5}; +const uint8_t PWM_SLAVE_ADDR = 9; + +uint16_t command = 0x00; +float frequency = 1000.0; +float dutyCycle = 50.0; + +void setup() { + i2c_init(); + pwm_init(); + pwm_enable(); // Включение ШИМ + + // Инициализируем пины кнопок + DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); // подключены кнопки на пинах А0, А1, А2, А3, А4, А5 + PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); // включение подтягивающего резистора + + Serial.begin(9600); // Инициализируем Serial монитор + Serial.println("PWM Controller started!"); + sendCommand(0x01, 0.0); // Включить ШИМ при запуске +} + +void loop() { + // Обработка нажатий на кнопки + checkButton(0, "Turn on PWM!", 0x01, 0.0); + checkButton(1, "Turn off PWM!", 0x02, 0.0); + checkButton(2, "Increase frequency!", 0x03, 1.25); + checkButton(3, "Decrease frequency!", 0x04, 0.8); + checkButton(4, "Increase duty cycle!", 0x05, 1.1); + checkButton(5, "Decrease duty cycle!", 0x06, 0.9); +} + +void checkButton(uint8_t pin, const char* message, uint16_t cmd, float value) { + if (bit_is_clear(PINC, pin)) { + Serial.println(message); + delay(500); + sendCommand(cmd, value); + if (cmd == 0x03) { + frequency *= value; + pwm_set_frequency(frequency); + } else if (cmd == 0x04) { + frequency *= value; + pwm_set_frequency(frequency); + } else if (cmd == 0x05) { + dutyCycle *= value; + pwm_set_duty_cycle(dutyCycle); + } else if (cmd == 0x06) { + dutyCycle *= value; + pwm_set_duty_cycle(dutyCycle); + } else if (cmd == 0x02) { + pwm_disable(); // Выключение ШИМ + } else if (cmd == 0x01) { + pwm_enable(); // Включение ШИМ + } + command = cmd; + } +} + +void sendCommand(uint16_t cmd, float value) { + if (cmd != 0x01 && cmd != 0x02) { // если команда изменения параметров + switch (cmd) { + case 0x03: // Увеличить частоту на 25% от текущего. + value = frequency; + value *= 0.25; + break; + case 0x04: // Уменьшить частоту на 20% от текущего. + value = frequency; + value *= 0.2; + break; + case 0x05: // Увеличить скважность на 10% от текущего. + value = dutyCycle; + value *= 0.1; + break; + case 0x06: // Уменьшить скважность на 10% от текущего. + value = dutyCycle; + value *= 0.1; + break; + } + } + // uint16_t data = (uint16_t)(value * 16.0); + // data |= cmd << 4; + // i2c_start(); + // i2c_write(PWM_SLAVE_ADDR << 1); + // i2c_write(data >> 8); + // i2c_write(data & 0xFF); + // i2c_stop(); + // Serial.print("Sent command: "); + // Serial.print(cmd, HEX); + // Serial.print(", value: "); + // Serial.println(value); + + + 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); + +} + + + diff --git a/main2.cpp b/main2.cpp new file mode 100644 index 0000000..b6b3860 --- /dev/null +++ b/main2.cpp @@ -0,0 +1,183 @@ +#include +#include +#include + + + +#define I2C_FREQ 100000UL +#define F_CPU 16000000UL +#define I2C_PRESCALER 1 +#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) +#define I2C_READ 1 +#define I2C_WRITE 0 + +void i2c_init() { + TWBR = I2C_BITRATE; +} + +void i2c_start() { + // отправляем START bit + TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); + + // ожидаем пока START bit будет успешно отправлен + while (!(TWCR & (1 << TWINT))); +} + +void i2c_stop() { + // отправляем STOP bit + TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); + + // ожидаем пока STOP bit будет успешно отправлен + while (TWCR & (1 << TWSTO)); +} + +void i2c_write(uint8_t address, const uint8_t* data, uint8_t length) { + i2c_start(); + // Отправляем адрес устройства с битом записи + TWDR = address << 1 | I2C_WRITE; + TWCR = (1 << TWINT) | (1 << TWEN); + while (!(TWCR & (1 << TWINT))); + + for (uint8_t i = 0; i < length; i++) { + TWDR = data[i]; + TWCR = (1 << TWINT) | (1 << TWEN); + while (!(TWCR & (1 << TWINT))); + } + + i2c_stop(); +} + +void i2c_read(uint8_t address, uint8_t* data, uint8_t length) { + i2c_start(); + + // Отправляем адрес устройства с битом чтения + TWDR = address << 1 | I2C_READ; + TWCR = (1 << TWINT) | (1 << TWEN); + while (!(TWCR & (1 << TWINT))); + + for (uint8_t i = 0; i < length; i++) { + // для всех байт, кроме последнего - отправляем ACK + if (i < length - 1) { + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); + while (!(TWCR & (1 << TWINT))); + data[i] = TWDR; + } + // для последнего байта - отправляем NACK + else { + TWCR = (1 << TWINT) | (1 << TWEN); + while (!(TWCR & (1 << TWINT))); + data[i] = TWDR; + } + } + + i2c_stop(); +} + + + +#define F_CPU 16000000UL + +void pwm_init() { + TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non-inverting mode, Fast PWM (mode 14) + TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM (mode 14), prescaler = 1 + DDRB |= (1 << PB1); // set pin PB1 as output +} + +void pwm_set_frequency(uint16_t frequency) { + uint16_t prescaler = 1; + uint32_t top = F_CPU / (prescaler * frequency) - 1; + ICR1 = top; +} + +void pwm_set_duty_cycle(float dutyCycle) { + uint16_t value = ICR1 * dutyCycle / 100.0; + OCR1A = value; +} + +void pwm_enable() { + pwm_set_duty_cycle(50); // Начальная скважность +} + +void pwm_disable() { + pwm_set_duty_cycle(0); // Выключение ШИМ +} + +void i2c_init(); +void i2c_start(); +void i2c_stop(); +void i2c_write(uint8_t address, const uint8_t* data, uint8_t length); +void i2c_read(uint8_t address, uint8_t* data, uint8_t length); + + +void pwm_init(); +void pwm_set_frequency(uint16_t frequency); +void pwm_set_duty_cycle(float dutyCycle); +void pwm_enable(); +void pwm_disable(); + +const uint8_t BUTTON_PIN[] = {0, 1, 2, 3, 4, 5}; +const uint8_t PWM_SLAVE_ADDR = 9; + +uint16_t command = 0x00; +float frequency = 1000.0; +float dutyCycle = 50.0; + +void setup() { + i2c_init(); + pwm_init(); + pwm_enable(); // Включение ШИМ + + + // Инициализируем пины кнопок + DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); // подключены кнопки на пинах А0, А1, А2, А3, А4, А5 + PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); // включение подтягивающего резистора +} + +void loop() { + // Обработка нажатий на кнопки + checkButton(0, 0x01, 0.0); + checkButton(1, 0x02, 0.0); + checkButton(2, 0x03, 1.25); + checkButton(3, 0x04, 0.8); + checkButton(4, 0x05, 1.1); + checkButton(5, 0x06, 0.9); +} + +void checkButton(uint8_t pin, uint16_t cmd, float value) { + if (bit_is_clear(PINC, pin)) { + sendCommand(cmd, value); + if (cmd == 0x03) { + frequency *= value; + pwm_set_frequency(frequency); + } else if (cmd == 0x04) { + frequency *= value; + pwm_set_frequency(frequency); + } else if (cmd == 0x05) { + dutyCycle *= value; + pwm_set_duty_cycle(dutyCycle); + } else if (cmd == 0x06) { + dutyCycle *= value; + pwm_set_duty_cycle(dutyCycle); + } else if (cmd == 0x02) { + pwm_disable(); // Выключение ШИМ + } else if (cmd == 0x01) { + pwm_enable(); // Включение ШИМ + } + command = cmd; + _delay_ms(500); // Задержка для антидребезга + } +} + +void sendCommand(uint16_t cmd, float value) { + uint16_t data = (uint16_t)(value * 16.0); + data |= cmd << 4; + uint8_t buffer[2]; + buffer[0] = data >> 8; + buffer[1] = data & 0xFF; + i2c_write(PWM_SLAVE_ADDR, buffer, 2); + + Serial.print("Command: "); + Serial.print(cmd); + Serial.print(", Value: "); + Serial.println(value, 4); +} \ No newline at end of file diff --git a/pwm.c b/pwm.c deleted file mode 100644 index 31dc91c..0000000 --- a/pwm.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "pwm.h" - -void pwm_init(void) -{ - // initialize timer 1 for PWM - TCCR1A = (1< 0xffff) ? 0xffff : top; - // set timer 1 TOP value - ICR1 = top; -} - -void pwm_set_duty_cycle(float duty_cycle) -{ - // calculate OCR1A value from duty cycle - uint16_t ocr = (uint16_t)(duty_cycle * ICR1); - ocr = (ocr > ICR1) ? ICR1 : ocr; - // set OCR1A value - OCR1A = ocr; -} \ No newline at end of file diff --git a/slave/slave.cpp b/slave/slave.cpp new file mode 100644 index 0000000..37cd979 --- /dev/null +++ b/slave/slave.cpp @@ -0,0 +1,83 @@ + +#include + +#define PWM_SLAVE_ADDR 9 + +void setup() { + Serial.begin(9600); + Wire.begin(PWM_SLAVE_ADDR); + Wire.onReceive(receiveEvent); +} + +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); + } +} + + + +// 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 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); +// } +// } diff --git a/test.cpp b/test.cpp deleted file mode 100644 index d53eaa1..0000000 --- a/test.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by FSB-PC on 21.06.2023. -// - -#include "test.h" diff --git a/test.h b/test.h deleted file mode 100644 index d897af8..0000000 --- a/test.h +++ /dev/null @@ -1,678 +0,0 @@ -// #include - - -// void pwm_init() { -// TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non-inverting mode, Fast PWM (mode 14) -// TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM (mode 14), prescaler = 1 -// DDRB |= (1 << PB1); // set pin PB1 as output -// } - -// void pwm_set_frequency(uint16_t frequency) { -// uint16_t prescaler = 1; -// uint32_t top = F_CPU / (prescaler * frequency) - 1; -// ICR1 = top; -// } - -// void pwm_set_duty_cycle(uint8_t dutyCycle) { -// uint16_t value = ICR1 * dutyCycle / 100.0; -// OCR1A = value; -// } - - -// void pwm_init(); -// void pwm_set_frequency(uint16_t frequency); -// void pwm_set_duty_cycle(uint8_t dutyCycle); - -// #define F_CPU 16000000UL -// #define I2C_FREQ 100000UL -// #define I2C_PRESCALER 1 -// #define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) - -// void i2c_init() { -// TWBR = I2C_BITRATE; -// } - -// void i2c_start() { -// // отправляем START bit -// TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); - -// // ожидаем пока START bit будет успешно отправлен -// while (!(TWCR & (1 << TWINT))); -// } - -// void i2c_stop() { -// // отправляем STOP bit -// TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); - -// // ожидаем пока STOP bit будет успешно отправлен -// while (TWCR & (1 << TWSTO)); -// } - -// void i2c_write(uint8_t data) { -// // загружаем данные в регистр TWDR -// TWDR = data; - -// // отправляем данные -// TWCR = (1 << TWINT) | (1 << TWEN); - -// // ожидаем пока данные будут успешно отправлены -// while (!(TWCR & (1 << TWINT))); -// } - -// uint8_t i2c_read_ack() { -// // разрешаем отправку ACK после прочтения байта -// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); - -// // ожидаем пока данные будут успешно прочитаны -// while (!(TWCR & (1 << TWINT))); - -// // возвращаем прочитанный байт -// return TWDR; -// } - -// uint8_t i2c_read_nack() { -// // запрещаем отправку ACK после прочтения байта -// TWCR = (1 << TWINT) | (1 << TWEN); - -// // ожидаем пока данные будут успешно прочитаны -// while (!(TWCR & (1 << TWINT))); - -// // возвращаем прочитанный байт -// return TWDR; -// } - -// void i2c_init(); -// void i2c_start(); -// void i2c_stop(); -// void i2c_write(uint8_t data); -// uint8_t i2c_read_ack(); -// uint8_t i2c_read_nack(); - -// const uint8_t BUTTON_PIN[] = {0, 1, 2, 3, 4, 5}; -// const uint8_t PWM_SLAVE_ADDR = 9; - -// uint16_t command = 0x00; -// float frequency = 1000.0; -// float dutyCycle = 50.0; - -// void setup() { -// i2c_init(); -// pwm_init(); - -// // Инициализируем пины кнопок -// DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); // подключены кнопки на пинах А0, А1, А2, А3, А4, А5 -// PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); // включение подтягивающего резистора - -// Serial.begin(9600); // Инициализируем Serial монитор -// Serial.println("PWM Controller started!"); -// sendCommand(0x01, 0.0); // Включить ШИМ при запуске -// } - -// void loop() { -// // Обработка нажатий на кнопки -// checkButton(0, "Turn on PWM!", 0x01, 0.0); -// checkButton(1, "Turn off PWM!", 0x02, 0.0); -// checkButton(2, "Increase frequency!", 0x03, 1.25); -// checkButton(3, "Decrease frequency!", 0x04, 0.8); -// checkButton(4, "Increase duty cycle!", 0x05, 1.1); -// checkButton(5, "Decrease duty cycle!", 0x06, 0.9); -// } - -// void checkButton(uint8_t pin, const char* message, uint16_t cmd, float value) { -// if (bit_is_clear(PINC, pin)) { -// Serial.println(message); -// delay(500); -// sendCommand(cmd, value); -// if (cmd == 0x03) { -// frequency *= value; -// pwm_set_frequency(frequency); -// } else if (cmd == 0x04) { -// frequency *= value; -// pwm_set_frequency(frequency); -// } else if (cmd == 0x05) { -// dutyCycle *= value; -// pwm_set_duty_cycle(dutyCycle); -// } else if (cmd == 0x06) { -// dutyCycle *= value; -// pwm_set_duty_cycle(dutyCycle); -// } else if (cmd == 0x02) { -// pwm_set_duty_cycle(0); -// } else if (cmd == 0x01) { -// pwm_set_duty_cycle(dutyCycle); -// } -// command = cmd; -// } -// } - -// void sendCommand(uint16_t cmd, float value) { -// if (cmd != 0x01 && cmd != 0x02) { // если команда изменения параметров -// switch (cmd) { -// case 0x03: // Увеличить частоту на 25% от текущего. -// value = frequency; -// value *= 0.25; -// break; -// case 0x04: // Уменьшить частоту на 20% от текущего. -// value = frequency; -// value *= 0.2; -// break; -// case 0x05: // Увеличить скважность на 10% от текущего. -// value = dutyCycle; -// value *= 0.1; -// break; -// case 0x06: // Уменьшить скважность на 10% от текущего. -// value = dutyCycle; -// value *= 0.1; -// break; -// } -// } -// uint16_t data = (uint16_t)(value * 16.0); -// data |= cmd << 4; -// i2c_start(); -// i2c_write(PWM_SLAVE_ADDR << 1); -// i2c_write(data >> 8); -// i2c_write(data & 0xFF); -// i2c_stop(); -// Serial.print("Sent command: "); -// Serial.print(cmd, HEX); -// Serial.print(", value: "); -// Serial.println(value); -// } - - - - - - -//VERSION #2 -#include - - -void pwm_init() { - TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non-inverting mode, Fast PWM (mode 14) - TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM (mode 14), prescaler = 1 - DDRB |= (1 << PB1); // set pin PB1 as output -} - -void pwm_set_frequency(uint16_t frequency) { - uint16_t prescaler = 1; - uint32_t top = F_CPU / (prescaler * frequency) - 1; - ICR1 = top; -} - -void pwm_set_duty_cycle(uint8_t dutyCycle) { - uint16_t value = ICR1 * dutyCycle / 100.0; - OCR1A = value; -} - -void pwm_enable() { - pwm_set_duty_cycle(50); // Начальная скважность -} - -void pwm_disable() { - pwm_set_duty_cycle(0); // Выключение ШИМ -} - -void pwm_init(); -void pwm_set_frequency(uint16_t frequency); -void pwm_set_duty_cycle(uint8_t dutyCycle); -void pwm_enable(); -void pwm_disable(); - -#define F_CPU 16000000UL -#define I2C_FREQ 100000UL -#define I2C_PRESCALER 1 -#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) - -void i2c_init() { - TWBR = I2C_BITRATE; -} - -void i2c_start() { - // отправляем START bit - TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); - - // ожидаем пока START bit будет успешно отправлен - while (!(TWCR & (1 << TWINT))); -} - -void i2c_stop() { - // отправляем STOP bit - TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); - - // ожидаем пока STOP bit будет успешно отправлен - while (TWCR & (1 << TWSTO)); -} - -void i2c_write(uint8_t data) { - // загружаем данные в регистр TWDR - TWDR = data; - - // отправляем данные - TWCR = (1 << TWINT) | (1 << TWEN); - - // ожидаем пока данные будут успешно отправлены - while (!(TWCR & (1 << TWINT))); -} - -uint8_t i2c_read_ack() { - // разрешаем отправку ACK после прочтения байта - TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); - - // ожидаем пока данные будут успешно прочитаны - while (!(TWCR & (1 << TWINT))); - - // возвращаем прочитанный байт - return TWDR; -} - -uint8_t i2c_read_nack() { - // запрещаем отправку ACK после прочтения байта - TWCR = (1 << TWINT) | (1 << TWEN); - - // ожидаем пока данные будут успешно прочитаны - while (!(TWCR & (1 << TWINT))); - - // возвращаем прочитанный байт - return TWDR; -} - -void i2c_init(); -void i2c_start(); -void i2c_stop(); -void i2c_write(uint8_t data); -uint8_t i2c_read_ack(); -uint8_t i2c_read_nack(); - -const uint8_t BUTTON_PIN[] = {0, 1, 2, 3, 4, 5}; -const uint8_t PWM_SLAVE_ADDR = 9; - -uint16_t command = 0x00; -float frequency = 1000.0; -float dutyCycle = 50.0; - -void setup() { - i2c_init(); - pwm_init(); - pwm_enable(); // Включение ШИМ - - // Инициализируем пины кнопок - DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); // подключены кнопки на пинах А0, А1, А2, А3, А4, А5 - PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); // включение подтягивающего резистора - - Serial.begin(9600); // Инициализируем Serial монитор - Serial.println("PWM Controller started!"); - sendCommand(0x01, 0.0); // Включить ШИМ при запуске -} - -void loop() { - // Обработка нажатий на кнопки - checkButton(0, "Turn on PWM!", 0x01, 0.0); - checkButton(1, "Turn off PWM!", 0x02, 0.0); - checkButton(2, "Increase frequency!", 0x03, 1.25); - checkButton(3, "Decrease frequency!", 0x04, 0.8); - checkButton(4, "Increase duty cycle!", 0x05, 1.1); - checkButton(5, "Decrease duty cycle!", 0x06, 0.9); -} - -void checkButton(uint8_t pin, const char* message, uint16_t cmd, float value) { - if (bit_is_clear(PINC, pin)) { - Serial.println(message); - delay(500); - sendCommand(cmd, value); - if (cmd == 0x03) { - frequency *= value; - pwm_set_frequency(frequency); - } else if (cmd == 0x04) { - frequency *= value; - pwm_set_frequency(frequency); - } else if (cmd == 0x05) { - dutyCycle *= value; - pwm_set_duty_cycle(dutyCycle); - } else if (cmd == 0x06) { - dutyCycle *= value; - pwm_set_duty_cycle(dutyCycle); - } else if (cmd == 0x02) { - pwm_disable(); // Выключение ШИМ - } else if (cmd == 0x01) { - pwm_enable(); // Включение ШИМ - } - command = cmd; - } -} - -void sendCommand(uint16_t cmd, float value) { - if (cmd != 0x01 && cmd != 0x02) { // если команда изменения параметров - switch (cmd) { - case 0x03: // Увеличить частоту на 25% от текущего. - value = frequency; - value *= 0.25; - break; - case 0x04: // Уменьшить частоту на 20% от текущего. - value = frequency; - value *= 0.2; - break; - case 0x05: // Увеличить скважность на 10% от текущего. - value = dutyCycle; - value *= 0.1; - break; - case 0x06: // Уменьшить скважность на 10% от текущего. - value = dutyCycle; - value *= 0.1; - break; - } - } - uint16_t data = (uint16_t)(value * 16.0); - data |= cmd << 4; - i2c_start(); - i2c_write(PWM_SLAVE_ADDR << 1); - i2c_write(data >> 8); - i2c_write(data & 0xFF); - i2c_stop(); - Serial.print("Sent command: "); - Serial.print(cmd, HEX); - Serial.print(", value: "); - Serial.println(value); - - - - - - - -// uint16_t data = (uint16_t)(value * 16.0); -// data |= cmd << 4; -// i2c_start(); -// i2c_write(PWM_SLAVE_ADDR << 1); -// i2c_write(data >> 8); -// Serial.print("Sent byte 1: "); -// Serial.println(data >> 8, HEX); -// i2c_write(data & 0xFF); -// Serial.print("Sent byte 2: "); -// Serial.println(data & 0xFF, HEX); -// i2c_stop(); -// Serial.print("Sent command: "); -// Serial.print(cmd, HEX); -// Serial.print(", value: "); -// Serial.println(value); -} - - - - -//VERSION 3.x - - -// #include -// #include -// #include - - -// void i2c_init(); -// void i2c_start(); -// void i2c_stop(); -// void i2c_write(uint8_t data); -// uint8_t i2c_read_ack(); -// uint8_t i2c_read_nack(); - - - -// #define F_CPU 16000000UL -// #define I2C_FREQ 100000UL -// #define I2C_PRESCALER 1 -// #define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) - -// void i2c_init() { -// TWBR = I2C_BITRATE; -// } - -// void i2c_start() { -// // отправляем START bit -// TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); - -// // ожидаем пока START bit будет успешно отправлен -// while (!(TWCR & (1 << TWINT))); -// } - -// void i2c_stop() { -// // отправляем STOP bit -// TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); - -// // ожидаем пока STOP bit будет успешно отправлен -// while (TWCR & (1 << TWSTO)); -// } - -// void i2c_write(uint8_t data) { -// // загружаем данные в регистр TWDR -// TWDR = data; - -// // отправляем данные -// TWCR = (1 << TWINT) | (1 << TWEN); - -// // ожидаем пока данные будут успешно отправлены -// while (!(TWCR & (1 << TWINT))); -// } - -// uint8_t i2c_read_ack() { -// // разрешаем отправку ACK после прочтения байта -// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); - -// // ожидаем пока данные будут успешно прочитаны -// while (!(TWCR & (1 << TWINT))); - -// // возвращаем прочитанный байт -// return TWDR; -// } - -// uint8_t i2c_read_nack() { -// // запрещаем отправку ACK после прочтения байта -// TWCR = (1 << TWINT) | (1 << TWEN); - -// // ожидаем пока данные будут успешно прочитаны -// while (!(TWCR & (1 << TWINT))); - -// // возвращаем прочитанный байт -// return TWDR; -// } - -// #ifndef PWM_H -// #define PWM_H - -// void pwm_init(); -// void pwm_set_frequency(uint16_t frequency); -// void pwm_set_duty_cycle(uint8_t dutyCycle); -// void pwm_enable(); -// void pwm_disable(); - -// #endif - - - -// void pwm_init() { -// TCCR1A |= (1 << COM1A1) | (1 << WGM11); // non-inverting mode, Fast PWM (mode 14) -// TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); // Fast PWM (mode 14), prescaler = 1 -// DDRB |= (1 << PB1); // set pin PB1 as output -// } - -// void pwm_set_frequency(uint16_t frequency) { -// uint16_t prescaler = 1; -// uint32_t top = F_CPU / (prescaler * frequency) - 1; -// ICR1 = top; -// } - -// void pwm_set_duty_cycle(uint8_t dutyCycle) { -// uint16_t value = ICR1 * dutyCycle / 100.0; -// OCR1A = value; -// } - -// void pwm_enable() { -// pwm_set_duty_cycle(50); // Начальная скважность -// } - -// void pwm_disable() { -// pwm_set_duty_cycle(0); // Выключение ШИМ -// } - -// #define F_CPU 16000000UL -// #define I2C_FREQ 100000UL -// #define I2C_PRESCALER 1 -// #define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) - -// #define BUTTON_PIN_CNT 6 - -// const uint8_t BUTTON_PIN[] = {0, 1, 2, 3, 4, 5}; -// const uint8_t PWM_SLAVE_ADDR = 9; - -// uint16_t command = 0x00; -// float frequency = 1000.0; -// float dutyCycle = 50.0; - -// bool is_button_pressed(uint8_t pin); -// void process_button_press(uint8_t pin); -// void send_command(uint16_t cmd, float value); - -// int main(void) { -// i2c_init(); -// pwm_init(); -// pwm_enable(); // Включение ШИМ - -// for (uint8_t i = 0; i < BUTTON_PIN_CNT; i++) { -// DDRC &= ~(1 << BUTTON_PIN[i]); -// PORTC |= (1 << BUTTON_PIN[i]); -// } - -// Serial.begin(9600); // Инициализируем Serial монитор -// Serial.println("PWM Controller started!"); - -// send_command(0x01, 0.0); // Включить ШИМ при запуске - -// while (true) { -// for (uint8_t i = 0; i < BUTTON_PIN_CNT; i++) { -// if (is_button_pressed(BUTTON_PIN[i])) { -// process_button_press(BUTTON_PIN[i]); -// _delay_ms(500); -// } -// } -// } -// } - -// bool is_button_pressed(uint8_t pin) { -// return bit_is_clear(PINC, pin); -// } - -// void process_button_press(uint8_t pin) { -// switch (pin) { -// case 0: -// send_command(0x01, 0.0); -// Serial.println("Turn on PWM!"); -// break; -// case 1: -// send_command(0x02, 0.0); -// Serial.println("Turn off PWM!"); -// break; -// case 2: -// if (frequency * 1.25 < 10000) { -// frequency *= 1.25; -// pwm_set_frequency(frequency); -// send_command(0x03, 1.25); -// Serial.println("Increased frequency!"); -// } else { -// Serial.println("Maximum frequency reached!"); -// } -// break; -// case 3: -// if (frequency * 0.8 > 1) { -// frequency *= 0.8; -// pwm_set_frequency(frequency); -// send_command(0x04, 0.8); -// Serial.println("Decreased frequency!"); -// } else { -// Serial.println("Minimum frequency reached!"); -// } -// break; -// case 4: -// if (dutyCycle * 1.1 <= 100) { -// dutyCycle *= 1.1; -// pwm_set_duty_cycle(dutyCycle); -// send_command(0x05, 1.1); -// Serial.println("Increased duty cycle!"); -// } else { -// Serial.println("Maximum duty cycle reached!"); -// } -// break; -// case 5: -// if (dutyCycle * 0.9 >= 0) { -// dutyCycle *= 0.9; -// pwm_set_duty_cycle(dutyCycle); -// send_command(0x06, 0.9); -// Serial.println("Decreased duty cycle!"); -// } else { -// Serial.println("Minimum duty cycle reached!"); -// } -// break; -// default: -// break; -// } -// } - -// void send_command(uint16_t cmd, float value) { -// if (cmd != 0x01 && cmd != 0x02) { // если команда изменения параметров -// switch (cmd) { -// case 0x03: // Увеличить частоту на 25% от текущего. -// value = frequency; -// value *= 0.25; -// break; -// case 0x04: // Уменьшить частоту на 20% от текущего. -// value = frequency; -// value *= 0.2; -// break; -// case 0x05: // Увеличить скважность на 10% от текущего. -// value = dutyCycle; -// value *= 0.1; -// break; -// case 0x06: // Уменьшить скважность на 10% от текущего. -// value = dutyCycle; -// value *= 0.1; -// break; -// } -// } -// uint16_t data = (uint16_t)(value * 16.0); -// data |= cmd << 4; -// i2c_start(); -// i2c_write(PWM_SLAVE_ADDR << 1); -// i2c_write(data >> 8); -// i2c_write(data & 0xFF); -// i2c_stop(); -// Serial.print("Sent command: "); -// Serial.print(cmd, HEX); -// Serial.print(", value: "); -// Serial.println(value); -// } - - - - - - -///test -// #include - -// #define PWM_ADDR 9 // Адрес устройства на шине I2C - -// void setup() { -// Wire.begin(); -// Serial.begin(9600); -// Serial.println("PWM Master device started!"); -// Serial.println("I2C connection established"); -// } - -// void loop() { -// // Генерация случайного значения ШИМ-сигнала -// int pwmValue = random(256); -// // Отправка значения по шине I2C на адрес устройства PWM_ADDR -// Wire.beginTransmission(PWM_ADDR); -// Wire.write(pwmValue); -// Wire.endTransmission(); -// Serial.print("Sent PWM value: "); -// Serial.println(pwmValue); -// delay(1000); // Пауза между отправкой нового значения -// }