From 0947951e15a9ad81a7dea0205141cb73905287fa Mon Sep 17 00:00:00 2001 From: manuel Date: Mon, 21 Dec 2020 15:34:36 +0100 Subject: Add initial glass lathe test code --- linz/glasslathe/.gitignore | 5 ++ linz/glasslathe/platformio.ini | 15 ++++++ linz/glasslathe/src/main.cpp | 104 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 linz/glasslathe/.gitignore create mode 100644 linz/glasslathe/platformio.ini create mode 100644 linz/glasslathe/src/main.cpp diff --git a/linz/glasslathe/.gitignore b/linz/glasslathe/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/linz/glasslathe/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/linz/glasslathe/platformio.ini b/linz/glasslathe/platformio.ini new file mode 100644 index 0000000..f2cb76e --- /dev/null +++ b/linz/glasslathe/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega328new] +platform = atmelavr +board = nanoatmega328new +framework = arduino +lib_deps = paulstoffregen/TimerOne @ ^1.1 \ No newline at end of file diff --git a/linz/glasslathe/src/main.cpp b/linz/glasslathe/src/main.cpp new file mode 100644 index 0000000..85b49dd --- /dev/null +++ b/linz/glasslathe/src/main.cpp @@ -0,0 +1,104 @@ +#include +#include + +void analogWriteSAH_Init(void) +{ + // Stop the timer while we muck with it + TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (0 << CS10); + + // Set the timer to mode 14... + // + // Mode WGM13 WGM12 WGM11 WGM10 Timer/Counter Mode of Operation TOP Update of OCR1x at TOV1 Flag Set on + // CTC1 PWM11 PWM10 + // ---- ----- ----- ----- ----- ------------------------------- ---- ----------------------- ----------- + // 14 1 1 1 0 Fast PWM ICR1 BOTTOM TOP + + // Set output on Channel A to... + // + // COM1A1 COM1A0 Description + // ------ ------ ----------------------------------------------------------- + // 1 0 Clear OC1A/OC1B on Compare Match (Set output to low level). + TCCR1A = + (1 << COM1A1) | (0 << COM1A0) | // COM1A1, COM1A0 = 1, 0 + (0 << COM1B1) | (0 << COM1B0) | + (1 << WGM11) | (0 << WGM10); // WGM11, WGM10 = 1, 0 + + // Set TOP to... + // + // fclk_I/O = 16000000 + // N = 1 + // TOP = 799 + // + // fOCnxPWM = fclk_I/O / (N * (1 + TOP)) + // fOCnxPWM = 16000000 / (1 * (1 + 799)) + // fOCnxPWM = 16000000 / 800 + // fOCnxPWM = 20000 + ICR1 = 799; + + // Ensure the first slope is complete + TCNT1 = 0; + + // Ensure Channel B is irrelevant + OCR1B = 0; + + // Ensure Channel A starts at zero / off + OCR1A = 0; + + // We don't need no stinkin interrupts + TIMSK1 = (0 << ICIE1) | (0 << OCIE1B) | (0 << OCIE1A) | (0 << TOIE1); + + // Ensure the Channel A pin is configured for output + DDRB |= (1 << DDB1); + + // Start the timer... + // + // CS12 CS11 CS10 Description + // ---- ---- ---- ------------------------ + // 0 0 1 clkI/O/1 (No prescaling) + TCCR1B = + (0 << ICNC1) | (0 << ICES1) | + (1 << WGM13) | (1 << WGM12) | // WGM13, WGM12 = 1, 1 + (0 << CS12) | (0 << CS11) | (1 << CS10); +} + +void analogWriteD9(uint16_t value) +{ + if ((value >= 0) && (value < 800)) + { + OCR1A = value; + } +} + +void setup() +{ + //analogWriteSAH_Init(); + Timer1.initialize(50); + Timer1.pwm(9, 1024/2); + Serial.begin(9600); +} + +void loop() +{ + static int old_poti = 0; + int poti = analogRead(A7) / 10; + if (poti != old_poti) { + // period_us = 1 / khz * 1000 + int period_us = (poti == 0) ? 0 : map(poti, 1, 102, 600, 50); + Timer1.initialize(period_us); + Timer1.pwm(9, 1024/2); + Serial.println(poti); + Serial.println(period_us); + old_poti = poti; + } + //Timer1.setPeriod(); + //Timer1.setPeriod(50); + //Timer1.restart(); + +#if 0 + for (float frequency = 500; frequency < 3000; frequency = frequency + 1) { + float T = 1 / frequency * 1000000; + //Timer1.setPeriod(T); + delay(5); + } +#endif +} \ No newline at end of file -- cgit v1.2.3