From fe558d57e8e15f1cfb75504d97a1c988a4a12764 Mon Sep 17 00:00:00 2001 From: zloihach <72695485+zloihach@users.noreply.github.com> Date: Thu, 22 Jun 2023 18:31:13 +0300 Subject: [PATCH] extended pwm library --- main.cpp | 99 +++++++++++++++++++++----------------------------------- 1 file changed, 36 insertions(+), 63 deletions(-) diff --git a/main.cpp b/main.cpp index 5a31bfc..fbfbb6e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,45 +1,3 @@ -#include - -void pwm_init() { - // Configure pin PB1 as output - DDRB |= (1 << PB1); - // Configure Timer1 in Fast PWM (mode 14) with non-inverting mode - TCCR1A |= (1 << COM1A1) | (1 << WGM11); - TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS10); -} - -void pwm_set_frequency(uint16_t frequency) { - // Calculate the top value for Timer1 - uint16_t prescaler = 1; - uint32_t top = F_CPU / (prescaler * frequency) - 1; - ICR1 = top; -} - -void pwm_set_duty_cycle(uint8_t duty_cycle) { - // Calculate the OCR1A value corresponding to the duty cycle - uint16_t value = ICR1 * duty_cycle / 100; - OCR1A = value; -} - -void pwm_enable() { - pwm_set_duty_cycle(50); // Initial duty cycle -} - -void pwm_disable() { - pwm_set_duty_cycle(0); // Turn off PWM -} - -uint16_t pwm_get_frequency() { - uint16_t prescaler = 1; - uint32_t frequency = F_CPU / (prescaler * (ICR1 + 1)); - return frequency; -} - -uint8_t pwm_get_duty_cycle() { - uint8_t duty_cycle = OCR1A * 100 / ICR1; - return duty_cycle; -} - #define F_CPU 16000000UL #define I2C_FREQ 100000UL @@ -106,8 +64,9 @@ 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; +float frequency = pwm_get_frequency(); +float dutyCycle = pwm_get_duty_cycle(); + void setup() { i2c_init(); @@ -121,7 +80,6 @@ void setup() { Serial.begin(9600); // Инициализируем Serial монитор Serial.println("PWM Controller started!"); Serial.println("PWM on master device initial successful!"); - sendCommand(0x01, 0.0); // Включить ШИМ при запуске } @@ -138,25 +96,36 @@ void loop() { 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(); // Включение ШИМ + delay(1000); + + switch(cmd) { + case 0x03: + case 0x04: + frequency *= value; + pwm_set_frequency(frequency); + sendCommand(cmd, value); + break; + + case 0x05: + case 0x06: + dutyCycle *= value; + pwm_set_duty_cycle(dutyCycle); + sendCommand(cmd, value); + break; + + case 0x02: + pwm_disable(); + sendCommand(cmd, value); + pwm_check_state(); + break; + + case 0x01: + pwm_enable(); + sendCommand(cmd, value); + pwm_check_state(); + break; } + command = cmd; } } @@ -178,6 +147,7 @@ void sendCommand(uint16_t cmd, float value) { i2c_write(PWM_SLAVE_ADDR << 1); // Отправляем адрес устройства i2c_write(cmd_value >> 8); // Передаем старший байт команды i2c_write(cmd_value & 0xFF); // Передаем младший байт команды + Serial.println("Packgae send succesfull!"); i2c_stop(); // Завершаем передачу // Выводим информацию о команде в монитор последовательного порта @@ -190,3 +160,6 @@ void sendCommand(uint16_t cmd, float value) { Serial.print(" "); Serial.println(cmd_value & 0xFF, HEX); } + + +