From 3dd1f7828b0a509357391cbe7d51c06352f310ef Mon Sep 17 00:00:00 2001 From: DashyFox Date: Mon, 26 Aug 2024 00:11:00 +0300 Subject: [PATCH] stm --- .vscode/arduino.json | 5 +-- MusicRing.ino | 29 +++++++--------- modules/LED_Ring.h | 81 +++++++++++++++++++++++++------------------- 3 files changed, 62 insertions(+), 53 deletions(-) diff --git a/.vscode/arduino.json b/.vscode/arduino.json index 8a13e19..9bd9113 100644 --- a/.vscode/arduino.json +++ b/.vscode/arduino.json @@ -1,5 +1,6 @@ { "port": "COM3", - "board": "arduino:avr:uno", - "sketch": "MusicRing.ino" + "board": "STMicroelectronics:stm32:GenF4", + "sketch": "MusicRing.ino", + "configuration": "pnum=BLACKPILL_F411CE,upload_method=swdMethod,xserial=generic,usb=CDCgen,xusb=FS,opt=osstd,dbg=none,rtlib=nano" } \ No newline at end of file diff --git a/MusicRing.ino b/MusicRing.ino index 74a59cb..340d04e 100644 --- a/MusicRing.ino +++ b/MusicRing.ino @@ -5,35 +5,30 @@ #include "./implement/Display__Adafruit_SSD1306.h" #include "./misc/bitmaps.h" -LED_Ring_PINOUT pinout { 5, 6, 7, 8, 9 }; +LED_Ring_PINOUT pinout { PA4, PA3, PA5, PA1, PA2 }; LED_Ring ring(32, pinout); -Screen screen(128, 32, Display::Ori_HORIZONTAL); + +void ISR() { ring.isr(); } void setup() { Serial.begin(115200); - ring.begin(); - screen.begin(); + ring.begin(ISR); + + + - screen.adafruit->drawBitmap(0, 0, DashyFox_logo, 128, 32, WHITE); - screen.adafruit->display(); } -ISR(TIMER1_COMPA_vect) { ring.isr(); } void loop() { - - static uint32_t tmr; - static uint8_t arr[6] = { 0x3f >> 1, 0xf0 ,0x7f >> 3, 0xf8, 0x3f >> 1, 0xf0 }; - static uint8_t arr2[6] = { 0 }; - static bool f = false; - if (millis() - tmr > 350) { - screen.adafruit->drawBitmap(105, 23, arr, 16, 3, f); - screen.adafruit->display(); - tmr = millis(); - f = !f; + if (Serial.available()) + { + uint8_t input = Serial.parseInt(); + ring.setFrec(input); } + } \ No newline at end of file diff --git a/modules/LED_Ring.h b/modules/LED_Ring.h index 36bbc1a..adfd2f8 100644 --- a/modules/LED_Ring.h +++ b/modules/LED_Ring.h @@ -3,15 +3,17 @@ struct LED_Ring_PINOUT { uint8_t - Data, - CLK_inside, - CLK_outside, - RESET, - OutputEnable; + Data, // yellow + CLK_inside, // red + CLK_outside, // orange + RESET, // green + OutputEnable; // pur }; class LED_Ring { private: + HardwareTimer *MyTim = nullptr; + LED_Ring_PINOUT pinOut; uint16_t ledCount; uint16_t updateFrec; @@ -35,7 +37,7 @@ public: }; - void begin() { + void begin(void(*func)(void)) { reset(); @@ -58,7 +60,7 @@ public: delay(del); } - timerIni(frec); + timerIni(frec, func); } /////////////////////////////////////////////////////////////////////////////////////// @@ -96,7 +98,7 @@ public: ////////////////////////////////////////////////////////////////////////////// void isr() { - if (position >= ledCount) { + if (position > ledCount) { point_ini(1); position = 1; } else shift(1); @@ -104,36 +106,47 @@ public: ////////////////////////////////////////////////////////////////////////////// - void timerIni(uint16_t _frec) { //TODO: Сделать возможность выбора таймеров - period = F_CPU / 8 / ledCount / _frec; - // инициализация Timer1 - cli(); // отключить глобальные прерывания - TCCR1A = 0; // установить регистры в 0 - TCCR1B = 0; +void timerIni(uint16_t _frec, void(*func)(void)) { + // Определяем таймер, например, используем TIM2 + MyTim = new HardwareTimer(TIM2); - OCR1A = period; // установка регистра совпадения - TCCR1B |= (1 << WGM12); // включение в CTC режим + // Рассчитываем период + uint32_t prescaler = 8; // Делитель + period = (SystemCoreClock / (prescaler * _frec)) - 1; - // Установка битов CS на коэффициент деления 8 - TCCR1B |= 0b00000010; + // Останавливаем таймер перед настройкой + MyTim->pause(); - TIMSK1 |= (1 << OCIE1A); // включение прерываний по совпадению - sei(); // включить глобальные прерывания - } - - void setFrec(uint8_t frec) { - period = F_CPU / 8 / ledCount / frec; - cli(); - OCR1A = period; // установка регистра совпадения - sei(); - } + // Настраиваем предделитель (делитель частоты) + MyTim->setPrescaleFactor(prescaler); - void setPeriod(uint8_t per) { - period = per; - cli(); - OCR1A = period; // установка регистра совпадения - sei(); - } + // Настраиваем таймер в режиме CTC (count-to-clear) + MyTim->setOverflow(period); + MyTim->attachInterrupt(func); + MyTim->resume(); +} + +void setFrec(uint16_t frec) { + uint32_t prescaler = 8; // Делитель + period = (SystemCoreClock / (prescaler * frec)) - 1; + + // Останавливаем таймер перед изменением периода + MyTim->pause(); + MyTim->setOverflow(period); + MyTim->refresh(); // Обновление настроек таймера + MyTim->resume(); +} + +void setPeriod(uint32_t per) { + period = per; + + // Останавливаем таймер перед изменением периода + MyTim->pause(); + MyTim->setOverflow(period); + MyTim->refresh(); // Обновление настроек таймера + MyTim->resume(); +} + ///////////////////////////////////////////////////////////////// };