This commit is contained in:
zloihach 2023-06-23 13:04:33 +03:00
parent d133eb9dd9
commit f498e9b0cc

480
main.c
View File

@ -1,256 +1,256 @@
//#define F_CPU 16000000UL #define F_CPU 16000000UL
//#define I2C_FREQ 100000UL #define I2C_FREQ 100000UL
//#define I2C_PRESCALER 1 #define I2C_PRESCALER 1
//#define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER) #define I2C_BITRATE ((F_CPU / I2C_FREQ) - 16) / (2 * I2C_PRESCALER)
//
//#include <avr/io.h> #include <avr/io.h>
//#include <avr/interrupt.h> #include <avr/interrupt.h>
//#include <util/delay.h> #include <util/delay.h>
//
//float pwm_frequency = 1.0; float pwm_frequency = 1.0;
//float pwm_duty_cycle = 0.5; float pwm_duty_cycle = 0.5;
//
//volatile uint8_t pwm_enabled = 0; volatile uint8_t pwm_enabled = 0;
//volatile uint8_t pwm_changed = 0; volatile uint8_t pwm_changed = 0;
//
//void i2c_init() { void i2c_init() {
// TWBR = I2C_BITRATE; TWBR = I2C_BITRATE;
//} }
//
//void i2c_start() { void i2c_start() {
// TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
// while (!(TWCR & (1 << TWINT))) while (!(TWCR & (1 << TWINT)))
// ; ;
//} }
//
//void i2c_stop() { void i2c_stop() {
// TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN); TWCR = (1 << TWINT) | (1 << TWSTO) | (1 << TWEN);
// while (TWCR & (1 << TWSTO)) while (TWCR & (1 << TWSTO))
// ; ;
//} }
//
//void i2c_write(uint8_t data) { void i2c_write(uint8_t data) {
// TWDR = data; TWDR = data;
// TWCR = (1 << TWINT) | (1 << TWEN); TWCR = (1 << TWINT) | (1 << TWEN);
// while (!(TWCR & (1 << TWINT))) while (!(TWCR & (1 << TWINT)))
// ; ;
//} }
//
//uint8_t i2c_read_ack() { uint8_t i2c_read_ack() {
// TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
// while (!(TWCR & (1 << TWINT))) while (!(TWCR & (1 << TWINT)))
// ; ;
// return TWDR; return TWDR;
//} }
//
//uint8_t i2c_read_nack() { uint8_t i2c_read_nack() {
// TWCR = (1 << TWINT) | (1 << TWEN); TWCR = (1 << TWINT) | (1 << TWEN);
// while (!(TWCR & (1 << TWINT))) while (!(TWCR & (1 << TWINT)))
// ; ;
// return TWDR; return TWDR;
//} }
//
//const uint8_t PWM_SLAVE_ADDR = 9; const uint8_t PWM_SLAVE_ADDR = 9;
//
//void pwm_enable() { void pwm_enable() {
// pwm_enabled = 1; pwm_enabled = 1;
// pwm_changed = 1; pwm_changed = 1;
//} }
//
//void pwm_disable() { void pwm_disable() {
// pwm_enabled = 0; pwm_enabled = 0;
// pwm_changed = 1; pwm_changed = 1;
//} }
//
//void setPWMFrequency(float frequency) { void setPWMFrequency(float frequency) {
// if (frequency < 1.0 || frequency > 50000.0) { if (frequency < 1.0 || frequency > 50000.0) {
// return; // Недопустимая частота return; // Недопустимая частота
// } }
// pwm_frequency = frequency; pwm_frequency = frequency;
// pwm_changed = 1; pwm_changed = 1;
//} }
//
//void setPWMDutyCycle(float duty_cycle) { void setPWMDutyCycle(float duty_cycle) {
// if (duty_cycle < 0.0 || duty_cycle > 1.0) { if (duty_cycle < 0.0 || duty_cycle > 1.0) {
// return; // Недопустимый коэффициент заполнения return; // Недопустимый коэффициент заполнения
// } }
// pwm_duty_cycle = duty_cycle; pwm_duty_cycle = duty_cycle;
// pwm_changed = 1; pwm_changed = 1;
//} }
//
//void pwm_check_state() { void pwm_check_state() {
// if (pwm_enabled) { if (pwm_enabled) {
// // Включаем пин ШИМ - реализация зависит от аппаратной платформы // Включаем пин ШИМ - реализация зависит от аппаратной платформы
// Serial.print("PWM enabled. Frequency: "); Serial.print("PWM enabled. Frequency: ");
// Serial.print(pwm_frequency); Serial.print(pwm_frequency);
// Serial.print(" Hz, duty cycle: "); Serial.print(" Hz, duty cycle: ");
// Serial.println(pwm_duty_cycle); Serial.println(pwm_duty_cycle);
// } else { } else {
// // Выключаем пин ШИМ - реализация зависит от аппаратной платформы // Выключаем пин ШИМ - реализация зависит от аппаратной платформы
// Serial.println("PWM disabled"); Serial.println("PWM disabled");
// } }
//} }
//
//void pwm_set_frequency(float frequency) { void pwm_set_frequency(float frequency) {
// // Устанавливаем частоту ШИМ - реализация зависит от аппаратной платформы // Устанавливаем частоту ШИМ - реализация зависит от аппаратной платформы
// Serial.print("Setting PWM frequency to: "); Serial.print("Setting PWM frequency to: ");
// Serial.println(frequency); Serial.println(frequency);
//} }
//
//void pwm_set_duty_cycle(float duty_cycle) { void pwm_set_duty_cycle(float duty_cycle) {
// // Устанавливаем коэффициент заполнения ШИМ - реализация зависит от аппаратной платформы // Устанавливаем коэффициент заполнения ШИМ - реализация зависит от аппаратной платформы
// Serial.print("Setting PWM duty cycle to: "); Serial.print("Setting PWM duty cycle to: ");
// Serial.println(duty_cycle); Serial.println(duty_cycle);
//} }
//
//void enablePWM() { void enablePWM() {
// pwm_enable(); pwm_enable();
// // Включаем ШИМ // Включаем ШИМ
// sendCommand(0x01, 0.0); sendCommand(0x01, 0.0);
//} }
//
//void disablePWM() { void disablePWM() {
// // Выключаем ШИМ // Выключаем ШИМ
// sendCommand(0x02, 0.0); sendCommand(0x02, 0.0);
// pwm_disable(); pwm_disable();
//} }
//
//void increaseFrequency() { void increaseFrequency() {
// setPWMFrequency(pwm_frequency * 1.25); setPWMFrequency(pwm_frequency * 1.25);
// // Выключаем ШИМ, отправляем команду и включаем обратно // Выключаем ШИМ, отправляем команду и включаем обратно
// disablePWM(); disablePWM();
// sendCommand(0x03, pwm_frequency); sendCommand(0x03, pwm_frequency);
// enablePWM(); enablePWM();
//} }
//
//void decreaseFrequency() { void decreaseFrequency() {
// setPWMFrequency(pwm_frequency * 0.8); setPWMFrequency(pwm_frequency * 0.8);
// // Выключаем ШИМ, отправляем команду и включаем обратно // Выключаем ШИМ, отправляем команду и включаем обратно
// disablePWM(); disablePWM();
// sendCommand(0x04, pwm_frequency); sendCommand(0x04, pwm_frequency);
// enablePWM(); enablePWM();
//} }
//
//void increaseDutyCycle() { void increaseDutyCycle() {
// if (pwm_duty_cycle < 0.9) { if (pwm_duty_cycle < 0.9) {
// setPWMDutyCycle(pwm_duty_cycle * 1.1); setPWMDutyCycle(pwm_duty_cycle * 1.1);
// // Выключаем ШИМ, отправляем команду и включаем обратно // Выключаем ШИМ, отправляем команду и включаем обратно
// disablePWM(); disablePWM();
// sendCommand(0x05, pwm_duty_cycle); sendCommand(0x05, pwm_duty_cycle);
// enablePWM(); enablePWM();
// } else { } else {
// Serial.println("Maximum duty cycle reached!"); Serial.println("Maximum duty cycle reached!");
// } }
//} }
//
//void decreaseDutyCycle() { void decreaseDutyCycle() {
// if (pwm_duty_cycle > 0.1) { if (pwm_duty_cycle > 0.1) {
// setPWMDutyCycle(pwm_duty_cycle*0.9); setPWMDutyCycle(pwm_duty_cycle*0.9);
// // Выключаем ШИМ, отправляем команду и включаем обратно // Выключаем ШИМ, отправляем команду и включаем обратно
// disablePWM(); disablePWM();
// sendCommand(0x06, pwm_duty_cycle); sendCommand(0x06, pwm_duty_cycle);
// enablePWM(); enablePWM();
// } else { } else {
// Serial.println("Minimum duty cycle reached!"); Serial.println("Minimum duty cycle reached!");
// } }
//} }
//
//void checkButton(uint8_t pin, const char* message, void (*command)()) { void checkButton(uint8_t pin, const char* message, void (*command)()) {
// if (bit_is_clear(PINC, pin)) { if (bit_is_clear(PINC, pin)) {
// Serial.println(message); Serial.println(message);
// delay(200); delay(200);
// command(); command();
// } }
//} }
//
//void setup() { void setup() {
// i2c_init(); i2c_init();
// DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5); DDRC &= ~(1 << PINC0) & ~(1 << PINC1) & ~(1 << PINC2) & ~(1 << PINC3) & ~(1 << PINC4) & ~(1 << PINC5);
// PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5); PORTC |= (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) | (1 << PINC4) | (1 << PINC5);
// Serial.begin(9600); Serial.begin(9600);
// Serial.println("PWM Controller started!"); Serial.println("PWM Controller started!");
// sendCommand(0x01, 0.0); sendCommand(0x01, 0.0);
//} }
//
//void loop() { void loop() {
// checkButton(0, "Turn on PWM!", enablePWM); checkButton(0, "Turn on PWM!", enablePWM);
// checkButton(1, "Turn off PWM!", disablePWM); checkButton(1, "Turn off PWM!", disablePWM);
// checkButton(2, "Increase frequency by 25%!", increaseFrequency); checkButton(2, "Increase frequency by 25%!", increaseFrequency);
// checkButton(3, "Decrease frequency by 20%!", decreaseFrequency); checkButton(3, "Decrease frequency by 20%!", decreaseFrequency);
// checkButton(4, "Increase duty cycle by 10%!", increaseDutyCycle); checkButton(4, "Increase duty cycle by 10%!", increaseDutyCycle);
// checkButton(5, "Decrease duty cycle by 10%!", decreaseDutyCycle); checkButton(5, "Decrease duty cycle by 10%!", decreaseDutyCycle);
//
// // Обновляем состояние ШИМ, если что-то изменилось // Обновляем состояние ШИМ, если что-то изменилось
// if (pwm_changed) { if (pwm_changed) {
// pwm_changed = 0; pwm_changed = 0;
// pwm_check_state(); pwm_check_state();
// if (pwm_enabled) { if (pwm_enabled) {
// pwm_set_frequency(pwm_frequency); pwm_set_frequency(pwm_frequency);
// pwm_set_duty_cycle(pwm_duty_cycle); pwm_set_duty_cycle(pwm_duty_cycle);
// } }
// } }
//} }
//
//
void sendCommand(uint8_t cmd, float value) {
// Приводим значение к 12 битам для целой части и 4 битам для дробной части
uint16_t integer_part = (uint16_t)value;
uint8_t fractional_part = (uint8_t)((value - integer_part) * 16);
// Проверяем, чтобы значение не превышало допустимых границ
if (integer_part > 0xFFF) {
integer_part = 0xFFF;
}
if (fractional_part > 0xF) {
fractional_part = 0xF;
}
// Формируем 4 байта данных для отправки
uint8_t upper_byte = (integer_part >> 4) & 0xFF;
uint8_t lower_byte = ((integer_part & 0xF) << 4) | (fractional_part & 0xF);
i2c_start();
i2c_write(PWM_SLAVE_ADDR << 1);
i2c_write(cmd);
i2c_write(upper_byte);
i2c_write(lower_byte);
i2c_stop();
Serial.print("Sent command: 0x");
Serial.print(cmd, HEX);
Serial.print(", value: ");
Serial.print(value);
Serial.print(", cmd value: ");
Serial.print((integer_part << 4) | fractional_part);
Serial.print(", data bytes: 0x");
Serial.print(upper_byte, HEX);
Serial.print(" ");
Serial.println(lower_byte, HEX);
}
// void sendCommand(uint8_t cmd, float value) { // void sendCommand(uint8_t cmd, float value) {
// // Приводим значение к 12 битам для целой части и 4 битам для дробной части // uint16_t cmd_value = ((uint16_t)cmd << 8) | (uint16_t)(value * 16);
// uint16_t integer_part = (uint16_t)value; // uint8_t upper_byte = cmd_value >> 8;
// uint8_t fractional_part = (uint8_t)((value - integer_part) * 16); // uint8_t lower_byte = cmd_value & 0xFF;
//
// // Проверяем, чтобы значение не превышало допустимых границ
// if (integer_part > 0xFFF) {
// integer_part = 0xFFF;
// }
// if (fractional_part > 0xF) {
// fractional_part = 0xF;
// }
//
// // Формируем 4 байта данных для отправки
// uint8_t upper_byte = (integer_part >> 4) & 0xFF;
// uint8_t lower_byte = ((integer_part & 0xF) << 4) | (fractional_part & 0xF);
//
// i2c_start(); // i2c_start();
// i2c_write(PWM_SLAVE_ADDR << 1); // i2c_write(PWM_SLAVE_ADDR << 1);
// i2c_write(cmd); // i2c_write(cmd);
// i2c_write(upper_byte); // i2c_write(upper_byte);
// i2c_write(lower_byte); // i2c_write(lower_byte);
// i2c_stop(); // i2c_stop();
//
// Serial.print("Sent command: 0x"); // Serial.print("Sent command: 0x");
// Serial.print(cmd, HEX); // Serial.print(cmd, HEX);
// Serial.print(", value: "); // Serial.print(", value: ");
// Serial.print(value); // Serial.print(cmd_value / 16);
// Serial.print(", cmd value: "); // Serial.print(", cmd value: ");
// Serial.print((integer_part << 4) | fractional_part); // Serial.print(cmd_value);
// Serial.print(", data bytes: 0x"); // Serial.print(", data bytes: 0x");
// Serial.print(upper_byte, HEX); // Serial.print(upper_byte, HEX);
// Serial.print(" "); // Serial.print(" ");
// Serial.println(lower_byte, HEX); // Serial.println(lower_byte, HEX);
// } // }
//
//
//
//
//// void sendCommand(uint8_t cmd, float value) {
//// uint16_t cmd_value = ((uint16_t)cmd << 8) | (uint16_t)(value * 16);
//// uint8_t upper_byte = cmd_value >> 8;
//// uint8_t lower_byte = cmd_value & 0xFF;
//
//// i2c_start();
//// i2c_write(PWM_SLAVE_ADDR << 1);
//// i2c_write(cmd);
//// i2c_write(upper_byte);
//// i2c_write(lower_byte);
//// i2c_stop();
//
//// Serial.print("Sent command: 0x");
//// Serial.print(cmd, HEX);
//// Serial.print(", value: ");
//// Serial.print(cmd_value / 16);
//// Serial.print(", cmd value: ");
//// Serial.print(cmd_value);
//// Serial.print(", data bytes: 0x");
//// Serial.print(upper_byte, HEX);
//// Serial.print(" ");
//// Serial.println(lower_byte, HEX);
//// }