#include <PWM.h> // Библиотека PWM для управления частотой сигнала ШИМ
#include <Adafruit_GFX.h> // graphics library
#include <Adafruit_ST7789.h> // library for this display
#include <SPI.h>
#define TFT_CS -1 // if your display has CS pin
#define TFT_RST 7 // reset pin
#define TFT_DC 9 // data pin
//#define TFT_MOSI 11 // Data out
//#define TFT_SCLK 13 // Clock out
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
//Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
int Encoder_OuputA = 4;
int Encoder_OuputB = 6;
int Encoder_Switch = 3;
int Previous_Output;
int multiplier = 1;
double angle = 0;
double increment = 0.2;
const int signal_pin = 10;
const int Sine_pin = 5;
const int POT_pin = A2;
int32_t frequency; // частота, которая должна быть установлена
int32_t lower_level_freq = 1; // Наименьшее возможное значение частоты составляет 1 Гц
int32_t upper_level_freq = 50000; // Максимально возможная частота составляет 100 кГц
void setup() {
tft.init(240, 240, SPI_MODE2); //, SPI_MODE2
tft.setRotation(1); // rotates the screen
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(20, 10); // starts to write text at y10 x10
tft.setTextSize(2); // sets font size
tft.setTextWrap(true);
tft.setTextColor(ST77XX_WHITE);
tft.print("Signal Generator");
tft.setCursor(26, 30); // starts to write text at y10 x10
tft.print("CircuitDigest");
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 10); // starts to write text at y10 x10
tft.print("Freq:00000Hz");
tft.setCursor(22, 30); // starts to write text at y10 x10
tft.print("Inc. by: 1 ");
//Serial.begin(9600); // Последовательный порт для отладки
InitTimersSafe(); // Инициализация таймеров без таймера 0
// настройка режимов выводов
pinMode(Encoder_OuputA, INPUT);
pinMode(Encoder_OuputB, INPUT);
pinMode(Encoder_Switch, INPUT);
Previous_Output = digitalRead(Encoder_OuputA); // Читаем начальное значение выхода энкодера A
attachInterrupt(0, generate_sine, CHANGE);
}
void loop() {
if (digitalRead(Encoder_OuputA) != Previous_Output) {
if (digitalRead(Encoder_OuputB) != Previous_Output) {
frequency = frequency + multiplier;
//Serial.println(frequency);
pwmWriteHR(signal_pin, 32768); // Рабочий цикл 50% по умолчанию, для 16-бит 65536/2 = 32768
SetPinFrequencySafe(signal_pin, frequency);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(50, 30); // starts to write text at y10 x10
tft.print("Freq: Hz");
tft.setCursor(110, 30); // starts to write text at y10 x10
tft.print(frequency);
} else {
frequency = frequency - multiplier;
//Serial.println(frequency);
pwmWriteHR(signal_pin, 32768); // Рабочий цикл 50% по умолчанию, для 16-бит 65536/2 = 32768
SetPinFrequencySafe(signal_pin, frequency);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(50, 30); // starts to write text at y10 x10
tft.print("Freq: Hz");
tft.setCursor(110, 30); // starts to write text at y10 x10
tft.print(frequency);
}
}
if (digitalRead(Encoder_Switch) == 0) {
multiplier = multiplier * 10;
if (multiplier > 1000)
multiplier = 1;
tft.fillScreen(ST77XX_BLACK);
tft.setTextSize(2);
tft.setCursor(50, 80); // starts to write text at y10 x10
tft.print("Cng. by: ");
tft.setCursor(150, 80); // starts to write text at y10 x10
tft.print(multiplier);
//Serial.println(multiplier);
delay(500);
while (digitalRead(Encoder_Switch) == 0)
;
}
Previous_Output = digitalRead(Encoder_OuputA);
}
void generate_sine() {
double sineValue = sin(angle);
sineValue *= 255;
int plot = map(sineValue, -255, +255, 0, 255);
//Serial.println(plot);
analogWrite(Sine_pin, plot);
angle += increment;
if (angle > 180)
angle = 0;
}