extended pwm library
This commit is contained in:
parent
d1ae25e83a
commit
fe558d57e8
93
main.cpp
93
main.cpp
@ -1,45 +1,3 @@
|
|||||||
#include <avr/io.h>
|
|
||||||
|
|
||||||
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 F_CPU 16000000UL
|
||||||
#define I2C_FREQ 100000UL
|
#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;
|
const uint8_t PWM_SLAVE_ADDR = 9;
|
||||||
|
|
||||||
uint16_t command = 0x00;
|
uint16_t command = 0x00;
|
||||||
float frequency = 1000.0;
|
float frequency = pwm_get_frequency();
|
||||||
float dutyCycle = 50.0;
|
float dutyCycle = pwm_get_duty_cycle();
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
i2c_init();
|
i2c_init();
|
||||||
@ -121,7 +80,6 @@ void setup() {
|
|||||||
Serial.begin(9600); // Инициализируем Serial монитор
|
Serial.begin(9600); // Инициализируем Serial монитор
|
||||||
Serial.println("PWM Controller started!");
|
Serial.println("PWM Controller started!");
|
||||||
Serial.println("PWM on master device initial successful!");
|
Serial.println("PWM on master device initial successful!");
|
||||||
|
|
||||||
sendCommand(0x01, 0.0); // Включить ШИМ при запуске
|
sendCommand(0x01, 0.0); // Включить ШИМ при запуске
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,25 +96,36 @@ void loop() {
|
|||||||
void checkButton(uint8_t pin, const char* message, uint16_t cmd, float value) {
|
void checkButton(uint8_t pin, const char* message, uint16_t cmd, float value) {
|
||||||
if (bit_is_clear(PINC, pin)) {
|
if (bit_is_clear(PINC, pin)) {
|
||||||
Serial.println(message);
|
Serial.println(message);
|
||||||
delay(500);
|
delay(1000);
|
||||||
|
|
||||||
|
switch(cmd) {
|
||||||
|
case 0x03:
|
||||||
|
case 0x04:
|
||||||
|
frequency *= value;
|
||||||
|
pwm_set_frequency(frequency);
|
||||||
sendCommand(cmd, value);
|
sendCommand(cmd, value);
|
||||||
if (cmd == 0x03) {
|
break;
|
||||||
frequency *= value;
|
|
||||||
pwm_set_frequency(frequency);
|
case 0x05:
|
||||||
} else if (cmd == 0x04) {
|
case 0x06:
|
||||||
frequency *= value;
|
|
||||||
pwm_set_frequency(frequency);
|
|
||||||
} else if (cmd == 0x05) {
|
|
||||||
dutyCycle *= value;
|
dutyCycle *= value;
|
||||||
pwm_set_duty_cycle(dutyCycle);
|
pwm_set_duty_cycle(dutyCycle);
|
||||||
} else if (cmd == 0x06) {
|
sendCommand(cmd, value);
|
||||||
dutyCycle *= value;
|
break;
|
||||||
pwm_set_duty_cycle(dutyCycle);
|
|
||||||
} else if (cmd == 0x02) {
|
case 0x02:
|
||||||
pwm_disable(); // Выключение ШИМ
|
pwm_disable();
|
||||||
} else if (cmd == 0x01) {
|
sendCommand(cmd, value);
|
||||||
pwm_enable(); // Включение ШИМ
|
pwm_check_state();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 0x01:
|
||||||
|
pwm_enable();
|
||||||
|
sendCommand(cmd, value);
|
||||||
|
pwm_check_state();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
command = cmd;
|
command = cmd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -178,6 +147,7 @@ void sendCommand(uint16_t cmd, float value) {
|
|||||||
i2c_write(PWM_SLAVE_ADDR << 1); // Отправляем адрес устройства
|
i2c_write(PWM_SLAVE_ADDR << 1); // Отправляем адрес устройства
|
||||||
i2c_write(cmd_value >> 8); // Передаем старший байт команды
|
i2c_write(cmd_value >> 8); // Передаем старший байт команды
|
||||||
i2c_write(cmd_value & 0xFF); // Передаем младший байт команды
|
i2c_write(cmd_value & 0xFF); // Передаем младший байт команды
|
||||||
|
Serial.println("Packgae send succesfull!");
|
||||||
i2c_stop(); // Завершаем передачу
|
i2c_stop(); // Завершаем передачу
|
||||||
|
|
||||||
// Выводим информацию о команде в монитор последовательного порта
|
// Выводим информацию о команде в монитор последовательного порта
|
||||||
@ -190,3 +160,6 @@ void sendCommand(uint16_t cmd, float value) {
|
|||||||
Serial.print(" ");
|
Serial.print(" ");
|
||||||
Serial.println(cmd_value & 0xFF, HEX);
|
Serial.println(cmd_value & 0xFF, HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user