Hello,
I need program with which to control 6 outputs/ transistors with 200 or 300 khz. But i have made this one
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Инициализация на LCD дисплея с адрес 0x27 и размер 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int outputPin1 = 9; // Първи изходен пин за сигнала
const int outputPin2 = 8; // Втори изходен пин за сигнала
const int outputPin3 = 7; // Трети изходен пин за сигнала
int dutyCycle = 50; // Запълване по подразбиране - 50%
void setup() {
Serial.begin(9600);
// Инициализация на LCD дисплея
lcd.init();
lcd.backlight(); // Включване на подсветката на LCD
// Показване на съобщение за инициализация
lcd.setCursor(0, 0);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
// Настройка на PWM за изходите
setupPWM(outputPin1, 300000, dutyCycle); // Изход 1
setupPWM(outputPin2, 300000, dutyCycle); // Изход 2
setupPWM(outputPin3, 300000, dutyCycle); // Изход 3
Serial.println("Signal generator at 300 kHz on three outputs.");
Serial.println("Enter 'D' followed by duty cycle in percent (0 - 100) to set duty cycle.");
updateDisplay();
}
void loop() {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n');
input.trim(); // Премахва излишни интервали и нови редове
handleSerialInput(input);
}
}
void handleSerialInput(String input) {
if (input.startsWith("D")) {
int newDutyCycle = input.substring(1).toInt();
if (newDutyCycle >= 0 && newDutyCycle <= 100) {
dutyCycle = newDutyCycle;
Serial.print("Duty cycle set to: ");
Serial.print(dutyCycle);
Serial.println("%");
// Актуализация на PWM за всички изходи
updatePWM(outputPin1, 300000, dutyCycle);
updatePWM(outputPin2, 300000, dutyCycle);
updatePWM(outputPin3, 300000, dutyCycle);
updateDisplay();
} else {
Serial.println("Error: Duty cycle out of range (0 - 100%)");
}
} else {
Serial.println("Invalid command.");
}
}
void setupPWM(int pin, int frequency, int duty) {
uint32_t pwmFreq = frequency; // Честота в Hz
uint32_t pwmDuty = (duty * 255) / 100; // Duty cycle като стойност от 0 до 255
pmc_enable_periph_clk(PWM_INTERFACE_ID);
PWMC_ConfigureClocks(pwmFreq * 255, 0, VARIANT_MCK);
PIO_Configure(g_APinDescription[pin].pPort,
g_APinDescription[pin].ulPinType,
g_APinDescription[pin].ulPin,
g_APinDescription[pin].ulPinConfiguration);
uint32_t channel = g_APinDescription[pin].ulPWMChannel;
PWMC_ConfigureChannel(PWM, channel, PWM_CMR_CPRE_CLKA, 0, 0);
PWMC_SetPeriod(PWM, channel, 255);
PWMC_SetDutyCycle(PWM, channel, pwmDuty);
PWMC_EnableChannel(PWM, channel);
}
void updatePWM(int pin, int frequency, int duty) {
uint32_t pwmDuty = (duty * 255) / 100; // Duty cycle като стойност от 0 до 255
uint32_t channel = g_APinDescription[pin].ulPWMChannel;
PWMC_SetDutyCycle(PWM, channel, pwmDuty);
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Freq: 300 kHz");
lcd.setCursor(0, 1);
lcd.print("Duty: ");
lcd.print(dutyCycle);
lcd.print("% ");
}
And i have 300 khz - measured with oscilloscope. I cannot insert 3 pins more, because i found out that pins 7,8,9 are directly connected to PWM controller of the Due.
I have tried to insert 3 more but their frequency is 170hz and below.
My question is: Can someone to help me to configurate properly 3 more pins like 7,8,9 to generate frequency from 300 khz.
Thank you in advance!
Best Regards,